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;
}
}
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;
}
}
Get Answers For Free
Most questions answered within 1 hours.