-
Notifications
You must be signed in to change notification settings - Fork 0
/
farkle.py
107 lines (71 loc) · 3.25 KB
/
farkle.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
#!/usr/bin/env python
from random import randint
# Verification functions and point calculation
from verify import *
scoreboard = {} # Keep track of players' scores
order = [] # Maintain order of players as input
# Input player's names and create scoreboard
def players():
for i in range(verify_players()):
name = input("Player %s's name: " % (i+1)) # TODO Check input for error
while True:
if name not in scoreboard:
scoreboard[name] = 0
order.append(name)
break
else:
name = input("%s has already been added to the game.\nPlease give player %s a unique name: "
% (name, i+1)) # TODO Check input for errors
return scoreboard
# Roll n number of dice
def roll(n):
return [str(randint(1, 6)) for _ in range(n)]
# Play hand
def hand(player, score):
dice = 6
set_aside = [] # Dice set aside
while True:
current_roll = roll(dice)
print("You just rolled:\n%s\n" % ' '.join(current_roll))
# Verify roll has value and check if all-dice
verified_roll = point_count(set_aside, current_roll)
if len(verified_roll[1]) is 0: # If all dice have value
print("Congratulations, you get to roll them all again!")
return [verified_roll[0], 'all']
if len(verified_roll[1]) != dice: # If there's at least one valuable di
print(point_count(set_aside, current_roll)[0]) # TODO Be more informative about scoring
while True:
action = verify_user_input() # Ask player what they want to do
if action == 1:
keep = verify_keep(set_aside, current_roll) # Keep some dice
set_aside.append(keep) # Add kept dice to list
dice -= len(keep) # Update number of dice available to roll
break
else:
# End turn and keep points
hand_result = point_count(set_aside, current_roll)
if scoreboard[player] < 1000 and hand_result[0] + score < 1000: # Check for min score for stopping
print("You must end your turn with at least 1000 points to get on the board.\n")
else:
return hand_result[0], 'done' # End hand as well as turn
else:
print("Sorry, you didn't roll any points.")
return [0, 'fail'] # If no points rolled, end turn with no points
# TODO Create turn system in order
# Starts the turn
def turn(player):
score = 0
while True:
hand_result = hand(player, score) # Passes name and accumulated turn score, returns points and ending reason
score += hand_result[0]
if hand_result[1] is 'done': # Finished turn and adds points to scoreboard if player successful
scoreboard[player] += score
print(scoreboard)
break
elif hand_result[1] is 'fail': # Finishes turn with no points if player failed
print(scoreboard)
break
print("Your score for this turn so far is:\n%s" % score) # Loops if all dice in hand had value
# TODO Make last turn scenario
players()
print(turn('John'))