Question: Note, The Java code pasted below is 5 seperate classes in the same package, and what all I have done so far. This is the
Note, The Java code pasted below is 5 seperate classes in the same package, and what all I have done so far. This is the last part and I am stuck.
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.
Possible Bonus Work:
-Let each player continue their turn if they guessed a letter correctly
-Allow users to add or remove players before each new round
-Keep track of prizes and money from the total game separately from the prizes and money in each round, so that bankrupting only removes money and prizes in the current round. Also report total game earnings and prizes before the users quit the game.
----------code 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.*;
/** * WofFortuneGame class Contains all logistics to run the game * * @author Matthew Welch */ public class WofFortuneGame {
private boolean puzzleSolved = false; private Wheel wheel; private Player player1; private String phrase = "Once upon a time"; private Random r; private String playerNames; private ArrayList
/** * 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 { //variable to store who's turn it is int playerTurn = 0; // while the puzzle isn't solved, keep going while (!puzzleSolved) { // let the current player play playTurn(user.get(playerTurn)); if (playerTurn == user.size() - 1) { playerTurn = 0; } else { playerTurn++; } } }
/** * Sets up all necessary information to run the game */ private void setUpGame() { //set up Scanner object Scanner input = new Scanner(System.in); //add phrases from addPhrase method addPhrase(); // variable for number of players int numPlayers = 0; //ask for how many players try { System.out.println("How many players?"); numPlayers = input.nextInt(); } catch (InputMismatchException e) { System.out.println("Error: Did not enter a number"); } //ask for thier names for (int i = 0; i < numPlayers; i++) { try { System.out.println("What is player " + (i + 1) + "'s name?"); playerNames = input.nextLine(); } catch (Exception e) { System.out.println("Invalid input"); } user.add(new Player(playerNames)); } 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 int choice = 0; //while loop for user choice while (choice == 1 || choice == 2) { try { System.out.println("Would you like to enter your own phrase? 1 for yes 2 for no."); choice = input.nextInt(); } catch (Exception e) { System.out.println("Invalid Input"); } //if loop to see what user input is if (choice == 1) { try { 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))); } } catch (Exception e) { System.out.println("Invalid Input"); } } //to add a random phrase and store it in userPhrase for the game if (choice == 2) { phrase = randomPhrase.get(r.nextInt(randomPhrase.size())); } //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 */ public ArrayList addPhrase() { randomPhrase.add("This is a game"); randomPhrase.add("This is a sentence"); randomPhrase.add("What do I add"); randomPhrase.add("I'm running out of sentences"); randomPhrase.add("So here is some generic Java sentences"); randomPhrase.add("Hello World"); randomPhrase.add("Java is fun"); randomPhrase.add("I can code now"); randomPhrase.add("Learning Java is rewarding"); randomPhrase.add("I'm completely out of sentences"); return randomPhrase; }
/** * 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; Wheel.WedgeType type = null; Scanner sc = new Scanner(System.in);
System.out.println(player.getName() + ", you have $" + player.getWinnings()); try { System.out.println("Spin the wheel!
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(" "); try { String guess = sc.next(); if (guess.compareToIgnoreCase(phrase) == 0) { System.out.println("Congratulations! You guessed it!"); puzzleSolved = true; //print out all the players stats for (Player p : user) { p.playerStats(); } // Round is over. Write message with final stats // TODO } else { System.out.println("Sorry, but that is not correct."); } } catch (Exception e) { System.out.println("Invalid input"); } }
/** * Display the puzzle on the console */ private void showPuzzle() { System.out.print("\t\t"); 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(); } }
----------Another class------
/* * 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;
/** * Class that defines a player for a game with monetary winnings and a limited * number of choices * * @author clatulip */ public class Player {
private int winnings = 0; // amount of money won private String name; // player's name private int numGuesses = 0; // number of times they've tried to solve puzzle private ArrayList
/** * Constructor * * @param name String that is the player's name */ public Player(String name) { this.name = name; }
/** * Getter * * @return int amount of money won so far */ public int getWinnings() { return winnings; }
/** * Adds amount to the player's winnings * * @param amount int money to add */ public void incrementScore(int amount) { if (amount < 0) { return; } this.winnings += amount; }
/** * Getter * * @return String player's name */ public String getName() { return name; }
/** * Getter * * @return int the number of guesses used up */ public int getNumGuesses() { return numGuesses; }
/** * Add 1 to the number of guesses used up */ public void incrementNumGuesses() { this.numGuesses++; }
/** * Resets for a new game (only number of guesses) This does not reset the * winnings. */ public void reset() { this.numGuesses = 0; }
public void bankrupt() { this.winnings = 0; }
/** * Method used to print out a player's stats */ public void playerStats() { System.out.println(this.name + " won $" + this.winnings); }
/** * * @param prize */ public void addPrize(String prize) { this.prizesEarned.add(prize); }
}
--------Another Class-----
/* * 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.*;
/** * This class defines one wedge of a wheel for the wheel of fortune game * * @author clatulip */ public class Wedge {
private Wheel.WedgeType type; private int amount = 0; private ArrayList
/** * 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; } if(type == Wheel.WedgeType.PRIZE){ prize = prizes(r.nextInt(prizes.length())); } }
/** * 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; }
/** * * @return */ public ArrayList prizes() { prizes.add("All Inclusive Trip to the Bahamas"); prizes.add("A brand new car"); prizes.add("Cruise on Royal Carribian"); return prizes; }
}
---------Another Class------
package woffortune;
import java.util.ArrayList;
/** * This class defines a Wheel for the Wheel of Fortune game * * @author clatulip */ public class Wheel {
// enumerated type, wheel wedges can be any of these public enum WedgeType { MONEY, BANKRUPT, LOSE_TURN, PRIZE } // the type for the current sping private WedgeType spinType; // if a money wedge, the amount private int spinDollarAmount; // list of wedges private ArrayList
/** * Constructor Creates the wheel */ public Wheel() { // put a bankrupt wedge on the wheel wedges.add(new Wedge(WedgeType.BANKRUPT));
// put a lose-turn wedge on the wheel wedges.add(new Wedge(WedgeType.LOSE_TURN));
// put 20 money wedges on the wheel for (int i = 1; i < 20; i++) { wedges.add(new Wedge(WedgeType.MONEY)); } //put in a prize wedge wedges.add(new Wedge(WedgeType.PRIZE)); }
/** * Spins the wheel * * @return WedgeType */ public WedgeType spin() { int index = (int) (Math.random() * wedges.size()); spinType = wedges.get(index).getType(); spinDollarAmount = wedges.get(index).getAmount(); return spinType; }
/** * Getter For when the spin lands on a money wedge * * @return int the amount of money on the wedge */ public int getAmount() { return spinDollarAmount; }
}
-------Another Class------
/* * 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;
/** * Execution Harness for Wheel of Fortune Game Creates wheel, creates game * instance, loops through rounds * * @author clatulip */ public class WofFortune {
public static void main(String[] args) throws InterruptedException { int round = 1; boolean keepPlaying = true; Wheel wheel = new Wheel(); Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Wheel of Fortune!"); WofFortuneGame game = new WofFortuneGame(wheel);
while (keepPlaying) { System.out.println("***********************"); System.out.println(" ROUND " + round); System.out.println("***********************"); System.out.println(); game.playGame();
// game played and ended, see if they want to play another round System.out.println(); System.out.println("Would you like to play again?"); try { char ans = sc.next().charAt(0); if ((ans == 'y') || (ans == 'Y')) { // play again round += 1; game.reset(); } else { // don't play again keepPlaying = false; } } catch (Exception e) { System.out.println("Invalid input"); }
}
System.out.println("Thank-you for playing the Wheel of Fortune!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
