-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
123 lines (103 loc) · 3.36 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
from player import Player
from pool import Pool
from tile import Tile
import utils
import math
class Game:
def __init__(self):
self.num_players = 2
self.num_factories = 5
self.tiles_per_factory = 4
self.max_score = 0
self.first_player = None
self.winning_player = None
self.end_game = False
self.end_round = False
self.rng_play = True
self.players = []
self.pool = None
for i in range(self.num_players):
self.players.append(Player("Player {}".format(i + 1)))
def play_game(self):
round_num = 1
while not self.end_game:
taken = set()
cycle = 1
print("#################### ROUND {} #####################".format(round_num))
self.pool = Pool(self.num_factories, self.tiles_per_factory)
while not self.end_round:
cycle += 1
print("\n#################### CYCLE {} #####################".format(cycle))
for factory in self.pool.factories:
print(factory)
print(self.pool.middle)
print("#################### CYCLE {} #####################\n".format(cycle))
for player in self.players:
while not self.pool.is_empty():
try:
remaining_factories = list(set(range(self.num_factories + 1)) - taken)
if self.rng_play:
f_index = utils.random_factory(remaining_factories)
target_tile = Tile.NULL
if f_index == 0:
target_tile = utils.random_tile(self.pool.middle)
else:
target_tile = utils.random_tile(self.pool.factories[f_index - 1])
grabbed_tiles = self.pool.draw(f_index, target_tile)
else:
f_index = 0
target_tile = Tile.NULL
grabbed_tiles = []
# TODO: Implement custom play
break
except IndexError:
pass
print("{} took {} from factory {}".format(player.name, target_tile, f_index))
if Tile.NULL in grabbed_tiles:
self.first_player = player
player.play_turn(grabbed_tiles, self.rng_play)
if self.pool.is_empty():
self.end_round = True
break
print("\n#################### ROUND {} #####################".format(round_num))
round_num += 1
self.players.pop(self.players.index(self.first_player))
self.players.insert(0, self.first_player)
self.first_player = None
self.end_round = False
for player in self.players:
score = player.get_score()
for i in player.board.board_container:
for j in i:
print(math.floor(j), end=' ')
print()
print("{} has {} points".format(player.name, score))
if score > self.max_score:
self.max_score = score
self.winning_player = player
elif score == self.max_score:
self.winning_player = None
self.end_game = self.end_game or player.is_finished()
print("\n#################### END OF GAME #####################")
scores = []
for player in self.players:
score = player.get_final_score()
for i in player.board.board_container:
for j in i:
print(math.floor(j), end=' ')
print()
print("{} has {} points".format(player.name, score))
scores.append(score)
if score > self.max_score:
self.max_score = score
self.winning_player = player
elif score == self.max_score:
self.winning_player = None
if self.winning_player:
print("{} wins!".format(self.winning_player.name))
else:
print("it was a tie!")
return scores, (self.winning_player, self.max_score)
if __name__ == '__main__':
g = Game()
scores, winner = g.play_game()