/*
* Activity 2.5.2
*
* The runner for the PhraseSolverGame
*/
public class Runner
{
public static void main(String[] args)
{
PhraseSolver p = new PhraseSolver();
p.play();
}
}
/*
* Activity 2.5.2
*
* A Player class the PhraseSolverGame
*/
import java.util.Scanner;
public class Player
{
/* your code here - attributes */
private String name;
private int points;
/* your code here - constructor(s) */
public Player()
{
System.out.println("Enter Player Name:");
Scanner sc = new Scanner(System.in);
String newName = sc.nextLine();
name = newName;
System.out.println("Hello and welcome to the game, " + name);
points = 0;
}
public Player(String inputName)
{
name = inputName;
System.out.println("Hello and welcome to the game, " + name);
points = 0;
}
/* your code here - accessor(s) */
public String getName()
{
return name;
}
public int getPoints()
{
return points;
}
public void setPoints(int inputPoints){
points+= 10;
points = inputPoints;
}
public void addToPoints(int value){
points+=value;
}
}
/*
* 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 boolean solved;
/* your code here - constructor(s) */
public PhraseSolver()
{
player1 = new Player();
player2 = new Player();
game = new Board();
solved = false;
}
public void play()
{
game.setLetterValue();
boolean solved = false;
int currentPlayer = 1;
Scanner input = new Scanner(System.in);
boolean correct = true;
while (!solved)
{
System.out.println("Guess a phrase: ");
String guess = input.next();
if(game.guessLetter(guess)){
System.out.println("you guessed the letter " + guess + " correctly");
System.out.println(game.getSolvedPhrase());
if(currentPlayer % 2 ==0){
System.out.println(player1.getName());
player1.setPoints(2);
}else{
System.out.println(player2.getName());
player2.setPoints(2);
}
}else if(game.isSolved(guess)){
System.out.println("You correctly guessed the phrase!");
System.out.println(game.getSolvedPhrase());
solved = true;
if(currentPlayer % 2 ==0){
System.out.println(player1.getName() + " guessed the phrase correctly");
player1.setPoints(10);
System.out.println(player1.getPoints());
}else{
System.out.println(player2.getName() + " guessed the phrase correctly");
player2.setPoints(10);
System.out.println(player2.getPoints());
}
}else{
System.out.println("Your guess was incorrect");
System.out.println(game.getSolvedPhrase());
if(currentPlayer % 2 ==0){
System.out.println(player1.getName());
}else if(currentPlayer % 2 ==1){
System.out.println(player2.getName());
}
}
currentPlayer++;
}
System.out.println(player1.getName() + " score " + player1.getPoints());
System.out.println(player2.getName() + " score " + player2.getPoints());
}
}
import java.util.Scanner;
import java.io.File;
public class Board
{
private String phrase = "";
private String solvedPhrase = "";
private int currentLetterValue;
public Board ()
{
phrase = loadPhrase();
setLetterValue();
}
public String getPhrase()
{
return phrase;
}
public String getSolvedPhrase()
{
return solvedPhrase;
}
public int getLetterValue()
{
return currentLetterValue;
}
public void setLetterValue()
{
int randomInt = (int) ((Math.random() * 10) + 1) * 100;
currentLetterValue = randomInt;
}
public boolean isSolved(String guess)
{
if (phrase.equals(guess))
{
return true;
}
return false;
}
private String loadPhrase()
{
String tempPhrase = "";
int numOfLines = 0;
try
{
Scanner sc = new Scanner(new File("phrases.txt"));
while (sc.hasNextLine())
{
tempPhrase = sc.nextLine().trim();
numOfLines++;
}
} catch(Exception e) { System.out.println("Error reading or parsing phrases.txt"); }
int randomInt = (int) ((Math.random() * numOfLines) + 1);
try
{
int count = 0;
Scanner sc = new Scanner(new File("phrases.txt"));
while (sc.hasNextLine())
{
count++;
String temp = sc.nextLine().trim();
if (count == randomInt)
{
tempPhrase = temp;
}
}
} catch (Exception e) { System.out.println("Error reading or parsing phrases.txt"); }
for (int i = 0; i < tempPhrase.length(); i++)
{
if (tempPhrase.substring(i, i + 1).equals(" "))
{
solvedPhrase += " ";
}
else
{
solvedPhrase += "_ ";
}
}
return tempPhrase;
}
public boolean guessLetter(String guess)
{
boolean foundLetter = false;
String newSolvedPhrase = "";
for (int i = 0; i < phrase.length(); i++)
{
if (phrase.substring(i, i + 1).equals(guess))
{
newSolvedPhrase += guess + " ";
foundLetter = true;
}
else
{
newSolvedPhrase += solvedPhrase.substring(i * 2, i * 2 + 1) + " ";
}
}
solvedPhrase = newSolvedPhrase;
return foundLetter;
}
}