Question

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, the player is faced with two decisions:

roll - If the player rolls a

1: the player scores nothing and it becomes the opponent's turn.

2 - 6: the number is added to the player's turn total and the player's turn continues.

hold - The turn total is added to the player's score and it becomes the opponent's turn.

You will be playing against the computer.

Requirements

An algorithm that you outlined – either in Word or in PowerPoint

A menu that explains how the game works.

A main method that allows the use to continue playing or quit.

Contain documentation for users and programmers

Directions on how to play

The use of an array.

The use of an if (else) statement

The use of a while loop

The use of a do while loop

The use of a for loop

Incorporate one class (it can be one we have used before)

Contain a minimum of 5 methods including at least 1 value return method (main should be very short and incorporate these methods)

Program must compile and run

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

//Java program to simulate a game of Pig

import java.util.Random;

import java.util.Scanner;

public class Pig {

      

       static Scanner scan = new Scanner(System.in);

       // function to return the value on die

       public static int rollDie(Random rand)

       {

            

             return rand.nextInt(6)+1;

       }

      

       // function to determine which player starts the game and determine which player rolls the die next when computer rolls the die

       public static int determineTurn(Random rand)

       {     

             return rand.nextInt(2);

       }

      

       // method to get the user choice to roll again or hold

       public static int getUserChoice()

       {

             int choice;

             do {

                    System.out.println("Press 1 to roll again and 0 to start computer's turn : ");

                    choice = scan.nextInt();

             }while(choice != 0 && choice != 1);

             scan.nextLine();

             return choice;

       }

      

       // method to play one turn

       public static int playOneTurn(int turnChoice, Random rand, int score)

       {

             int die, turnScore=0, choice;

             // its computer's turn

            

             if(turnChoice == 0)

                    System.out.println("\nComputer's turn (current points: "+score+" )" );

             else

                    System.out.println("\nYour turn (current points: "+score+" )" );

            

             do {

                    die = rollDie(rand);

                   

                    if(die == 1)

                    {

                           turnScore = 0;

                           displayTurn(die,turnScore,turnChoice);

                           break;

                    }

                    else {

                           turnScore += die;

                           displayTurn(die,turnScore,turnChoice);

                    }

                    if(turnChoice == 1)

                    {

                           choice = getUserChoice();

                    }else

                           choice = determineTurn(rand);

            

             }while(turnChoice == choice);

            

            

             return (turnScore+score);

       }

      

       // method to display a turn

       public static void displayTurn(int die, int turnScore, int turn)

       {

             if(turn == 0)

                    System.out.println("Computer rolled "+die+" and turn score = "+turnScore);

             else

                    System.out.println("You rolled "+die+" and turn score = "+turnScore);

       }

            

       public static void main(String[] args) {

            

             int scores[] = new int[2];

            

             int turnChoice; // variable storing which player next rolls the die

             Random rand = new Random(); // random generator for die1 and determineTurn

             String choice;

            

             do

             {

                    for(int i=0;i<scores.length;i++)

                           scores[i] = 0;

                    turnChoice = determineTurn(rand); // initially determine who will roll the die  

                    while(scores[0]<100 && scores[1] <100)

                    {

                          

                           if(turnChoice == 0)

                           {

                                 scores[0] = playOneTurn(turnChoice,rand,scores[0]);

                                 System.out.println("Computer's score = "+scores[0]);

                                 turnChoice = 1;

                           }else

                           {

                                 scores[1] = playOneTurn(turnChoice,rand,scores[1]);

                                 System.out.println("Your score = "+scores[1]);

                                 turnChoice = 0;

                           }

                          

                    }

                   

                    if(scores[0] == 100)

                           System.out.println("Computer Won");

                    else

                           System.out.println("You Won");

                    System.out.println("\nDo you want to play again ? (y/n) ");

                    choice = scan.nextLine();

             }while(choice.equalsIgnoreCase("y"));

            

       }

}

//end of program

Output:

Your turn (current points: 0) You rolled 1 and turn score0 Your score0 Computers turn (current points: 0 Computer rolled 4 a

Your turn (current points: 72 You rolled 5 and turn score- 5 Press 1 to roll again and 0 to start computers turn You rolled

Add a comment
Know the answer?
Add Answer to:
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...
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 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...

  • 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...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a...

    java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a randomly-chosen player. On their turn a player rolls a single 6-sided die 1 or more times. After each roll, the player has a choice: hold Stop rolling and add the total of al numbers rolled this turn to their score, or roll Roll the die again. If the player rolls a 1, their turn ends and they score 0 for the turn. Points scored...

  • 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...

  • 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...

  • Create a Dice Game: PYTHON This game is between two people. They will roll a pair...

    Create a Dice Game: PYTHON This game is between two people. They will roll a pair of dice each 10 times. Each roll will be added to a total, and after 10 rolls the player with the highest total will be the Winner! You will use the RANDOM class RANDRANGE to create a random number from 1 to 6. You'll need a function for "rollDice" to roll a pair of dice and return a total of the dice. You must...

  • Assignment 2 – The two player game of Poaka The game of Poaka is a dice...

    Assignment 2 – The two player game of Poaka The game of Poaka is a dice game played by two players. Each player takes turns at rolling the dice. At each turn, the player repeatedly rolls a dice until either the player rolls a 1 (in which case the player scores 0 for their turn) or the player chooses to end their turn (in which case the player scores the total of all their dice rolls). At any time during...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • 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,...

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