-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
161 lines (132 loc) · 5.29 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
from action import *
from cells import *
from constants import Constants
from money import Money
from player import Player
from turn_state import TurnState
class Game:
def __init__(self, manager):
self.mgr = manager
self.cells = [
StartCell(),
PropertyCell(0, "property.brown.1"),
CommunityChestCell(),
PropertyCell(0, "property.brown.2"),
TaxCell(200),
RailroadCell("property.railroad.1"),
PropertyCell(1, "property.light_blue.1"),
ChanceCell(),
PropertyCell(1, "property.light_blue.2"),
PropertyCell(1, "property.light_blue.3"),
JailCell(),
PropertyCell(2, "property.pink.1"),
CompanyCell("property.electric"),
PropertyCell(2, "property.pink.2"),
PropertyCell(2, "property.pink.3"),
RailroadCell("property.railroad.2"),
PropertyCell(3, "property.orange.1"),
CommunityChestCell(),
PropertyCell(3, "property.orange.2"),
PropertyCell(3, "property.orange.3"),
ParkingCell(),
PropertyCell(4, "property.red.1"),
ChanceCell(),
PropertyCell(4, "property.red.2"),
PropertyCell(4, "property.red.3"),
RailroadCell("property.railroad.3"),
PropertyCell(5, "property.yellow.1"),
PropertyCell(5, "property.yellow.2"),
CompanyCell("property.water"),
PropertyCell(5, "property.yellow.3"),
GoToJailCell(),
PropertyCell(6, "property.green.1"),
PropertyCell(6, "property.green.2"),
CommunityChestCell(),
PropertyCell(6, "property.green.3"),
RailroadCell("property.railroad.4"),
ChanceCell(),
PropertyCell(7, "property.blue.1"),
TaxCell(100),
PropertyCell(7, "property.blue.2")
]
self.players = []
for i in range(8):
player = Player(i)
player.money[500] = 2
player.money[100] = 2
player.money[50] = 2
player.money[20] = 6
player.money[10] = 5
player.money[5] = 5
player.money[1] = 5
self.players.append(player)
self.player = self.players[0]
for cell in self.cells:
if isinstance(cell, PropertyCell) and cell.owner is not None:
self.players[cell.owner].properties.append(cell)
self.money_pot = []
self.turn = 0
self.turn_state = TurnState.ACTION_CHOICE
self.start_turn()
def get_cell(self, i):
cell = self.cells[i]
n = i%10
if i < 10:
x = 10-n
y = 10
elif i < 20:
x = 0
y = 10-n
elif i < 30:
x = n
y = 0
else:
x = 10
y = n
r = i//10
return (cell, x, y, r)
def start_turn(self):
self.turn_state = TurnState.ACTION_CHOICE
self.player.has_rolled = False
self.actions = self.get_actions()
def end_turn(self):
self.turn += 1
self.turn %= len(self.players)
def get_actions(self):
if self.turn != self.player.id: return []
actions = []
player = self.player
cell = self.get_cell(self.player.pos)
if player.has_rolled:
actions.append(EndTurnAction(self, player))
if player.in_jail:
if not player.has_rolled and player.jail_rolls < Constants.MAX_JAIL_ROLLS:
actions.append(RollDiceAction(self, player))
actions.append(PayJailAction(self, player))
if player.get_out_of_jail_cards:
actions.append(GetOutJailCardAction(self, player))
elif not player.has_rolled:
actions.append(RollDiceAction(self, player))
if isinstance(cell, PropertyCell):
if cell.owner is None:
actions.append(BuyAction(self, player, cell))
if player.properties:
actions.append(SellAction(self, player))
not_mortgaged = list(filter(lambda p: not p.mortgaged, player.properties))
mortgaged = list(filter(lambda p: p.mortgaged, player.properties))
house_place = list(filter(lambda p: 0 <= p.get_houses() < 4, player.properties))
four_houses = list(filter(lambda p: p.get_houses() == 4 and not p.has_hotel(), player.properties))
hotel = list(filter(lambda p: p.has_hotel(), player.properties))
if not_mortgaged:
actions.append(MortgageAction(self, player))
if mortgaged:
actions.append(LiftMortgageAction(self, player))
if house_place:
actions.append(BuyHouseAction(self, player))
if house_place or four_houses:
actions.append(SellHouseAction(self, player))
if four_houses:
actions.append(BuyHotelAction(self, player))
if hotel:
actions.append(SellHotelAction(self, player))
return actions