0% found this document useful (0 votes)
25 views

PhraseSolver Java

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)
25 views

PhraseSolver Java

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
You are on page 1/ 3

/*

* Activity 2.5.2
*
* The PhraseSolver class the PhraseSolverGame
*/
import java.util.Scanner;

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(System.in);

boolean correct = true;


while (!solved)
{
/* your code here - game logic */
String guess = "";
boolean guessPhrase = false;
while (currentPlayer == 1){
System.out.println("Current phrase solved: " + game.getSolvedPhrase());
System.out.println("Point value of the next letter guess: " +
game.getCurrentLetterValue());
System.out.println("Current player name: " + player1.getName());
System.out.println("Player points: " + player1.getPoints());
System.out.println("Type in a guess(type in a letter, for guessing the
entire phrase or when it is revealed, type in \" try phrase\"): ");
guess = input.nextLine();
correct = game.guessLetter(guess);
if (guess.length() == 1 && correct){
System.out.println("You got it correct! You will receive " +
game.getCurrentLetterValue() + " points.");
player1.addToPoints(game.getCurrentLetterValue());
game.setLetterValue();
if (game.getSolvedPhrase().indexOf("_") == -1){
currentPlayer = 0;
solved = true;
System.out.println("You have solved the phrase!");
if (player1.getPoints()>player2.getPoints()){
System.out.println(player1.getName() + " wins with the highest
score!");

This study source was downloaded by 100000835025530 from CourseHero.com on 01-30-2024 23:14:54 GMT -06:00

https://www.coursehero.com/file/101811162/PhraseSolverjava/
}
else if(player2.getPoints()>player1.getPoints()) {
System.out.println(player2.getName() + " wins with the highest
score!");
}
else{
System.out.println(player1.getName() + " and " + player2.getName() +
" ties the game!");
}
}
}
else if(guess.equals("try phrase")) {
currentPlayer = 0;
playerGuessPhrase = 1;
guessPhrase = true;
}
else if (!correct){
System.out.println("Your guess was incorrect!");
currentPlayer = 2;
}
}

while (currentPlayer == 2) {
System.out.println("Current phrase solved: " + game.getSolvedPhrase());
System.out.println("Point value of the next letter guess: " +
game.getCurrentLetterValue());
System.out.println("Current player name: " + player2.getName());
System.out.println("Player points: " + player2.getPoints());
System.out.println("Type in a guess(type in a letter, for guessing the
entire phrase or when it is revealed, type in \" try phrase\"): ");
guess = input.nextLine();
correct = game.guessLetter(guess);
if (guess.length() == 1 && correct){
System.out.println("You got it correct! You will receive " +
game.getCurrentLetterValue() + " points.");
player2.addToPoints(game.getCurrentLetterValue());
game.setLetterValue();
if (game.getSolvedPhrase().indexOf("_") == -1){
currentPlayer = 0;
solved = true;
System.out.println("You have solved the phrase!");
if (player1.getPoints()>player2.getPoints()){
System.out.println(player1.getName() + " wins with the highest
score!");
}
else if(player2.getPoints()>player1.getPoints()) {
System.out.println(player2.getName() + " wins with the highest
score!");
}
else{
System.out.println(player1.getName() + " and " + player2.getName()
+ " ties the game!");
}
}
}
else if(guess.equals("try phrase")) {
currentPlayer = 0;
playerGuessPhrase = 2;
guessPhrase = true;

This study source was downloaded by 100000835025530 from CourseHero.com on 01-30-2024 23:14:54 GMT -06:00

https://www.coursehero.com/file/101811162/PhraseSolverjava/
}
else if (!correct){
System.out.println("You guess was incorrect!");
currentPlayer = 1;
}
}

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


if (guessPhrase){
System.out.println("Type in your guess for the entire phrase: ");
guess = input.nextLine();
if (game.isSolved(guess)){
solved = true;
System.out.println("The phrase has been solved!");
if (player1.getPoints()>player2.getPoints()){
System.out.println(player1.getName() + " wins with the highest score!");
}
else if(player2.getPoints()>player1.getPoints()) {
System.out.println(player2.getName() + " wins with the highest score!");
}
else{
System.out.println(player1.getName() + " and " + player2.getName() + "
ties the game!");
}
}
else{
System.out.println("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 CourseHero.com on 01-30-2024 23:14:54 GMT -06:00

https://www.coursehero.com/file/101811162/PhraseSolverjava/
Powered by TCPDF (www.tcpdf.org)

You might also like