Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I need to do this part in my lab that has passed: In the setupGame method, if

ID: 3721917 • Letter: S

Question

So I need to do this part in my lab that has passed: In the setupGame method, if the user says ‘no’ to indicate that they don’t want to enter their own phrase, randomly pick one of the phrases from your new ArrayList and assign that to the ‘phrase’ field. Can someone look over method addPhrase(); and the setupGame(); method's if else statement to be sure my code A. puts the random phrase in a place that can be recalled and it be recalled properly in the setupGame(); method. The code is below:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package woffortune;

import java.util.ArrayList;
import java.util.Scanner;

/**
* WofFortuneGame class Contains all logistics to run the game
*
* @author clatulip
*/
public class WofFortuneGame {

private boolean puzzleSolved = false;
private Wheel wheel;
private Player player1;
private String phrase = "Once upon a time";
private ArrayList<Letter> userPhrase = new ArrayList<>();
private ArrayList<String> randomPhrase = new ArrayList<>();
private ArrayList<Player> user = new ArrayList<>();
//private Letter[] letter_array = new Letter[16];

/**
* Constructor
*
* @param wheel Wheel
* @throws InterruptedException
*/
public WofFortuneGame(Wheel wheel) throws InterruptedException {
// get the wheel
this.wheel = wheel;

// do all the initialization for the game
setUpGame();
}

/**
* Plays the game
*
* @throws InterruptedException
*/
public void playGame() throws InterruptedException {
// while the puzzle isn't solved, keep going
while (!puzzleSolved) {
// let the current player play
playTurn(player1);
}
}

/**
* Sets up all necessary information to run the game
*/
private void setUpGame() {
//add phrases from addPhrase method
addPhrase();
// create a single player
int numPlayers = 0;

player1 = new Player("Player1");

// print out the rules
System.out.println("RULES!");
System.out.println("Each player gets to spin the wheel, to get a number value");
System.out.println("Each player then gets to guess a letter. If that letter is in the phrase, ");
System.out.println(" the player will get the amount from the wheel for each occurence of the letter");
System.out.println("If you have found a letter, you will also get a chance to guess at the phrase");
System.out.println("Each player only has three guesses, once you have used up your three guesses, ");
System.out.println("you can still guess letters, but no longer solve the puzzle.");
System.out.println();

//ask user if they would like to enter a phrase
Scanner input = new Scanner(System.in);
int choice = 0;
//while loop for user choice
while (choice == 1 || choice == 2) {
System.out.println("Would you like to enter your own phrase? 1 for yes 2 for no 3 to have the computer pick a random phrase");
choice = input.nextInt();
//if loop to see what user input is
if (choice == 1) {
System.out.println("Please type your phrase");
String playerPhrase = input.nextLine();
//for loop to store user phrase into userPhrase ArrayList
for (int i = 0; i < playerPhrase.length(); i++) {
userPhrase.add(new Letter(playerPhrase.charAt(i)));
}
}
//if they choose no, set the phrase to the default phrase
if (choice == 2) {
for (int i = 0; i < phrase.length(); i++) {
userPhrase.add(new Letter(phrase.charAt(i)));
}
}
if (choice == 3) {
addPhrase();
for (int i = 0; i < phrase.length(); i++) {
userPhrase.add(new Letter(phrase.charAt(i)));
}
}
//invalid inputs
else {
System.out.println("Invalid input");
}
}
// for each character in the phrase, create a letter and add to letters array
for (int i = 0; i < phrase.length(); i++) {
//letter_array[i] = new Letter(phrase.charAt(i));
userPhrase.add(new Letter(phrase.charAt(i)));
}
// setup done
}

/**
* Method that uses a random number generator to add a random phrase out of
* ten to be used by game when prompted by the user
*/
private void addPhrase() {
//RNG
int randomChoice = (int) (Math.random() * 10);
//if loops to determine which phrase to add
if (randomChoice == 1) {
randomPhrase.add("This is a game");
}
if (randomChoice == 2) {
randomPhrase.add("This is a sentence");
}
if (randomChoice == 3) {
randomPhrase.add("What do I add");
}
if (randomChoice == 4) {
randomPhrase.add("I'm running out of sentences");
}
if (randomChoice == 5) {
randomPhrase.add("So here is some generic Java sentences");
}
if (randomChoice == 6) {
randomPhrase.add("Hello World");
}
if (randomChoice == 7) {
randomPhrase.add("Java is fun");
}
if (randomChoice == 8) {
randomPhrase.add("I can code now");
}
if (randomChoice == 9) {
randomPhrase.add("Learning Java is rewarding");
}
if (randomChoice == 10) {
randomPhrase.add("I'm completely out of sentences");
}
}

/**
* One player's turn in the game Spin wheel, pick a letter, choose to solve
* puzzle if letter found
*
* @param player
* @throws InterruptedException
*/
private void playTurn(Player player) throws InterruptedException {
int money = 0;
Scanner sc = new Scanner(System.in);

System.out.println(player.getName() + ", you have $" + player.getWinnings());
System.out.println("Spin the wheel! <press enter>");
sc.nextLine();
System.out.println("<SPINNING>");
Thread.sleep(200);
Wheel.WedgeType type = wheel.spin();
System.out.print("The wheel landed on: ");
switch (type) {
case MONEY:
money = wheel.getAmount();
System.out.println("$" + money);
break;

case LOSE_TURN:
System.out.println("LOSE A TURN");
System.out.println("So sorry, you lose a turn.");
return; // doesn't get to guess letter

case BANKRUPT:
System.out.println("BANKRUPT");
player.bankrupt();
return; // doesn't get to guess letter

default:

}
System.out.println("");
System.out.println("Here is the puzzle:");
showPuzzle();
System.out.println();
System.out.println(player.getName() + ", please guess a letter.");
//String guess = sc.next();

char letter = sc.next().charAt(0);
if (!Character.isAlphabetic(letter)) {
System.out.println("Sorry, but only alphabetic characters are allowed. You lose your turn.");
} else {
// search for letter to see if it is in
int numFound = 0;
for (Letter l : userPhrase) {
if ((l.getLetter() == letter) || (l.getLetter() == Character.toUpperCase(letter))) {
l.setFound();
numFound += 1;
}
}
if (numFound == 0) {
System.out.println("Sorry, but there are no " + letter + "'s.");
} else {
if (numFound == 1) {
System.out.println("Congrats! There is 1 letter " + letter + ":");
} else {
System.out.println("Congrats! There are " + numFound + " letter " + letter + "'s:");
}
System.out.println();
showPuzzle();
System.out.println();
player.incrementScore(numFound * money);
System.out.println("You earned $" + (numFound * money) + ", and you now have: $" + player.getWinnings());

System.out.println("Would you like to try to solve the puzzle? (Y/N)");
letter = sc.next().charAt(0);
System.out.println();
if ((letter == 'Y') || (letter == 'y')) {
solvePuzzleAttempt(player);
}
}

}

}

/**
* Logic for when user tries to solve the puzzle
*
* @param player
*/
private void solvePuzzleAttempt(Player player) {

if (player.getNumGuesses() >= 3) {
System.out.println("Sorry, but you have used up all your guesses.");
return;
}

player.incrementNumGuesses();
System.out.println("What is your solution?");
Scanner sc = new Scanner(System.in);
sc.useDelimiter(" ");
String guess = sc.next();
if (guess.compareToIgnoreCase(phrase) == 0) {
System.out.println("Congratulations! You guessed it!");
puzzleSolved = true;
// Round is over. Write message with final stats
// TODO
} else {
System.out.println("Sorry, but that is not correct.");
}
}

/**
* Display the puzzle on the console
*/
private void showPuzzle() {
System.out.print(" ");
for (Letter l : userPhrase) {
if (l.isSpace()) {
System.out.print(" ");
} else {
if (l.isFound()) {
System.out.print(Character.toUpperCase(l.getLetter()) + " ");
} else {
System.out.print(" _ ");
}
}
}
System.out.println();

}

/**
* For a new game reset player's number of guesses to 0
*/
public void reset() {
player1.reset();
}

}

