Question

I need to make some modifications to the code below. Numbers 1 through 3 are the...

I need to make some modifications to the code below. Numbers 1 through 3 are the required modifications.
1. Write a static method to have the players guess what was rolled
2. If guessed what was rolled you get 10 points
3. You win if you reach 30 points before the end of the round otherwise the person with the points wins at the end of five rounds
The code is below
import java.util.Scanner;
public class ChoHan
{
public static void main(String[] args)
{
final int MAX_ROUNDS = 5; // Number of rounds
String player1Name; // First player's name
String player2Name; // Second player's name

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the player's names.
System.out.print("Enter the first player's name: ");
player1Name = keyboard.nextLine();
System.out.print("Enter the second player's name: ");
player2Name = keyboard.nextLine();

// Create the dealer.
Dealer dealer = new Dealer();

// Create the two players.
Player player1 = new Player(player1Name);
Player player2 = new Player(player2Name);

// Play the rounds.
for (int round = 0; round < MAX_ROUNDS; round++)
{
System.out.println("----------------------------");
System.out.printf("Now playing round %d.\n", round + 1);

// Roll the dice.
dealer.rollDice();

// The players make their guesses.
player1.makeGuess();
player2.makeGuess();

// Determine the winner of this round.
roundResults(dealer, player1, player2);
}

// Display the grand winner.
displayGrandWinner(player1, player2);
}

/**
* The roundResults method determines the results of
* the current round. The parameters are:
* dealer: The Dealer object
* player1: Player #1 object
* player2: Player #2 object
*/

public static void roundResults(Dealer dealer, Player player1,
Player player2)
{
// Show the dice values.
System.out.printf("The dealer rolled %d and %d.\n",
dealer.getDie1Value(), dealer.getDie2Value());
System.out.printf("Result: %s\n", dealer.getChoOrHan());

// Check each player's guess and award points.
checkGuess(player1, dealer);
checkGuess(player2, dealer);
}

/**
* The checkGuess method checks a player's guess against
* the dealer's result. The parameters are:
* player: The Player object to check.
* dealer: The Dealer object.
*/

public static void checkGuess(Player player, Dealer dealer)
{
final int POINTS_TO_ADD = 1; // Points to award winner
String guess = player.getGuess(); // Player's guess
String choHanResult = dealer.getChoOrHan(); // Cho or Han

// Display the player's guess.
System.out.printf("The player %s guessed %s.\n",
player.getName(), player.getGuess());

// Award points if the player guessed correctly.
if (guess.equalsIgnoreCase(choHanResult))
{
player.addPoints(POINTS_TO_ADD);
System.out.printf("Awarding %d point(s) to %s.\n",
POINTS_TO_ADD, player.getName());
}
}
/**
* The displayGrandWinner method displays the game's grand winner.
* The parameters are:
* player1: Player #1
* player2: Player #2
*/

public static void displayGrandWinner(Player player1, Player player2)
{
System.out.println("----------------------------");
System.out.println("Game over. Here are the results:");
System.out.printf("%s: %d points.\n", player1.getName(),
player1.getPoints());
System.out.printf("%s: %d points.\n", player2.getName(),
player2.getPoints());

if (player1.getPoints() > player2.getPoints())
System.out.println(player1.getName() + " is the grand winner!");
else if (player2.getPoints() > player1.getPoints())
System.out.println(player2.getName() + " is the grand winner!");
else
System.out.println("Both players are tied!");
}
}
public class Dealer
{
private int die1Value; // The value of die #1
private int die2Value; // The value of die #2

/**
* Constructor
*/

public Dealer()
{
die1Value = 0;
die2Value = 0;
}

/**
* The rollDice method rolls the dice and saves
* their values.
*/

public void rollDice()
{
final int SIDES = 6; // Number of sides for the dice

// Create the two dice. (This also rolls them.)
Die die1 = new Die(SIDES);
Die die2 = new Die(SIDES);

// Record their values.
die1Value = die1.getValue();
die2Value = die2.getValue();
}

/**
* The getChoOrHan method returns the result of the dice
* roll. If the sum of the dice is even, the method returns
* "Cho (even)". Othewise, it returns "Han (odd)".
*/

public String getChoOrHan()
{
String result; // To hold the result

// Get the sum of the dice.
int sum = die1Value + die2Value;

// Determine even or odd.
if (sum % 2 == 0)
result = "Cho (even)";
else
result = "Han (odd)";

// Return the result.
return result;
}

/**
* The getDie1Value method returns the value of
* die #1.
*/

public int getDie1Value()
{
return die1Value;
}
/**
* The getDie2Value method returns the value of
* die #2.
*/

public int getDie2Value()
{
return die2Value;
}
}
import java.util.Random;
/**
* The Die class simulates a six-sided die.
*/
public class Die
{
private int sides; // Number of sides
private int value; // The die's value

/**
* The constructor performs an initial
* roll of the die. The number of sides
* for the die is passed as an argument.
*/

public Die(int numSides)
{
sides = numSides;
roll();
}

/**
* The roll method simlates the rolling of
* the die.
*/

public void roll()
{
// Create a Random object.
Random rand = new Random();

// Get a random value for the die.
value = rand.nextInt(sides) + 1;
}

/**
* The getSides method returns the
* number of sides for the die.
*/

public int getSides()
{
return sides;
}

/**
* The getValue method returns the
* value of the die.
*/

public int getValue()
{
return value;
}
}
import java.util.Random;
/**
* Player class for the game of Cho-Han
*/

