-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.py
106 lines (88 loc) · 3.68 KB
/
State.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
#! /usr/bin/env python
from copy import deepcopy
import Constant as constant
from logger import logger
class State:
def __init__(self, turn, diceRoll, position, cashHoldings, bankMoney, propertyStatus):
self.turn = turn
self.diceRoll = diceRoll
self.position = position
self.cashHoldings = cashHoldings
self.bankMoney = bankMoney
self.propertyStatus = propertyStatus
def __deepcopy__(self, memo):
# Referred: https://stackoverflow.com/questions/4794244/how-can-i-create-a-copy-of-an-object-in-python
id_self = id(self)
_copy = memo.get(id_self)
if _copy is None:
_copy = type(self)(
deepcopy(self.turn, memo),
deepcopy(self.diceRoll, memo),
deepcopy(self.position, memo),
deepcopy(self.cashHoldings, memo),
deepcopy(self.bankMoney, memo),
deepcopy(self.propertyStatus, memo))
memo[id_self] = _copy
return _copy
def isPlayerInJail(self, playerId):
return self.position[playerId] == constant.IN_JAIL_INDEX
def getPlayerPosition(self, playerId):
return self.position[playerId]
def isPropertyEmpty(self, position):
return self.propertyStatus[position] == constant.INITIAL_PROPERTY_STATUS
def allPropertiesOfMonopolyOwned(self, playerId, position, monopolyGroup):
propertyOwner = self.getPropertyOwner(position)
for position in monopolyGroup:
if propertyOwner != self.getPropertyOwner(position):
return False
return True
def allHousesEvenlyDistributed(self, playerId, position, monopolyGroup):
max = 0
min = 10
#Property between -7 and 7
monopolyGroup.append(position)
for position in monopolyGroup:
status = abs(self.propertyStatus[position])
if status == 0:
return False
if status < min:
min = status
if status > max:
max = status
#Diff of <=1 in min and max houses and property status = min
return (max-min <=1) and (abs(self.propertyStatus[position]) == min)
def updateHouseStatus(self, playerId, position):
#All conditions for buying house must satisfy!!
status = self.propertyStatus[position]
if status < 0:
self.propertyStatus[position] = status-1
else:
self.propertyStatus[position] = status+1
def allHouseConditionsSatisfied(self, playerId, position, monopolyGroup):
logger.debug('Checking for House Conditions')
if (self.allHousesEvenlyDistributed(playerId, position, monopolyGroup) \
and self.allPropertiesOfMonopolyOwned(playerId, position, monopolyGroup) \
and self.propertyStatus[position] > -5 and self.propertyStatus[position] < 5):
#Houses can be built from -5 to 5
return True
return False
def getPropertyOwner(self, position):
# Player 1
if self.propertyStatus[position] > 0:
return 0
# Player 2
elif self.propertyStatus[position] < 0:
return 1
else:
return -1
def getPropertyStatus(self, position):
return self.propertyStatus[position]
def updatePropertyStatus(self, position, playerId):
sign = 1
if playerId == 1:
sign = -1
self.propertyStatus[position] = sign
def updateCashHolding(self, playerId, cashToAdd):
newCashHolding = list(self.cashHoldings)
newCashHolding[playerId] = self.cashHoldings[playerId] + cashToAdd
self.cashHoldings = tuple(newCashHolding)