-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
223 lines (206 loc) · 8.76 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import sys
import random
print('Welcome to 5,000 dice.')
rules = input('Do you want to read the rules? (Y/N)')
if rules.upper() == 'Y':
print('''
OBJECTIVE:
Be the first player to score 5,000 points!
Roll 5 dice and earn points by collecting three of a kind, 1's and 5's.
Once a player reaches 5,000 points, each player gets one last turn to try and earn the top score.
COUNTERS:
Points are obtained through counters.
Counters are:
1's
5's
Any 3 of a kind
SCORING:
1 = 100pts each or 1,000pts for three,
5 = 50pts each or 500pts for three,
3 of a kind = *00pts (200 - 600)
ADDITIONAL:
You must initially score 350 points to add a score to the board, then you can score at any time.
You must save at least one counter to continue rolling. i.e. save at least one scoring die from your current roll to continue.
If your roll is 1, 2, 2, 3, 4.. you can save both 2's to try for a third. But, you also have to save the 1 to roll again.
If your next roll is 1, 2, 2, 5, 6 because you saved 1, 2, 2; then your current score is 150 and you have to keep 1, 2, 2, 5 to roll again.
If you roll no counters ('garbage'), your turn is over and you lose your points accumulated in that turn.
If all five dice are counters, you can continue increasing your score by rerolling all five dice again.
''')
# The player turn. One of these for each turn. Return the next player.
def rollLoop(playerTurn: int) -> int:
score = 0
currScore = 0
diceRoll = []
keepDice = []
while True:
print(f"It's player {playerTurn}'s turn.")
turn = input('Do you want to...\n(R)oll Dice\n(K)eep Dice\nShow (D)ice\n(S)core Roll/End Turn\nShow (A)ll Scores\nShow (C)urrent Score\n(Q)uit Game\n')
if turn.upper() == 'R':
if not keepDice:
score += currScore
currScore = 0
if len(keepDice) == 5:
score += currScore
currScore = 0
keepDice = []
diceRoll = rollDice(5 - len(keepDice), diceRoll[0:len(keepDice)])
if checkScore(diceRoll[len(keepDice):]) == 0:
showDice(diceRoll, score, currScore)
print('You blew it.')
showScores()
return playerTurn
currScore = checkScore(diceRoll)
showDice(diceRoll, score, currScore)
if checkScore(diceRoll) == 0:
print('You blew it!')
showScores()
return playerTurn
elif turn.upper() == 'K':
if not diceRoll:
print('You do not have any dice to keep. Roll first.')
continue
else:
try:
keepDice = [int(i) for i in (input('Which dice do you want to keep? e.g. 12, 123, 35\n'))]
except ValueError:
print('Please enter a valid combo. e.g. 12, 123, 35')
diceRoll = saveDice(keepDice, diceRoll)
#TODO You have to keep the same dice and at least one additional roller to keep rolling.
showDice(diceRoll, score, currScore)
elif turn.upper() == 'D':
showDice(diceRoll, score, currScore)
elif turn.upper() == 'S':
if playerScores[playerTurn] == 0:
if (score + checkScore(diceRoll)) < 350:
print("You need at least 350 points to get on the board.")
else:
playerScores[playerTurn] += score + checkScore(diceRoll)
showScores()
return playerTurn
else:
playerScores[playerTurn] += score + currScore
showScores()
return playerTurn
elif turn.upper() == 'A':
showScores()
elif turn.upper() == 'C':
print(f"Your current score is {score}, don't blow it")
elif turn.upper() == 'Q':
sys.exit()
else:
print('Enter a valid move. ("S", "K", "R", "D", "C", "Q")')
continue
# Choose the number of players and load a score card in playerScores.
def numPlayers() -> int:
while True:
players = input('How many people are playing? (1 - 10)')
if players not in [str(i) for i in range(1, 11)]:
print('Please enter a number between 1 and 10')
else:
return int(players)
# Print all player scores.
def showScores() -> None:
for player, score in playerScores.items():
print()
print(f'Player {player} score is {score}')
print()
# Print the current roll nicely.
def showDice(diceRoll: list, score: int, currScore: int) -> None:
dice = {0: ['| |','| |','| |'],
1: ['| |','| 0 |','| |'],
2: ['| |','| 0 0 |','| |'],
3: ['| 0 |','| 0 |','| 0 |'],
4: ['| 0 0 |','| |','| 0 0 |'],
5: ['| 0 0 |','| 0 |','| 0 0 |'],
6: ['| 0 0 |','| 0 0 |','| 0 0 |']}
if not diceRoll:
print("You haven't rolled the dice yet.")
return
print(f' Die 1 \t Die 2 \t Die 3 \t Die 4 \t Die 5')
print(f'-----------\t-----------\t-----------\t-----------\t-----------')
print(f'{dice[diceRoll[0]][0]}\t{dice[diceRoll[1]][0]}\t{dice[diceRoll[2]][0]}\t{dice[diceRoll[3]][0]}\t{dice[diceRoll[4]][0]}')
print(f'{dice[diceRoll[0]][1]}\t{dice[diceRoll[1]][1]}\t{dice[diceRoll[2]][1]}\t{dice[diceRoll[3]][1]}\t{dice[diceRoll[4]][1]}')
print(f'{dice[diceRoll[0]][2]}\t{dice[diceRoll[1]][2]}\t{dice[diceRoll[2]][2]}\t{dice[diceRoll[3]][2]}\t{dice[diceRoll[4]][2]}')
print(f'-----------\t-----------\t-----------\t-----------\t-----------')
print(f'\nYour current score is {currScore + score}')
# Calculate a score from the current roll.
def checkScore(diceRoll: list) -> int:
scoreCheck = 0
if 1 in diceRoll:
if diceRoll.count(1) >= 3:
scoreCheck += 1000 + ((diceRoll.count(1) - 3) * 100)
if diceRoll.count(1) < 3:
scoreCheck += 100 * diceRoll.count(1)
if 2 in diceRoll:
if diceRoll.count(2) >= 3:
scoreCheck += 200
if 3 in diceRoll:
if diceRoll.count(3) >= 3:
scoreCheck += 300
if 4 in diceRoll:
if diceRoll.count(4) >= 3:
scoreCheck += 400
if 5 in diceRoll:
if diceRoll.count(5) >= 3:
scoreCheck += 500 + ((diceRoll.count(5) - 3) * 50)
if diceRoll.count(5) < 3:
scoreCheck += 50 * diceRoll.count(5)
if 6 in diceRoll:
if diceRoll.count(6) >= 3:
scoreCheck += 600
return scoreCheck
# Check playerScores for a winner.
def checkWin(playerTurn: int, playerScores: dict, players: int) -> bool:
for i in range(1, players + 1):
if playerScores[i] >= 5000:
print(f'Player {playerTurn} has reached 5,000 points! The other players get one more turn to earn the highest score.')
return False
return True
# Save selected dice from the roll. Check if they are good to save.
# Return 5 dice with 0's as placeholders for dice not saved.
def saveDice(keepDice: list, diceRoll: list) -> list:
toSave = []
for i in keepDice:
toSave.append(diceRoll[int(i) - 1])
if checkScore(toSave) == 0:
print('You cannot save this combination of dice because they have no scoring counters.')
return diceRoll
for i in range(5 - len(toSave)):
toSave.append(0)
return toSave
# Roll and return 5 dice. If dice were saved, move them to the front of the roll and roll the remaining of 5 dice.
def rollDice(numDice: int, keptDice=[]) -> list:
diceRoll = []
if numDice == 5:
for i in range(5):
roll = random.randint(1, 6)
diceRoll.append(roll)
else:
diceRoll = keptDice
for i in range(5 - len(diceRoll)):
roll = random.randint(1, 6)
diceRoll.append(roll)
return diceRoll
# Globals
running = True
players = numPlayers() # Select number of players.
playerScores = {(i + 1): 0 for i in range(players)} # Dictionary of Players (1 - x) and scores initialized at 0.
playerTurn = 1 # Player 1 starts the game.
# Game loop. Run a player's turn and exit the loop if a player has won.
while running:
playerTurn = rollLoop(playerTurn)
running = checkWin(playerTurn, playerScores, players)
if playerTurn < players:
playerTurn += 1
else:
playerTurn = 1
# Someone has won, give every other player one more turn.
if playerTurn != 1:
playerTurn -= 1
else:
playerTurn = players
for i in range(1, players + 1):
if i != playerTurn:
rollLoop(i)
# Winner is whoever has the highest score after the last extra turn.
print(f'Player {max(playerScores, key=playerScores.get)} has won the game!')