public class Player
{
private String name; // The player's name
private String guess; // The player's guess
private int points; // The player's points

/**
* Constructor
* Accepts the player's name as an argument
*/

public Player(String playerName)
{
name = playerName;
guess = "";
points = 0;
}

/**
* The makeGuess method causes the player to guess
* either "Cho (even)" or "Han (odd)".
*/

public void makeGuess()
{
// Create a Random object.
Random rand = new Random();

// Get a random number, either 0 or 1.
int guessNumber = rand.nextInt(2);

// Convert the random number to a guess of
// either "Cho (even)" or "Han (odd)".
if (guessNumber == 0)
guess = "Cho (even)";
else
guess = "Han (odd)";
}

/**
* The addPoints method adds a specified number of
* points to the player's current balance. The number
* of points is passed as an argument.
*/

public void addPoints(int newPoints)
{
points += newPoints;
}

/**
The getName method returns the player's name.
*/

public String getName()
{
return name;
}
/**
The getGuess method returns the player's guess.
*/

public String getGuess()
{
return guess;
}
/**
The getPoints method returns the player's points
*/

public int getPoints()
{
return points;
}
}

Homework Answers

Answer #1

Given required modifications are incorporated into below Java Code. Please comment in case more functionalities to add.

1. Added playersGuess static method to have the players guess what was rolled instead of random function guess makeGuess method present in Player.

2. Refactored checkGuess method to update POINTS_TO_ADD = 10

3. Added Check 30 points before the end of the all round in main method round loop to break.

Java complete code:

import java.util.Scanner;

/**
* I need to make some modifications to the code below. Numbers 1 through 3 are
* the required modifications.
*
* 1. Write a static method to have the players guess what was rolled
* 2. If guessed what was rolled you get 10 points
* 3. You win if you reach 30 points before the end of the round otherwise
* the person with the points wins at the end of five rounds
*/
public class ChoHan {
   public static void main(String[] args) {
       final int MAX_ROUNDS = 5; // Number of rounds
       String player1Name; // First player's name
       String player2Name; // Second player's name
       // Create a Scanner object for keyboard input.
       try (Scanner keyboard = new Scanner(System.in)) {
           // Get the player's names.
           System.out.print("Enter the first player's name: ");
           player1Name = keyboard.nextLine();
           System.out.print("Enter the second player's name: ");
           player2Name = keyboard.nextLine();
           // Create the dealer.
           Dealer dealer = new Dealer();
           // Create the two players.
           Player player1 = new Player(player1Name);
           Player player2 = new Player(player2Name);
           // Play the rounds.
           for (int round = 0; round < MAX_ROUNDS; round++) {
               System.out.println("----------------------------");
               System.out.printf("Now playing round %d.\n", round + 1);
               // Roll the dice.
               dealer.rollDice();
               // The players guess what was rolled
               playersGuess(keyboard,player1, player2);

               // Determine the winner of this round.
               roundResults(dealer, player1, player2);
               // Check 30 points before the end of the all round
               if (player1.getPoints() >= 30 || player2.getPoints() >= 30)
                   break;

           }
           // Display the grand winner.
           displayGrandWinner(player1, player2);
       }
   }

