Javatpoint Logo
Javatpoint Logo

The Pig Game in Java

The Pig Game, also known as "Pig Dice Game" or "Pass the Pigs," is a simple and entertaining dice game that can be implemented using Java programming language. It involves rolling a pair of dice and accumulating points based on the outcome. The objective of the game is to be the first player to reach a predetermined point total, often 100. In this article, we will explore the rules of the Pig Game and demonstrate how to implement it in Java.

Rules of the Pig Game:

The game is typically played with two or more players.

  • On each turn, a player rolls a pair of six-sided dice.
  • The player accumulates points based on the sum of the dice roll.
  • If the player rolls a 1 on either die, their turn ends, and they lose all the points accumulated during that turn.
  • If the player rolls a 1 on both dice, they lose all their points and their turn ends.
  • The player can choose to "hold" at any point during their turn, which adds the accumulated points to their total score.
  • The first player to reach or exceed the predetermined point total (e.g., 100) wins the game.

Implementation in Java:

To implement the Pig Game in Java, we can follow these steps:

Step 1: Set up the game:

  • Define the number of players and the target point total.
  • Initialize the total score of each player to zero.

Step 2: Implement the dice roll:

  • Generate random numbers between 1 and 6 to simulate rolling the dice.
  • Add the numbers rolled to the player's current turn score.
  • Check for a roll of 1 on either die and handle the turn ending conditions.

Step 3: Implement the player's turn:

  • Prompt the current player to roll the dice or hold.
  • Call the dice roll function and update the player's score accordingly.
  • Repeat until the player decides to hold or rolls a 1.

Step 4: Implement the game loop:

  • Rotate the turn between players.
  • Keep track of the total score for each player.
  • Check if any player has reached or exceeded the target point total.

Step 5: Declare the winner:

  • Display the winner's name and total score.

Here's a complete Java program that implements the Pig Game.

PigGame.java

Output:

Enter the number of players: 2

Player 1's turn.
Current Score: 0
Dice 1: 4
Dice 2: 3
Turn score: 7
Do you want to roll again (R) or hold (H)? r

Player 1's turn.
Current Score: 7
Dice 1: 2
Dice 2: 6
Turn score: 8
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 15
Dice 1: 1
Dice 2: 3
Oops! You rolled a 1. Turn score reset to 0.

Player 1's turn.
Current Score: 15
Dice 1: 5
Dice 2: 5
Turn score: 10
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 0
Dice 1: 4
Dice 2: 4
Turn score: 8
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 8
Dice 1: 6
Dice 2: 6
Oops! You rolled two 1's. Turn score reset to 0.

Player 1's turn.
Current Score: 25
Dice 1: 1
Dice 2: 6
Oops! You rolled a 1. Turn score reset to 0.

Player 2's turn.
Current Score: 0
Dice 1: 3
Dice 2: 5
Turn score: 8
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 25
Dice 1: 2
Dice 2: 1
Turn score: 3
Do you want to roll again (R) or hold (H)? r

Player 1's turn.
Current Score: 28
Dice 1: 6
Dice 2: 3
Turn score: 9
Do you want to roll again (R) or hold (H)? r

Player 1's turn.
Current Score: 37
Dice 1: 4
Dice 2: 6
Turn score: 10
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 8
Dice 1: 2
Dice 2: 3
Turn score: 5
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 13
Dice 1: 1
Dice 2: 4
Oops! You rolled a 1. Turn score reset to 0.

Player 1's turn.
Current Score: 47
Dice 1: 3
Dice 2: 1
Turn score: 4
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 51
Dice 1: 6
Dice 2: 2
Turn score: 8
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 13
Dice 1: 2
Dice 2: 4
Turn score: 6
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 19
Dice 1: 5
Dice 2: 2
Turn score: 7
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 26
Dice 1: 3
Dice 2: 4
Turn score: 7
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 33
Dice 1: 4
Dice 2: 1
Turn score: 5
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 38
Dice 1: 5
Dice 2: 3
Turn score: 8
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 59
Dice 1: 1
Dice 2: 2
Turn score: 3
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 46
Dice 1: 2
Dice 2: 4
Turn score: 6
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 62
Dice 1: 5
Dice 2: 4
Turn score: 9
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 52
Dice 1: 5
Dice 2: 1
Turn score: 6
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 71
Dice 1: 3
Dice 2: 4
Turn score: 7
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 58
Dice 1: 6
Dice 2: 2
Turn score: 8
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 78
Dice 1: 1
Dice 2: 2
Turn score: 3
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 66
Dice 1: 5
Dice 2: 3
Turn score: 8
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 74
Dice 1: 6
Dice 2: 4
Turn score: 10
Do you want to roll again (R) or hold (H)? h

Player 1's turn.
Current Score: 81
Dice 1: 2
Dice 2: 2
Turn score: 4
Do you want to roll again (R) or hold (H)? r

Player 1's turn.
Current Score: 85
Dice 1: 1
Dice 2: 4
Turn score: 5
Do you want to roll again (R) or hold (H)? h

Player 2's turn.
Current Score: 84
Dice 1: 5
Dice 2: 4
Turn score: 9
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 93
Dice 1: 2
Dice 2: 6
Turn score: 8
Do you want to roll again (R) or hold (H)? r

Player 2's turn.
Current Score: 101
Dice 1: 3
Dice 2: 5
Turn score: 8

Player 2 wins with a score of 101!

The Pig Game is a simple yet entertaining dice game that can be easily implemented in Java. By following the rules and steps outlined in this article, you can create a fully functional Pig Game program. This project offers an excellent opportunity to practice your Java programming skills while having fun. So, gather your friends, roll the dice, and enjoy playing the Pig Game in Java!







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA