-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
193 lines (145 loc) · 4.61 KB
/
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
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
"""Game Module.
Contains the core game code.
"""
import copy
from functools import reduce
from typing import Any, NamedTuple
from cards import Cards, is_royal
# Game Settings
PLAYERS = 2
# Helpers:
def inc_player(turn):
return (turn + 1) % PLAYERS
def dec_player(turn):
return (turn - 1) % PLAYERS
# Python doesn't have compose?
def compose(f, g):
return lambda x: f(g(x))
# Game State Representation
class Game(NamedTuple):
player_cards: Any
stack: Any
next_player: Any
winner: Any = None
def new_game(deck=None):
"""Create a Game State representation of a new game."""
if not deck:
deck = Cards.new_deck().shuffle()
players = []
# NOTE This only works if there is no remainder, ie number of
# players is a divisor of the number of cards. For now.
cards_per_player = int(len(deck) / PLAYERS)
for i in range(0, PLAYERS):
players.append(deck.top_n(cards_per_player))
deck = deck.top_n_rest(cards_per_player)
stack = Cards()
next_player = 0
return Game(players, stack, next_player)
# Royal Penaltys
def last_royal(game):
"""Find the last Royal put down in the stack
Returns: (position of Royal, the Card)
"""
if len(game.stack) > 0:
try:
return next(filter(compose(is_royal, lambda c: c[1]),
enumerate(reversed(game.stack))))
except StopIteration:
pass
return None
royal_penalties = {
'J': 1,
'Q': 2,
'K': 3,
'A': 4,
}
# Game Operations
def flip_card(game):
"""Have the active player put a card down. Nonmutating."""
(player_cards, stack, next_player, winner) = game
player_cards = player_cards.copy()
active_player = player_cards[next_player]
# Lets place a card down
stack = Cards(stack + active_player.top())
player_cards[next_player] = active_player.top_rest()
return Game(player_cards, stack, next_player)
def stack_won(game):
"""Have the stack be won by the previous player. Nonmutating.
Additionally, change the next player to the winning player.
"""
(player_cards, stack, next_player, winner) = game
player_cards = player_cards.copy()
winning_player = dec_player(next_player)
# NOTE The stack is effectively turned upside down and put underneath
player_cards[winning_player] = Cards(player_cards[winning_player] + stack)
stack = Cards()
next_player = winning_player
return Game(player_cards, stack, next_player)
def next_turn(game):
"""Have the turn move to the next_player. Nonmutating."""
(player_cards, stack, next_player, winner) = game
return Game(player_cards, stack, inc_player(next_player))
def skip_out_players(game):
"""Skip players that are 'out'. Nonmutating"""
(player_cards, stack, next_player, winner) = game
if len(player_cards[next_player]) == 0:
return next_turn(game)
return game
def is_winner(game):
return reduce(lambda acc, cards: acc or len(cards) == 0,
game.player_cards, False)
def apply_wins(game):
"""Have the winner be declared, if any. Nonmutating."""
if is_winner(game):
(player_cards, stack, next_player, winner) = game
winner = next(filter(lambda x: len(x[1]) != 0,
enumerate(game.player_cards)))[0]
return Game(player_cards, stack, next_player, winner)
return game
def step_game(game):
"""Have the game progress a step. Nonmutating."""
if game.winner is not None:
return game
game = skip_out_players(game)
game = flip_card(game)
last = last_royal(game)
if last:
(pos, c) = last
if pos == 0:
game = next_turn(game)
elif pos == royal_penalties[c.rank]:
game = stack_won(game)
else:
game = next_turn(game)
game = apply_wins(game)
return game
def run_tests():
for i in range(0, 100):
g = new_game()
# Checking that they don't mutate game
orig_g = copy.deepcopy(g)
next_turn(g)
assert orig_g == g
skip_out_players(g)
assert orig_g == g
flip_card(g)
assert orig_g == g
g = flip_card(g)
orig_g = copy.deepcopy(g)
stack_won(g)
assert orig_g == g
step_game(g)
assert orig_g == g
for i in range(0, 10):
g = step_game(g)
orig_g = copy.deepcopy(g)
step_game(g)
assert orig_g == g
flip_card(g)
assert orig_g == g
stack_won(g)
assert orig_g == g
apply_wins(g)
assert orig_g == g
print("Testing game.py: OK")
run_tests()