   /**
   * Method to have the players guess what was rolled
   * @param player1
   * @param player2
   */
   public static void playersGuess(Scanner keyboard, Player player1, Player player2) {
       // The players make their guesses.
       System.out.println("Player " + player1.getName() + " turn to make guess.\n[ 2 to 12 ] ");
       player1.makePlayerGuess(keyboard.nextInt());
       System.out.println("Player " + player2.getName() + " turn to make guess.\n[ 2 to 12 ] ");
       player2.makePlayerGuess(keyboard.nextInt());
   }

   /**
   * The roundResults method determines the results of the current round. The
   * parameters are: dealer: The Dealer object player1: Player #1 object
   * player2: Player #2 object
   */
   public static void roundResults(Dealer dealer, Player player1, Player player2) {
       // Show the dice values.
       System.out.printf("The dealer rolled %d and %d.\n", dealer.getDie1Value(), dealer.getDie2Value());
       System.out.printf("Result: %s\n", dealer.getChoOrHan());
       // Check each player's guess and award points.
       checkGuess(player1, dealer);
       checkGuess(player2, dealer);
   }

   /**
   * The checkGuess method checks a player's guess against the dealer's
   * result. The parameters are: player: The Player object to check. dealer:
   * The Dealer object.
   */
   public static void checkGuess(Player player, Dealer dealer) {
       int POINTS_TO_ADD = 1; // Points to award winner
       if(player.getGuessNumber()==dealer.getTwoDiesSum()){
           POINTS_TO_ADD=10;
       }

       String guess = player.getGuess(); // Player's guess
       String choHanResult = dealer.getChoOrHan(); // Cho or Han
       // Display the player's guess.
       System.out.printf("The player %s guessed %s.\n", player.getName(), player.getGuess());
       // Award points if the player guessed correctly.
       if (guess.equalsIgnoreCase(choHanResult)) {
           player.addPoints(POINTS_TO_ADD);
           System.out.printf("Awarding %d point(s) to %s.\n", POINTS_TO_ADD, player.getName());
       }
   }

   /**
   * The displayGrandWinner method displays the game's grand winner. The
   * parameters are: player1: Player #1 player2: Player #2
   */
   public static void displayGrandWinner(Player player1, Player player2) {
       System.out.println("----------------------------");
       System.out.println("Game over. Here are the results:");
       System.out.printf("%s: %d points.\n", player1.getName(), player1.getPoints());
       System.out.printf("%s: %d points.\n", player2.getName(), player2.getPoints());
       if (player1.getPoints() > player2.getPoints())
           System.out.println(player1.getName() + " is the grand winner!");
       else if (player2.getPoints() > player1.getPoints())
           System.out.println(player2.getName() + " is the grand winner!");
       else
           System.out.println("Both players are tied!");
   }
}

public class Dealer {
   private int die1Value; // The value of die #1
   private int die2Value; // The value of die #2

   /**
   * Constructor
   */
   public Dealer() {
       die1Value = 0;
       die2Value = 0;
   }

   /**
   * The rollDice method rolls the dice and saves their values.
   */
   public void rollDice() {
       final int SIDES = 6; // Number of sides for the dice
       // Create the two dice. (This also rolls them.)
       Die die1 = new Die(SIDES);
       Die die2 = new Die(SIDES);
       // Record their values.
       die1Value = die1.getValue();
       die2Value = die2.getValue();
   }

   /**
   * The getChoOrHan method returns the result of the dice roll. If the sum of
   * the dice is even, the method returns "Cho (even)". Othewise, it returns
   * "Han (odd)".
   */
   public String getChoOrHan() {
       String result; // To hold the result
       // Get the sum of the dice.
       int sum = die1Value + die2Value;
       // Determine even or odd.
       if (sum % 2 == 0)
           result = "Cho (even)";
       else
           result = "Han (odd)";
       // Return the result.
       return result;
   }
  
