Phrase Solver Game Implementation
Phrase Solver Game Implementation
The game in the PhraseSolver class ends either when a player successfully solves the entire phrase or when the phrase is completed through letter guesses without any blanks remaining. In both cases, the solved flag is set to true, terminating the game loop. If the phrase is guessed successfully by entering the complete phrase correctly, determined by calling game.isSolved(guess), or if all letters are correctly guessed filling in the entire phrase, the game announces that the phrase is solved and then compares player scores to determine the winner or if it is a tie .
The PhraseSolver class ensures that the phrase is fully solved by checking whether there are any blanks left in the phrase using the condition game.getSolvedPhrase().indexOf("_") == -1. This condition is evaluated within player turns to determine if the phrase has been completed without any missing letters. Once no blanks remain, the solved variable is set to true, which concludes the game loop. Additionally, when a player guesses the entire phrase, the correctness is verified with game.isSolved(guess), which will also end the game if true .
At the end of the PhraseSolver game, once the phrase is solved, the system checks the scores of both players. If both players have equal points, a message is displayed indicating that the game has ended in a tie, specifically stating that player1 and player2 tied the game .
In the PhraseSolver game, after an incorrect letter guess, the current player is switched to the other player. This is managed using the currentPlayer variable, which is set to 2 if the incorrect guess was made by player 1, and set to 1 if the incorrect guess was made by player 2. This effectively passes the turn to the other player for their guess .
The playerGuessPhrase variable tracks which player is attempting to guess the entire phrase. Its main role is to facilitate the transition of turns correctly after a player chooses to guess a phrase. If a player's phrase guess is incorrect, this variable helps decide which player should be given the next opportunity to guess. Specifically, if playerGuessPhrase equals 1, it indicates it was player 1's turn to guess the phrase, resulting in transitioning to player 2 if incorrect, and vice versa. This ensures the correct player gets the subsequent turn .
The PhraseSolver class updates the letter point value using the game.setLetterValue() method. After a correct letter guess, this method is called to reset or change the point value of the next letter guess in preparation for subsequent turns .
During their turns, players are required to input a guessed letter or the entire phrase. If inputting a letter, it must be a single character, and if correct, points are awarded based on the current letter value. If opting to guess the entire phrase by typing "try phrase" and then inputting the phrase, it is checked for correctness using game.isSolved(). Incorrect guesses result in no points and a switch to the other player's turn. All inputs are processed using the Scanner class for reading lines of input, and game-related logic is handled by the methods of the Board game object .
The PhraseSolver class uses a while loop that repeatedly runs until the phrase is solved. Each player's turn within this loop is managed by a currentPlayer variable, indicating which player's turn it is (1 or 2). For each player, the loop prints the current game state, requests a guess, and evaluates the guess using the game.guessLetter() method. If the guess is correct and consists of a single letter, points are awarded based on the game's current letter value and added to the player's total points. If the phrase is solved, the loop ends, and the winner is determined by comparing the points of both players. If a player opts to guess the entire phrase, the loop prompts for the phrase, checks its correctness using game.isSolved(), and determines the winner or passes the turn to the other player in case of an incorrect phrase guess .
The PhraseSolver class maintains the score system using addToPoints() methods defined in the Player class for each player. After a correct letter guess, a player is awarded points equivalent to the value of the letter guessed, determined by game.getCurrentLetterValue(). This ensures accuracy by directly adding the correct amount to the player's point total. Points are updated on each successful guess, with each player having independent point tracking .
The PhraseSolver class calls on the game.getSolvedPhrase() method to display the current state of the partially solved phrase. This information is used by both players during their turns to make informed guesses about the next letter or the entire phrase, helping guide their decisions for maximizing points and ultimately solving the phrase .