0% found this document useful (0 votes)
18 views3 pages

Phrase Solver Game Implementation

This Java code defines a PhraseSolver class that runs a phrase guessing game between two Player objects. The class contains attributes for the players, board, and current player guess. The play() method runs the game logic in a loop, alternating turns between players and having them guess letters or the full phrase. Points are tracked and a winner is declared once the phrase is solved or all guesses are exhausted.

Uploaded by

718697
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Phrase Solver Game Implementation

This Java code defines a PhraseSolver class that runs a phrase guessing game between two Player objects. The class contains attributes for the players, board, and current player guess. The play() method runs the game logic in a loop, alternating turns between players and having them guess letters or the full phrase. Points are tracked and a winner is declared once the phrase is solved or all guesses are exhausted.

Uploaded by

718697
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/*

* Activity 2.5.2
*
* The PhraseSolver class the PhraseSolverGame
*/
import [Link];

public class PhraseSolver


{
/* your code here - attributes */
private Player player1;
private Player player2;
private Board game;
private int playerGuessPhrase;

/* your code here - constructor(s) */


public PhraseSolver(){
player1 = new Player();
player2 = new Player();
game = new Board();
}
/* your code here - accessor(s) */

/* your code here - mutator(s) */


public void play()
{
boolean solved = false;
int currentPlayer = 1;

Scanner input = new Scanner([Link]);

boolean correct = true;


while (!solved)
{
/* your code here - game logic */
String guess = "";
boolean guessPhrase = false;
while (currentPlayer == 1){
[Link]("Current phrase solved: " + [Link]());
[Link]("Point value of the next letter guess: " +
[Link]());
[Link]("Current player name: " + [Link]());
[Link]("Player points: " + [Link]());
[Link]("Type in a guess(type in a letter, for guessing the
entire phrase or when it is revealed, type in \" try phrase\"): ");
guess = [Link]();
correct = [Link](guess);
if ([Link]() == 1 && correct){
[Link]("You got it correct! You will receive " +
[Link]() + " points.");
[Link]([Link]());
[Link]();
if ([Link]().indexOf("_") == -1){
currentPlayer = 0;
solved = true;
[Link]("You have solved the phrase!");
if ([Link]()>[Link]()){
[Link]([Link]() + " wins with the highest
score!");

This study source was downloaded by 100000835025530 from [Link] on 01-30-2024 23:14:54 GMT -06:00

[Link]
}
else if([Link]()>[Link]()) {
[Link]([Link]() + " wins with the highest
score!");
}
else{
[Link]([Link]() + " and " + [Link]() +
" ties the game!");
}
}
}
else if([Link]("try phrase")) {
currentPlayer = 0;
playerGuessPhrase = 1;
guessPhrase = true;
}
else if (!correct){
[Link]("Your guess was incorrect!");
currentPlayer = 2;
}
}

while (currentPlayer == 2) {
[Link]("Current phrase solved: " + [Link]());
[Link]("Point value of the next letter guess: " +
[Link]());
[Link]("Current player name: " + [Link]());
[Link]("Player points: " + [Link]());
[Link]("Type in a guess(type in a letter, for guessing the
entire phrase or when it is revealed, type in \" try phrase\"): ");
guess = [Link]();
correct = [Link](guess);
if ([Link]() == 1 && correct){
[Link]("You got it correct! You will receive " +
[Link]() + " points.");
[Link]([Link]());
[Link]();
if ([Link]().indexOf("_") == -1){
currentPlayer = 0;
solved = true;
[Link]("You have solved the phrase!");
if ([Link]()>[Link]()){
[Link]([Link]() + " wins with the highest
score!");
}
else if([Link]()>[Link]()) {
[Link]([Link]() + " wins with the highest
score!");
}
else{
[Link]([Link]() + " and " + [Link]()
+ " ties the game!");
}
}
}
else if([Link]("try phrase")) {
currentPlayer = 0;
playerGuessPhrase = 2;
guessPhrase = true;

This study source was downloaded by 100000835025530 from [Link] on 01-30-2024 23:14:54 GMT -06:00

[Link]
}
else if (!correct){
[Link]("You guess was incorrect!");
currentPlayer = 1;
}
}

/* your code here - determine how game ends */


if (guessPhrase){
[Link]("Type in your guess for the entire phrase: ");
guess = [Link]();
if ([Link](guess)){
solved = true;
[Link]("The phrase has been solved!");
if ([Link]()>[Link]()){
[Link]([Link]() + " wins with the highest score!");
}
else if([Link]()>[Link]()) {
[Link]([Link]() + " wins with the highest score!");
}
else{
[Link]([Link]() + " and " + [Link]() + "
ties the game!");
}
}
else{
[Link]("Your guess for the phrase was incorrect! The chance
will pass on to the other player!");
if (playerGuessPhrase == 1){
currentPlayer = 2;
}
else{
currentPlayer = 1;
}
}
}
}
}

This study source was downloaded by 100000835025530 from [Link] on 01-30-2024 23:14:54 GMT -06:00

[Link]
Powered by TCPDF ([Link])

Common questions

Powered by AI

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 .

You might also like