   public int getTwoDiesSum(){
       return die1Value + die2Value;
   }

   /**
   * The getDie1Value method returns the value of die #1.
   */
   public int getDie1Value() {
       return die1Value;
   }

   /**
   * The getDie2Value method returns the value of die #2.
   */
   public int getDie2Value() {
       return die2Value;
   }
}

import java.util.Random;

/**
* The Die class simulates a six-sided die.
*/
public class Die {
   private int sides; // Number of sides
   private int value; // The die's value

   /**
   * The constructor performs an initial roll of the die. The number of sides
   * for the die is passed as an argument.
   */
   public Die(int numSides) {
       sides = numSides;
       roll();
   }

   /**
   * The roll method simlates the rolling of the die.
   */
   public void roll() {
       // Create a Random object.
       Random rand = new Random();
       // Get a random value for the die.
       value = rand.nextInt(sides) + 1;
   }

   /**
   * The getSides method returns the number of sides for the die.
   */
   public int getSides() {
       return sides;
   }

   /**
   * The getValue method returns the value of the die.
   */
   public int getValue() {
       return value;
   }
}

/**
* Player class for the game of Cho-Han
*/
public class Player {
   private String name; // The player's name
   private String guess; // The player's guess
   private int points; // The player's points
   private int guessNumber; // The player's guessNumber

   /**
   * Constructor Accepts the player's name as an argument
   */
   public Player(String playerName) {
       name = playerName;
       guess = "";
       points = 0;
   }

   /**
   * The makeGuess method causes the player to guess either "Cho (even)" or
   * "Han (odd)".
   */
   private void makeGuess(int guessNumber) {
       if (guessNumber % 2 == 0)
           guess = "Cho (even)";
       else
           guess = "Han (odd)";
   }
  
   /**
   * The makePlayerGuess method causes the player to guess between 2 to 12
   */
   public void makePlayerGuess(int guessNumber) {
       //Set player guess either "Cho (even)" or "Han (odd)"
       makeGuess(guessNumber);
       this.guessNumber=guessNumber;
   }

   /**
   * The addPoints method adds a specified number of points to the player's
   * current balance. The number of points is passed as an argument.
   */
   public void addPoints(int newPoints) {
       points += newPoints;
   }

   /**
   * The getName method returns the player's name.
   */
   public String getName() {
       return name;
   }

   /**
   * The getGuess method returns the player's guess.
   */
   public String getGuess() {
       return guess;
   }

   /**
   * The getPoints method returns the player's points
   */
   public int getPoints() {
       return points;
   }

   /**
   * @return the guessNumber
   */
   public int getGuessNumber() {
       return guessNumber;
   }
  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Determine how all of your classes are related, and create a complete UML class diagram representing...
Determine how all of your classes are related, and create a complete UML class diagram representing your class structure. Don't forget to include the appropriate relationships between the classes. GameDriver import java.util.Scanner; public class GameDriver { Scanner in = new Scanner(System.in); public static void main(String[] args) { Move move1 = new Move("Vine Whip", "Grass", 45, 1.0f); Move move2 = new Move("Tackle", "Normal", 50, 1.0f); Move move3 = new Move("Take Down", "Normal", 90, 0.85f); Move move4 = new Move("Razor Leaf", "Grass",...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Currently stuck on this part of a project. I am struggling with creating the String for...
Currently stuck on this part of a project. I am struggling with creating the String for creating the dice values in the boxes for the "showDice" string. I have already completed the first part which was creating the class for the Die. This is Javascript. For this part, we will create the class that allows you to roll and score the dice used in a game of Yahtzee. This will be the longest part of the project, so I suggest...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...
For this assignment, we will be creating at least two classes. The first will be the...
For this assignment, we will be creating at least two classes. The first will be the Die class. The second will be the DiceTower class. The specifications for the classes are below. You will also be expected to create and deliver a test plan that demonstrates how you will prove your classes fulfill requirements and are bug-free. Use expected naming conventions and commenting. Part I: Die: Die(int sides): creates a die with sides quantity of sides with values from 1...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...