-
Notifications
You must be signed in to change notification settings - Fork 13
/
game.py
executable file
·177 lines (156 loc) · 6.35 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
#!/usr/bin/python
#
# Copyright 2015 - Jonathan Gordon
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
import random
from common import *
from cards import helpers
from players import *
import logger
def init_games():
global __all_cards
global __all_wonders
__all_cards = helpers.read_cards_file("card-descriptions.txt")
__all_wonders = Wonders.read_wonders_file("wonders.txt")
class GameState:
def __init__(self, players):
self.player_count = len(players)
self.players = []
self.ages = []
self.decks = [[]] * len(players)
self.discard_pile = []
self.logger = logger.Logger()
for i in range(len(players)):
name, persona = players[i]
self.players.append(Players.Player(name))
self.players[i].set_personality(persona())
def setup_age_cards(self, cards):
age_1 = [c for c in cards if c.age == 1 and c.players <= self.player_count]
age_2 = [c for c in cards if c.age == 2 and c.players <= self.player_count]
age_3 = [c for c in cards if c.age == 3 and c.get_colour() != CARDS_PURPLE and c.players <= self.player_count]
purple = [c for c in cards if c.age == 3 and c.get_colour() == CARDS_PURPLE and c.players <= self.player_count]
random.shuffle(age_1)
random.shuffle(age_2)
random.shuffle(purple)
age_3 += purple[0 : self.player_count + 2]
random.shuffle(age_3)
self.ages = [age_1, age_2, age_3]
def deal_age_cards(self, age):
cards = self.ages[age][0:]
p = 0
for i in range(self.player_count):
self.decks[i] = []
while len(cards):
self.decks[p].append(cards[0])
p += 1
p %= self.player_count
cards = cards[1:]
def _get_west_player(self, playerid):
return self.players[(playerid + self.player_count - 1) % self.player_count]
def _get_east_player(self, playerid):
return self.players[(playerid + 1) % self.player_count]
def play_turn(self, offset):
for i in range(self.player_count):
player = self.players[i]
west_player = self._get_west_player(i)
east_player = self._get_east_player(i)
deckid = (i + offset) % self.player_count
player.print_tableau()
# This loop is actually wrong.
# Everyone should choose the card they will play, server
# validates the move is legal, then each player plays the card
# Then each player adds the new card to their tableau
while True:
action, card = player.play_hand(self.decks[deckid], west_player, east_player)
if action == ACTION_PLAYCARD:
can_buy = False
# see if the player can buy the card
if player.can_build_with_chain(card):
can_buy = True
self.logger.log_buy_card_with_chain(player, card)
else:
buy_options = player.buy_card(card, west_player, east_player)
if len(buy_options) == 0:
continue
self.logger.log_buy_card(player, card, buy_options[0])
can_buy = True
player.money -= buy_options[0].total_cost
east_player.money += buy_options[0].east_cost
west_player.money += buy_options[0].west_cost
if can_buy:
card.play(player, west_player, east_player)
player.get_cards().append(card)
break
elif action == ACTION_DISCARD:
self.logger.log_action(player, action, card)
self.discard_pile.append(card)
player.money += 3
break
elif action == ACTION_STAGEWONDER:
self.logger.log_action(player, action, card)
# make sure we can do that
break
self.decks[deckid].remove(card)
def game_loop(self):
for age in range(3):
self.logger.log_age_header(age)
self.deal_age_cards(age)
offset = 0
while len(self.decks[0]) > 1:
self.play_turn(offset)
offset = (offset + [1, self.player_count - 1, 1][age]) % self.player_count
# everyone discards the last card
for p in range(self.player_count):
self.discard_pile.append(self.decks[p][0])
# score military
for p in range(self.player_count):
west = self._get_west_player(p)
east = self._get_east_player(p)
player = self.players[p]
player_strength, opponent_strength, score = helpers.score_military(player, west, age)
self.logger.log_military_battle(player.get_name(), player_strength, west.get_name(), opponent_strength, score)
self.players[p].military.append(score)
player_strength, opponent_strength, score = helpers.score_military(player, east, age)
self.logger.log_military_battle(player.get_name(), player_strength, east.get_name(), opponent_strength, score)
self.players[p].military.append(score)
for i in range(self.player_count):
player = self.players[i]
west = self._get_west_player(i)
east = self._get_east_player(i)
score = 0
bluescore = helpers.score_blue(player)
(_,_,_,), greenscore = helpers.score_science(player)
score += greenscore
redscore = 0
for military in player.military:
redscore += military
moneyscore = player.money / 3
yellowscore = helpers.score_yellow(player, west, east)
purplescore = helpers.score_purple(player, west, east)
player.print_tableau()
totalscore = bluescore + greenscore + redscore + moneyscore + yellowscore + purplescore
text = "Final score: Blue: %d, Green: %d, red: %d, yellow: %d, purple: %d, $: %d, total: %d" % (bluescore, greenscore, redscore, yellowscore, purplescore, moneyscore, totalscore)
self.logger.log_freetext(player.get_name() + " " + text)
print text
logfile = open("logfile.txt", "w")
self.logger.dump(logfile)
logfile.close()
init_games()
game = GameState([("alice", Personalities.StupidAI), ("Bob", Personalities.StupidAI), ("Frank", Personalities.StupidAI)])
game.logger.card_list = __all_cards
game.setup_age_cards(__all_cards)
game.game_loop()
#p = game.players[0]
#p.money = 10
#p.tableau += [find_card(__all_cards, "quarry"), find_card(__all_cards, "clay pit"), find_card(__all_cards, "press")]
#game.players[1].tableau += [find_card(__all_cards, "glassworks"), find_card(__all_cards, "sawmill"), find_card(__all_cards, "foundry")]
#p.buy_card(find_card(__all_cards, "fortification"), game.players[1], game.players[2])
#game.players[0].tableau += [find_card(__all_cards, "study"), find_card(__all_cards, "lodge"), find_card(__all_cards, "scientist guild")]
#print helpers.score_science(game.players[0])