Java code for Pig Game:
import static java.lang.System.*; import java.util.*; public class PigDiceGame { public static Scanner scan = new Scanner (in); public static Random generator = new Random (); public static int p1Score = 0; public static int p2Score = 0; public static int counter; public static void main(String args[]){ out.println("Welcome to Pig, the two-player dice game!"); out.println("Player 1 go first"); p1Mechanics (); } public static int Roll() { int Low = 1; int High = 7; int Roll = generator.nextInt (High - Low) + Low; return Roll; } public static void p1Mechanics() { counter = 0; out.println("Do you want to play this turn? (Y/N)"); String answer = scan.nextLine (); answer = answer.replaceAll (" ", "") ; answer = answer.toUpperCase (); if (answer.equals("N")) { out.println ("Player 1 skipped their turn."); out.println ("Player 2's turn to roll"); p2Mechanics (); } int Roll = Roll(); out.println ("You rolled a " + Roll); while (p1Score + counter < 100) { while (Roll != 1) { counter += Roll; out.println ("Do you want to roll more? (Y/N)"); answer = scan.nextLine (); answer = answer.replaceAll (" ", "") ; answer = answer.toUpperCase (); while(!(answer.equals("Y")) && !(answer.equals("N"))){ out.println("Please enter only Y/N:"); answer = scan.nextLine (); answer = answer.replaceAll (" ", "") ; answer = answer.toUpperCase (); } if (answer.equals("N")) { p1Score += counter; out.println ("Player 1 ended their turn."); out.println ("P1 vs P2: " + p1Score + "-" + p2Score); out.println ("Player 2's turn to roll"); p2Mechanics (); } Roll = Roll (); out.println ("You rolled a " + Roll); } if (Roll == 1) { counter = 0; } out.println ("Player 1 rolled a '1'."); out.println ("P1 vs P2: " + p1Score + "-" + p2Score); out.println ("Player 2's turn to roll"); p2Mechanics (); } out.println("Player 1's score reached 100!"); out.println("P1 wins by " + (p1Score - p2Score) + " points!"); exit (0); } public static void p2Mechanics() { counter = 0; out.println("Do you want to play this turn? (Y/N)"); String answer = scan.nextLine (); answer = answer.replaceAll (" ", "") ; answer = answer.toUpperCase (); if (answer.equals("N")) { out.println ("Player 2 skipped their turn."); out.println ("Player 1's turn to roll"); p1Mechanics (); } int Roll = Roll(); out.println ("You rolled a " + Roll); maxScore: while (p2Score < 100) { while (Roll != 1) { counter += Roll; out.println ("Do you want to roll more? (Y/N)"); answer = scan.nextLine (); answer = answer.replaceAll (" ", "") ; answer = answer.toUpperCase (); while(!(answer.equals("Y")) && !(answer.equals("N"))){ out.println("Please enter only Y/N:"); answer = scan.nextLine (); answer = answer.replaceAll (" ", "") ; answer = answer.toUpperCase (); } if (answer.equals("N")) { p2Score += counter; out.println ("Player 2 ended their turn."); out.println ("P1 vs P2: " + p1Score + "-" + p2Score); out.println ("Player 1's turn to roll"); p1Mechanics (); } Roll = Roll (); out.println ("You rolled a " + Roll); } if (Roll == 1) { counter = 0; } out.println ("Player 2 rolled a '1'."); out.println ("P1 vs P2: " + p1Score + "-" + p2Score); out.println ("Player 1's turn to roll"); p1Mechanics (); } out.println("Player 2's score reached 100!"); out.println("P2 wins by " + (p2Score - p1Score) + " points!"); exit (0); } }
Thank you,
Don't forget to upvote this answer. :)
java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a...
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...
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,...
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...
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...
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...
// 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,...
can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...
Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...
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...