Question

Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two...

Requirements:

1. You are not allowed to use global variables.
2. Must declare the following two constants
public static final int POINTS = 30;
public static final int FORFEIT_POINTS = 20;
3. You can implement your own solution but you are required to break down the problem into smaller pieces.
4. You must provide the output for two rounds of game minimum.
5. Since we are using the Random number generator your output will not be exactly like mine. However, all the prompts must be exactly as the provided output.

Problem: Write a program to play the pig game against the computer. At each turn, the current player will roll a pair of dice and accumulate points. The goal is to reach 100 or more points before your opponent does. (For testing purposes use 30 instead of 100 points) If, on any turn, the player rolls a 1, all the points accumulated for that round are forfeited and the control of the dice moves to the other player. If the player rolls two 1s in one turn, the player loses all the points accumulated thus far and the control moves to the other player. The player may voluntarily turn over the control of the dice after each roll. Therefore, player must decide to roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. The computer flips a coin to choose the first player. Note that there are two constants in this program, POINTS and FORFEIT_POINTS. The POINTS constant is the number of points necessary for winning the game, for testing purposes we will use 30 but it could be set to any value. The FORFEIT_POINTS constant is the number of points that the computer player needs to reach before forfeiting their turn and passing control back to the human player. After the computer player has a score greater than or equal to FORFEIT_POINTS then it will only roll one time each subsequent turn. If the computer has less than FORFEIT_POINTS then it will continue to roll until it achieves a score greater than or equal to FORFEIT_POINTS.

Here is the list of the tasks that needs to be done

1. Describe the game by writing a method.
2. Data validation: A method that accepts a Scanner object as its parameter, prompt the user to enter “yes” or “no”. as long as the user is not entering a valid input , prompt the user again
3. Flip the coin: This method accepts a Random object and returns” head” or “tail” based on the random number that was generated
4. Roll two dices: this method accepts a Random object. Generates two random number representing one of the numbers on a dice. Returns the sum of the die.
5. Choose a name for the computer: Come up with 10 different name for the computer. Then select a random name from the list that you created. Return the selected name.
6. Play: this method calls the other methods to play the game
     a. Declare all the needed variables to keep track of the scores for each player, and Boolean variables to indicate who is playing at the moment.
     b. Ask the user’s name
     c. Decide who start the game first by calling one of the methods you created to flip the coin.
     d. Write conditional statements to switch the game between the computer and the player based on the dice rolled and overall points. Read the output and the program description to figure out the conditions. You need to use couple while loops: one loop for the human player, one loop for the computer player. The previous two loops will be nested in another while loop to witch the game between the two players.
//declaring your variables
While (there is no winner)
{
     //some codes
     While (human is playing)
     {
           //some codes, conditional statements
     }
     //you may need some codes
     While (computer is playing)
     {
          //some codes, conditional statements
     }
     //you may need some codes
     }
     e. Keep playing the game until the player or the computer has 100 or more points (30 or more for the simplicity). You are not allowed to hard code any numbers.
7. Main method: Calls the method play, keep playing the game as long as there are more players.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class PigGame
{
public static void main(String[] args)
{
int turnScore = 0;
int totalScore = 0;
int turnScore2 = 0;
int totalScore2 = 0;
final int WIN = 30;
int dice = 0;
int dice2 = 0;
String input = "r";
String input2 = "r";
char repeat;

Scanner keyboard = new Scanner(System.in);
Scanner s = new Scanner (System.in);

Random randomNumbers = new Random();

while(totalScore < WIN && totalScore2 < WIN)
{
//Player 1's turn

do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.println("You rolled: " + dice);

if(dice == 1)
{
turnScore = 0;
System.out.println("Turn over.");
System.out.println("Player 1 total is " + totalScore);
break;
}
else
{
turnScore += dice;
System.out.print("Player 1 turn total is " + turnScore + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);


if(repeat == 's')
{
System.out.println("Turn over.");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;

}
}
}
while(input.equalsIgnoreCase("r") || dice != 1);
{

totalScore += turnScore;
}

if(totalScore >= WIN)
{
System.out.println("Your total Score is " + totalScore);
System.out.println("Player 1 wins!");

}


//player2's turn
System.out.println();
System.out.println("It is Player 2's turn.");

{ do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("Player 2 rolled: " + dice2);

if(dice2 == 1)
{
turnScore2 = 0;
System.out.print("Turn over");
System.out.println("Player 2 total is " + totalScore2);
break;
}
else
{
turnScore2 += dice2;
System.out.print("Player 2 total is " +turnScore2 + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);


if(repeat == 's')
{
System.out.println("Turn over");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}  
while(input2.equalsIgnoreCase("r") && dice != 1); {

totalScore2 += turnScore2;

}
}
if(totalScore2 >= WIN);
{
System.out.println("Player 2 score is " + totalScore2 + "\n");
System.out.println("Player 2 wins");
break;
}
}


}
}

Add a comment
Know the answer?
Add Answer to:
Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • can someone help me fix my jeopardy dice game I am having a hard time figuring...

    can someone help me fix my jeopardy dice game I am having a hard time figuring out what i am doing wrong #include #include using namespace std; //this function makes a number between 1 and 6 come out just like a dice int rollDie() { return (rand() % 6+1); } //this function asks the user with a printed statement if they want to roll the dice and gives instructions for yes and no (y/n) void askYoNs(){ cout<<"Do you want to...

  • #2 New Turn Rules A frustrating aspect of programming on a project with other programmers is the ...

    #2 New Turn Rules A frustrating aspect of programming on a project with other programmers is the fact that everyone solves problems in different ways. Just when you think you know which way things are headed, someone throws a snag in your design. Objects needed: Die Attributes Current value of the die Actions roll the die DiceSet Attributes An array of six Die The number of dice available to roll. When this number reaches 0, it is reset to 6....

  • INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first....

    INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...

  • Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

    Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user...

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT