-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
147 lines (118 loc) · 3.05 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
import random
import fileinput
suits = {
0: "dots",
1: "bamboo",
2: "characters",
3: "winds",
4: "dragons",
}
values = {
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "east",
11: "south",
12: "west",
13: "north",
14: "white",
15: "green",
16: "red",
}
class Suit:
DOT = 0
BAMBOO = 1
CHARACTER = 2
WIND = 3
DRAGON = 4
class Wind:
EAST = 10
SOUTH = 11
WEST = 12
NORTH = 13
class Dragon:
WHITE = 14
GREEN = 15
RED = 16
class Tile:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def is_honor(self):
return self.value > 9
def __repr__(self):
return values[self.value] + " of " + suits[self.suit]
def __str__(self):
return values[self.value] + " of " + suits[self.suit]
class Player:
def __init__(self, seat):
self.seat = seat
self.hand = []
self.discards = []
def __str__(self):
r = ""
for tile in self.hand:
r += str(tile) + "\n"
return r
def draw(self, state, count = 1):
for i in range(count):
self.hand.append(state.wall.pop())
def discard(self, tile_index):
self.discards.append(self.hand.pop(tile_index))
class GameState:
current_player_index = 0
def __init__(self, players, wall):
self.players = players
self.wall = wall
def increment_player(self):
self.current_player_index = (self.current_player_index + 1) % len(self.players)
def current_player(self):
return self.players[self.current_player_index]
def generate_wall():
tiles = []
for suit in range(Suit.CHARACTER + 1):
for value in range(1, 10):
tiles.append(Tile(suit, value))
for value in range(Wind.EAST, Wind.NORTH + 1):
tiles.append(Tile(Suit.WIND, value))
for value in range(Dragon.WHITE, Dragon.RED + 1):
tiles.append(Tile(Suit.DRAGON, value))
return tiles + tiles + tiles + tiles
def initial_draw(state):
for i in range(3 * len(state.players)):
player = state.current_player()
player.draw(state, 4)
state.increment_player()
for i in range(len(state.players)):
player = state.current_player()
player.draw(state)
state.increment_player()
def init_game():
wall = generate_wall()
random.shuffle(wall)
players = list(map(lambda wind: Player(wind), range(Wind.EAST, Wind.NORTH + 1)))
state = GameState(players, wall)
initial_draw(state)
return state
def discard(state, args):
index = int(args[0])
state.current_player().discard(index)
state.increment_player()
return state
def handle_command(state, command, args):
return {
"discard": discard,
}[command](state, args)
def main():
state = init_game()
for input in fileinput.input():
command, *args = input.split()
state = handle_command(state, command.strip(), args)
fileinput.close()
return state