Explanation / Answer

Hello There,

PFB issues identified in addPhrase(); and setupGame(); methods:

addPhrase() Method:

-----------------------------------

setUpGame() method:

-----------------------------------------

if (choice == 3) {

addPhrase();

String generatedRandomPhrase = randomPhrase.get(0); // reading the randomPhrase stored in the randomPhrase list

for (int i = 0; i < generatedRandomPhrase.length(); i++) {

userPhrase.add(new Letter(generatedRandomPhrase.charAt(i)));

}

}

Final Code:

-----------------------------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package woffortune;

import java.util.ArrayList;
import java.util.Scanner;

/**
* WofFortuneGame class Contains all logistics to run the game
*
* @author clatulip
*/
public class WofFortuneGame {

private boolean puzzleSolved = false;
private Wheel wheel;
private Player player1;
private String phrase = "Once upon a time";
private ArrayList<Letter> userPhrase = new ArrayList<>();
private ArrayList<String> randomPhrase = new ArrayList<>();
private ArrayList<Player> user = new ArrayList<>();
//private Letter[] letter_array = new Letter[16];

/**
* Constructor
*
* @param wheel Wheel
* @throws InterruptedException
*/
public WofFortuneGame(Wheel wheel) throws InterruptedException {
// get the wheel
this.wheel = wheel;

// do all the initialization for the game
setUpGame();
}

/**
* Plays the game
*
* @throws InterruptedException
*/
public void playGame() throws InterruptedException {
// while the puzzle isn't solved, keep going
while (!puzzleSolved) {
// let the current player play
playTurn(player1);
}
}

/**
* Sets up all necessary information to run the game
*/
private void setUpGame() {
//add phrases from addPhrase method
addPhrase();
// create a single player
int numPlayers = 0;

player1 = new Player("Player1");

// print out the rules
System.out.println("RULES!");
System.out.println("Each player gets to spin the wheel, to get a number value");
System.out.println("Each player then gets to guess a letter. If that letter is in the phrase, ");
System.out.println(" the player will get the amount from the wheel for each occurence of the letter");
System.out.println("If you have found a letter, you will also get a chance to guess at the phrase");
System.out.println("Each player only has three guesses, once you have used up your three guesses, ");
System.out.println("you can still guess letters, but no longer solve the puzzle.");
System.out.println();

//ask user if they would like to enter a phrase
Scanner input = new Scanner(System.in);
int choice = 0;
System.out.println("Would you like to enter your own phrase? 1 for yes 2 for no 3 to have the computer pick a random phrase");
choice = input.nextInt();
//if loop to see what user input is
if (choice == 1) {
System.out.println("Please type your phrase");
String playerPhrase = input.nextLine();
//for loop to store user phrase into userPhrase ArrayList
for (int i = 0; i < playerPhrase.length(); i++) {
userPhrase.add(new Letter(playerPhrase.charAt(i)));
}
}
//if they choose no, set the phrase to the default phrase
if (choice == 2) {
for (int i = 0; i < phrase.length(); i++) {
userPhrase.add(new Letter(phrase.charAt(i)));
}
}
if (choice == 3) {
addPhrase();
String generatedRandomPhrase = randomPhrase.get(0); // reading the randomPhrase stored in the randomPhrase list
for (int i = 0; i < generatedRandomPhrase.length(); i++) {
userPhrase.add(new Letter(generatedRandomPhrase.charAt(i)));
}
}
for (int i = 0; i < phrase.length(); i++) {
userPhrase.add(new Letter(phrase.charAt(i)));
}
}
//invalid inputs
else {
System.out.println("Invalid input");
}
// for each character in the phrase, create a letter and add to letters array
for (int i = 0; i < phrase.length(); i++) {
//letter_array[i] = new Letter(phrase.charAt(i));
userPhrase.add(new Letter(phrase.charAt(i)));
}
// setup done
}

