-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-player-dice-rolling-game.py
58 lines (47 loc) · 2.22 KB
/
multi-player-dice-rolling-game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import random
# Function to simulate rolling a dice
def roll():
# Get the number of players
while True:
try:
players = int(input('Enter the number of players (2-4): '))
if players < 2 or players > 4:
print("Please enter a valid number of players (2-4).")
else:
break
except ValueError:
print("Invalid input, please enter an integer between 2 and 4.")
max_score = 50
player_scores = [0] * players # Initialize a list to store each player's score
roll_options = ('y', 'n')
while True:
# Loop through each player's turn
for player in range(players):
print(f"\nPlayer {player + 1}'s turn")
# Ask if the player wants to roll the dice
roll_ = input('Would you like to roll (y/n): ').lower()
if roll_ == 'n':
print(f"Player {player + 1} has chosen to skip their turn.")
continue # Skip to the next player
elif roll_ == 'y':
roll_value = random.randint(1, 6) # Roll the dice
print(f"You rolled: {roll_value}")
if roll_value == 1:
print("You rolled a 1! Your turn is over with 0 points added.")
player_scores[player] = 0 # Reset the player's score for this turn
else:
player_scores[player] += roll_value # Add roll to the player's total score
print(f"Player {player + 1}'s current score: {player_scores[player]}")
# Check if the player reached the target score
if player_scores[player] >= max_score:
print(f"\nPlayer {player + 1} wins with {player_scores[player]} points!")
return # End the game if a player wins
else:
print("Invalid input. Please enter 'y' to roll or 'n' to skip the turn.")
# Display the updated scores after each player's turn
print("\nCurrent scores:")
for i in range(players):
print(f"Player {i + 1}: {player_scores[i]} points")
def main():
roll()
main()