-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameSquare.py
executable file
·157 lines (118 loc) · 3.78 KB
/
gameSquare.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
#!/usr/bin/python
"""
CS Games 2012
AI Competition
Chris Iverach-Brereton
This file contains the squares that the game board is made up of
Each type of square is a subclass of a generic GameSquare superclass
"""
import entity
import sys
class GameSquare:
"""
This class is the generic superclass that all other types of square
inherit from
"""
_id = -1
_isPassable = True
_blocksLOS = False
_isSearched = False
_x = -1
_y = -1
# the Entity that is in this square
_occupant = None
def __init__(self, x, y, isPassable=True, blocksLOS=False, isSearched=False):
"""
Create a new square with a specific x/y position
"""
self._x = x
self._y = y
self._isPassable = isPassable
self._blocksLOS = blocksLOS
self._isSearched = isSearched
GameSquare.lastId = GameSquare.lastId + 1
self._id = GameSquare.lastId
GameSquare.allSquares.insert(self._id,self)
def __str__(self):
occupantId = -1
if(self._occupant!=None):
occupantId = self._occupant.getId()
return "Square {0} [occupant:{1},passable:{2},blocklos:{3},searched:{4},x:{5},y:{6}]".format(
self._id,
occupantId,
self._isPassable,
self._blocksLOS,
self._isSearched,
self._x,
self._y
)
def loadParameters(self, configStr):
tokens = configStr.split()
id = tokens[1]
self._id = int(id)
attrs = configStr[configStr.index('[')+1:configStr.index(']')]
attrTokens = attrs.split(',')
for t in attrTokens:
tokens = t.split(':')
name = tokens[0].lower().strip()
value = tokens[1].lower().strip()
if name == 'occupant':
if int(value) == -1:
self._occupant = None
else:
#sys.stderr.write(str(value)+ ' ' + str(len(entity.Entity.allEntities))+'\n')
self.setOccupant(entity.Entity.allEntities[int(value)])
elif name == 'passable':
self._isPassable = (value == 'true')
elif name == 'blocklos':
self._blocksLOS = (value == 'true')
elif name == 'searched':
self._isSearched = (value == 'true')
elif name == 'x':
self._x = int(value)
elif name == 'y':
self._y = int(value);
def isSearched(self):
return self._isSearched
def isPassable(self):
return self._isPassable
def blocksLOS(self):
return self._blocksLOS
def search(self):
if not self.isSearched():
self.isSearched = True
def isEmpty(self):
return self._occupant == None
def getRow(self):
return self._y
def getColumn(self):
return self._x
def getId(self):
return self._id
def setOccupant(self, entity):
self._occupant = entity
if entity!=None:
entity.setSquare(self)
def getOccupant(self):
return self._occupant
def clearOccupant(self):
if self._occupant !=None:
self._occupant.setSquare(None)
self.setOccupant(None)
GameSquare.lastId = -1
GameSquare.DIRECTION_UP = 1
GameSquare.DIRECTION_DOWN = 2
GameSquare.DIRECTION_LEFT = 3
GameSquare.DIRECTION_RIGHT = 4
GameSquare.allSquares = []
def findSquareByLocation(x,y):
"""
Find a square with specific x/y coordinates
(note: x is the same as column, y is the same as row
so if you use row/column notation instead of x/y you
should call this function as findSquareByLocation(col,row)
"""
for s in GameSquare.allSquares:
if s._x == x and s._y == y:
return s
return None