/**
* Method that uses a random number generator to add a random phrase out of
* ten to be used by game when prompted by the user
*/
private void addPhrase() {
//RNG
Random random = new Random();
int randomChoice = random.nextInt(10)+1; // generates random numbers in the range 1-10(both inclusive)
//if loops to determine which phrase to add
if (randomChoice == 1) {
randomPhrase.add("This is a game");
}
if (randomChoice == 2) {
randomPhrase.add("This is a sentence");
}
if (randomChoice == 3) {
randomPhrase.add("What do I add");
}
if (randomChoice == 4) {
randomPhrase.add("I'm running out of sentences");
}
if (randomChoice == 5) {
randomPhrase.add("So here is some generic Java sentences");
}
if (randomChoice == 6) {
randomPhrase.add("Hello World");
}
if (randomChoice == 7) {
randomPhrase.add("Java is fun");
}
if (randomChoice == 8) {
randomPhrase.add("I can code now");
}
if (randomChoice == 9) {
randomPhrase.add("Learning Java is rewarding");
}
if (randomChoice == 10) {
randomPhrase.add("I'm completely out of sentences");
}
}

/**
* One player's turn in the game Spin wheel, pick a letter, choose to solve
* puzzle if letter found
*
* @param player
* @throws InterruptedException
*/
private void playTurn(Player player) throws InterruptedException {
int money = 0;
Scanner sc = new Scanner(System.in);

System.out.println(player.getName() + ", you have $" + player.getWinnings());
System.out.println("Spin the wheel! <press enter>");
sc.nextLine();
System.out.println("<SPINNING>");
Thread.sleep(200);
Wheel.WedgeType type = wheel.spin();
System.out.print("The wheel landed on: ");
switch (type) {
case MONEY:
money = wheel.getAmount();
System.out.println("$" + money);
break;

case LOSE_TURN:
System.out.println("LOSE A TURN");
System.out.println("So sorry, you lose a turn.");
return; // doesn't get to guess letter

case BANKRUPT:
System.out.println("BANKRUPT");
player.bankrupt();
return; // doesn't get to guess letter

default:

}
System.out.println("");
System.out.println("Here is the puzzle:");
showPuzzle();
System.out.println();
System.out.println(player.getName() + ", please guess a letter.");
//String guess = sc.next();

char letter = sc.next().charAt(0);
if (!Character.isAlphabetic(letter)) {
System.out.println("Sorry, but only alphabetic characters are allowed. You lose your turn.");
} else {
// search for letter to see if it is in
int numFound = 0;
for (Letter l : userPhrase) {
if ((l.getLetter() == letter) || (l.getLetter() == Character.toUpperCase(letter))) {
l.setFound();
numFound += 1;
}
}
if (numFound == 0) {
System.out.println("Sorry, but there are no " + letter + "'s.");
} else {
if (numFound == 1) {
System.out.println("Congrats! There is 1 letter " + letter + ":");
} else {
System.out.println("Congrats! There are " + numFound + " letter " + letter + "'s:");
}
System.out.println();
showPuzzle();
System.out.println();
player.incrementScore(numFound * money);
System.out.println("You earned $" + (numFound * money) + ", and you now have: $" + player.getWinnings());

System.out.println("Would you like to try to solve the puzzle? (Y/N)");
letter = sc.next().charAt(0);
System.out.println();
if ((letter == 'Y') || (letter == 'y')) {
solvePuzzleAttempt(player);
}
}

}

}

/**
* Logic for when user tries to solve the puzzle
*
* @param player
*/
private void solvePuzzleAttempt(Player player) {

if (player.getNumGuesses() >= 3) {
System.out.println("Sorry, but you have used up all your guesses.");
return;
}

player.incrementNumGuesses();
System.out.println("What is your solution?");
Scanner sc = new Scanner(System.in);
sc.useDelimiter(" ");
String guess = sc.next();
if (guess.compareToIgnoreCase(phrase) == 0) {
System.out.println("Congratulations! You guessed it!");
puzzleSolved = true;
// Round is over. Write message with final stats
// TODO
} else {
System.out.println("Sorry, but that is not correct.");
}
}

/**
* Display the puzzle on the console
*/
private void showPuzzle() {
System.out.print(" ");
for (Letter l : userPhrase) {
if (l.isSpace()) {
System.out.print(" ");
} else {
if (l.isFound()) {
System.out.print(Character.toUpperCase(l.getLetter()) + " ");
} else {
System.out.print(" _ ");
}
}
}
System.out.println();

}

/**
* For a new game reset player's number of guesses to 0
*/
public void reset() {
player1.reset();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote