-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzombie_dice.py
90 lines (74 loc) · 2.93 KB
/
zombie_dice.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
from random import randint
'''
Features
- Player can roll a die and get a result of caught, ran, or fought. DONE
- Player can roll three dice at a time. DONE
- Player can keep rolling and accumulate a point for every 'caught' result. DONE
- Player's turn ends with 0 points if they accumulate three 'fought' results. DONE
- Player can end turn early. DONE
- Player keeps their points from turn to turn. DONE
- Two or more players can play.
- Game ends at the end of a round if a player has 13 or more points.
- Players can get dice with different probabilities, selected randomly.
'''
def main():
player = Player()
keep_playing = True
player.take_turn()
while keep_playing: # TODO: Fix bug: The game won't let the player only take one turn.
player.take_turn()
keep_playing = ask_yes_no('Keep playing?')
class Die(object):
def __init__(self):
self.value = 'ran'
def roll(self):
number = randint(1, 6)
if number == 1:
self.value = 'fought'
elif 2 <= number <= 4: # How sure am I that this works right?
self.value = 'ran'
else: # Should I be more explicit here and use "elif number >= 5:" instead?
self.value = 'caught'
class Player(object):
def __init__(self):
self.score = 0
def take_turn(self):
print(f'Your current total score is {self.score}.')
roll_again = True
turn_score = 0
fought_rolls = 0
while roll_again and fought_rolls < 3:
print('-' * 20)
rolls = self.roll()
roll_score = 0 # Maybe I should calculate roll_score in its own method?
for roll in rolls:
print(f'Your rolled a "{roll}".')
if roll == 'caught':
roll_score += 1
if roll == 'fought':
fought_rolls += 1
print(f'You scored {roll_score} points.')
turn_score += roll_score
print(f'Your turn score is now {turn_score} points and have gotten {fought_rolls} fought rolls.')
roll_again = ask_yes_no('Roll again?') # TODO: Don't ask if they want to roll again if they've rolled 3 foughts.
if fought_rolls < 3:
self.score += turn_score
else:
print(f'You rolled too many fought rolls and got no points this turn.')
print(f'Your total score is now {self.score}.')
print('=' * 20)
def roll(self):
dice = [Die(), Die(), Die()]
for die in dice:
die.roll()
return [die.value for die in dice]
def ask_yes_no(question): # TODO: My questions look like this: "Do you want chocolate?yes". I want a space between my question and the user's answer.
answer = ''
while answer.lower() not in ['yes', 'y', 'no', 'n']:
answer = input(question)
if answer in ['yes', 'y']:
return True
else:
return False
if __name__ == '__main__':
main()