-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205da3a
commit e644d04
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Pig Dice Game | ||
|
||
A simple command-line implementation of the **Pig Dice Game** in Python. Two players take turns rolling a die, racing to reach 100 points. The game has a risk/reward mechanic where rolling a 1 results in losing all points for the current turn. | ||
|
||
## How to Play | ||
|
||
1. Players take turns to roll a die. | ||
2. On each roll, if the player rolls a 1, their turn ends and they score nothing for that turn. | ||
3. If the player rolls a 2-6, they can choose to roll again or "hold". | ||
4. If the player chooses to hold, their turn total is added to their overall score, and their turn ends. | ||
5. The first player to reach 100 points wins the game. | ||
|
||
## Setup Instructions | ||
|
||
### Prerequisites | ||
|
||
- Python 3.x installed on your system. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import random | ||
|
||
def roll_die(): | ||
return random.randint(1, 6) | ||
|
||
def player_turn(player_name, total_score): | ||
turn_score = 0 | ||
while True: | ||
roll = roll_die() | ||
print(f"{player_name} rolled a {roll}") | ||
if roll == 1: | ||
print(f"{player_name} rolled a 1! Turn over. No points added.") | ||
return total_score | ||
else: | ||
turn_score += roll | ||
print(f"{player_name}'s turn score is now {turn_score}") | ||
choice = input("Do you want to 'roll' again or 'hold'? ").lower() | ||
if choice == 'hold': | ||
total_score += turn_score | ||
print(f"{player_name} holds. Total score is now {total_score}") | ||
return total_score | ||
|
||
def pig_game(): | ||
player_1 = input("Enter Player 1's name: ") | ||
player_2 = input("Enter Player 2's name: ") | ||
|
||
score_1 = 0 | ||
score_2 = 0 | ||
winning_score = 100 | ||
|
||
while score_1 < winning_score and score_2 < winning_score: | ||
print(f"\n{player_1}'s turn:") | ||
score_1 = player_turn(player_1, score_1) | ||
if score_1 >= winning_score: | ||
print(f"\n{player_1} wins with a score of {score_1}!") | ||
break | ||
|
||
print(f"\n{player_2}'s turn:") | ||
score_2 = player_turn(player_2, score_2) | ||
if score_2 >= winning_score: | ||
print(f"\n{player_2} wins with a score of {score_2}!") | ||
break | ||
|
||
if __name__ == "__main__": | ||
pig_game() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
if score_1 >= winning_score: |