Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have provided the code needed, please put all new code into the appropriate sections. Thanks! Will Rate In the real Wheel of Fortune game,

I have provided the code needed, please put all new code into the appropriate sections. Thanks! Will Rate

In the real Wheel of Fortune game, sometimes players win prizes instead of money (of course they only get the prize that came up on the wheel if they guess a letter that is in the puzzle). Add a set of possible prizes to the game. Add an ArrayList to the Player class to keep track of the prizes a Player has earned. Add a wedge to the wheel for a PRIZE. Youll need to add prize as a type in the same way that MONEY, LOSE_TURN and BANKRUPT are used. When a player lands on a PRIZE, youll need to randomly select one of the prizes that you added. When the players stop playing and wish to end the program, you should iterate through each player announcing how much money they have won and any prizes they have won. You will likely want to create a method to do this in the Player class. Also note that if a player lands on BANKRUPT they lose any prizes they have accumulated.

WoFortuneGame:

import java.util.Scanner; import java.util.ArrayList; import java.util.Random; import java.util.InputMismatchException;

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

private boolean puzzleSolved = false;

private Wheel wheel; private Player players; private String phrase = "Once upon a time"; // private Letter[] letter_array = new Letter[16]; private static ArrayListLetters=new ArrayList<>();//changed from array to array list private ArrayList Phrases = new ArrayList<>(); // Arraylist that holds string objects named Phrases private ArrayList Players = new ArrayList<>(); // Arraylist that holds player objects named players

/** * 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 int counter=0;//variable to change turns while (!puzzleSolved){ int guess=0; for(Player turn:Players){ Players.get(guess); guess++; counter+=1; playTurn(turn); if(puzzleSolved){ break;//ends when phrase is guessed } } // let the current player play // playTurn(player1); } } /** * Sets up all necessary information to run the game */ private void setUpGame() { // create a single player // player1 = new Player("Player1"); addPhrase(); // Calling method to add more phrases to the game3 System.out.println("How many players?");//prompting number of players try{ Scanner playerCount = new Scanner(System.in); int player = playerCount.nextInt(); System.out.print(+player); System.out.println("Please enter players names");//prompting for names for(int i =0; i Scanner playerName= new Scanner(System.in); String name =playerName.nextLine(); players=new Player(name); Players.add(players); } }catch(InputMismatchException e){ System.out.println("Enter correct input! " + e); // Error message System.out.println("The program will now close, as correct input must be entered"); System.exit(0); //program exits } // 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(); System.out.println("Would you like to enter your own phrase?(y/n)"); try { Scanner customPhrase = new Scanner(System.in); // Scanner char letter = customPhrase.next().charAt(0); if ((letter == 'Y') || (letter == 'y')) { System.out.println("Please enter your phrase!"); Scanner typePhrase = new Scanner(System.in); // Scanner String custom = typePhrase.nextLine(); phrase = custom; System.out.println("Now let's play!"); } else { Random randomPhrase = new Random(); phrase = Phrases.get(randomPhrase.nextInt(Phrases.size())); } // create a letter and add to letters arraylist for (int i = 0; i < phrase.length(); i++) { Letters.add(new Letter(phrase.charAt(i))); // stores in array list } } catch (Exception a) { System.out.println("Incorrect input" + a); System.out.println("Program will now exit"); System.exit(0); } } /** * Method to add a number of phrases into the game. */ private void addPhrase() { String phrase1 = "I am Bruce Wayne"; Phrases.add(phrase1); String phrase2 = "Always on Time"; Phrases.add(phrase2); String phrase3 = "Shoot for the Stars"; Phrases.add(phrase3); String phrase4 = "Chow Down"; Phrases.add(phrase4); String phrase5 = "Hair of the Dog"; Phrases.add(phrase5); String phrase6 = "Merry Christmas"; Phrases.add(phrase6); String phrase7 = "Happy Holidays"; Phrases.add(phrase7); String phrase8 = "Off the Record"; Phrases.add(phrase8); String phrase9 = "A Sight for Sore Eyes"; Phrases.add(phrase9); String phrase10 = "Mans Best Friend"; Phrases.add(phrase10); String phrase11 = "An Apple a Day Keeps the Doctor Away"; Phrases.add(phrase11); String phrase12 = "Cool as a cucumber"; Phrases.add(phrase12); }

/** * 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); try{ System.out.println(player.getName() + ", you have $" + player.getWinnings()); System.out.println("Spin the wheel! "); sc.nextLine(); System.out.println(""); 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 : Letters) { 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); } } } }catch(InterruptedException e){ System.out.println("Error During Game"+e); } } /** * 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("\t\t"); for (Letter l : Letters) { 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() { players.reset(); } }

Wedge:

public class Wedge { private Wheel.WedgeType type; private int amount = 0;

/** * Constructor * @param type Wheel.WedgeType */ public Wedge(Wheel.WedgeType type) { this.type = type; if (type == Wheel.WedgeType.MONEY) { amount = (int)(Math.random()*20 + 1)*25; } }

/** * Getter * @return Wheel.WedgeType */ public Wheel.WedgeType getType() { return type; }

/** * Getter * @return int amount (only for wedges of Wheel.WedgeType.MONEY) */ public int getAmount() { return amount; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions