-
Notifications
You must be signed in to change notification settings - Fork 0
/
Big-Pig.py
225 lines (200 loc) · 6.93 KB
/
Big-Pig.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
224
225
'''
Pedro Cruz
big-pig.py
'''
import random
def printInstructions() -> None:
"""
This function prints the instructions of the game to the console.
@parameters:
none.
@return:
none.
"""
print("Welcome to Big Pig, the dice rolling game where players try to be "\
"the first get 100 points! Players (you and the computer) will take turns "\
"rolling two dice as many times as they want, adding all roll results to "\
"a running total. Players lose their gained score for the turn if they "\
"roll a 1.")
def printStatus(human: int, computer: int) -> None:
"""
This functions prints the current scoreboard of the players to the console.
@parameters:
human (int) - current score of human player
computer (int) - current score of computer player
@return:
none.
"""
print()
print("--------------------------------------------------")
print("Player has %d and computer has %d." %(human, computer))
def getUserChoice() -> str:
"""
This function gets the user input for the current turn. User can choose
to either [r]oll the dice - keep playing for the turn - or [h]old
the dice - save points from the current turn to game score.
@parameters:
none.
@return:
choice (string) - user input; either [r]oll or [h]old.
"""
choice = input("What do you want to do: [r]oll or [h]old? ")
while (not isValid(choice)):
choice = input("What do you want to do: [r]oll or [h]old? ")
return choice
def isValid(choice: str) -> bool:
"""
This function checks whether the user input is valid.
@parameters:
choice (string) - user's choice.
@return:
boolean; True if @choice is valid or False if @choice is not valid.
"""
valid = ["r", "h", "hold", "roll"]
if choice not in valid:
return False
return True
def printRoll(name: str, dice: list, pts: int) -> None:
"""
This function prints the roll outcomes.
@parameters:
name - (str) player who rolled.
diceValuces - (list) values of the dice.
score - (int) total round score.
@return:
none.
"""
print("%s rolled [%d, %d], current round score: %d"%(name, dice[0], dice[1], pts))
def rollDice()-> 'list':
"""
This function randomly rolls two dice.
@parameters:
none.
@return:
List; values of random dice rolling.
"""
diceOne = random.randrange(1,7)
diceTwo = random.randrange(1,7)
return [diceOne, diceTwo]
def rollUpdate(diceValues: 'list') -> int:
"""
This function computes the score of the player based on the values of
their dice.
@parameters:
diceValues (list) - the values of the dice after rolling.
@return:
diceScore (int) - the score associated with the values of the dice.
It can be:
a. 0 if the player rolls a 1 on one die and anything except a
1 on the other die;
b. (diceValues[0] + diceValues[1]) if the player rolls two different
values on their dice;
c. 25 if the player rolls a 1 on both dice;
d. (2 * (diceValues[0] + diceValues[1])) If the player rolls the
same value on both dice (and that value is greater than 1).
"""
diceScore = 0
if (diceValues[0] == 1 and diceValues[1] == 1):
diceScore = 25
elif (1 in diceValues):
diceScore = 0
elif (diceValues[0] != diceValues[1]):
diceScore = (diceValues[0] + diceValues[1])
else:
diceScore = 2*(diceValues[0] + diceValues[1])
return diceScore
def isGameOver(human: int, computer: int) -> bool:
"""
This function determines whether the game is over or not. The game ends
when one of the players score at least 100 points.
@parameters:
human (int) - current score of human player
computer (int) - current score of computer player
@return:
boolean; True if game is over or False is should continue playing.
"""
if (human >= 100 or computer >= 100):
return True
return False
def printGameResults(human: int, computer: int) -> None:
"""
This function prints the final results of the game, announcing the winner
alongside the final scoreboard.
@parameters:
human (int) - current score of human player
computer (int) - current score of computer player
@return:
none.
"""
winner = "Computer"
if human > computer:
winner = "Human"
print()
print("%s wins [%d, %d]!" %(winner, human, computer))
def playComputerGame(human: int, computer: int) -> int:
"""
This function simulates one turn played by the computer.
@parameter:
human (int) - current score of human player
computer (int) - current score of computer player
@return:
turnResult (int) - the result of the turn played by the computer.
"""
turnScore = 0
if human < 100:
while (turnScore < 20):
myDice = rollDice()
rollResult = rollUpdate(myDice)
if (rollResult == 0):
turnScore = 0
printRoll("computer", myDice, turnScore)
print("Big pig!")
break
turnScore += rollResult
printRoll("computer", myDice, turnScore)
else:
while (computer < human):
myDice = rollDice()
rollResult = rollUpdate(myDice)
if (rollResult == 0):
turnScore = 0
printRoll("computer", myDice, turnScore)
print("Big pig!")
break
turnScore += rollResult
printRoll("computer", myDice, turnScore)
return turnScore
def playUserGame(human: int, computer: int) -> int:
"""
This functions plays one turn of the game.
@parameters:
human (int) - current score of human player
computer (int) - current score of computer player
@return:
turnScore (int) - the score of the turn played by the user.
"""
turnScore = 0
choice = getUserChoice()
while (choice not in ["hold", "h"]):
myDice = rollDice()
rollResult = rollUpdate(myDice)
if (rollResult == 0):
turnScore = 0
printRoll("human", myDice, turnScore)
print("Big pig!")
break
turnScore += rollResult
printRoll("human", myDice, turnScore)
choice = getUserChoice()
return turnScore
def main():
userScore = 0
computerScore = 0
printInstructions()
while (not (isGameOver(userScore, computerScore))):
printStatus(userScore, computerScore)
userScore += playUserGame(userScore, computerScore)
printStatus(userScore, computerScore)
computerScore += playComputerGame(userScore, computerScore)
printGameResults(userScore, computerScore)
main()