-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.py
58 lines (43 loc) · 1.54 KB
/
board.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
class Board(object):
def __init__(self, board):
self.board = board
self.width = len(board[0])
self.height = len(board)
def tile_at(self, loc):
from const import TILE_OUT_OF_BOUNDS
(x,y) = loc
if((y >= 0) and (y < len(self.board)) and (x >= 0) and (x < len(self.board[y]))):
return self.board[y][x]
return TILE_OUT_OF_BOUNDS
def is_floor(self, loc):
from const import TILE_FLOOR
return self.tile_at(loc) == TILE_FLOOR
class Location(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "(%s,%s)"%(self.x, self.y)
def __add__(self, other):
return Location(self.x+other.x, self.y+other.y)
def __sub__(self, other):
return Location(self.x-other.x, self.y-other.y)
def __len__(self):
return abs(self.x)+abs(self.y)
def __neg__(self):
return Location(-self.x,-self.y)
def __abs__(self):
return Location(abs(self.x),abs(self.y))
def __getitem__(self, key):
return [self.x, self.y][key]
def __eq__(self, other):
return (self.x,self.y) == (other.x,other.y)
def __hash__(self):
return hash((self.x,self.y))
class Positionable(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.loc = Location(x,y)
def __repr__(self):
return "%s: %s"%(self.__class__.__name__, "(%s,%s)"%(self.x, self.y))