-
Notifications
You must be signed in to change notification settings - Fork 168
/
ChessPiece.py
executable file
·50 lines (43 loc) · 1.41 KB
/
ChessPiece.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
class ChessPiece:
selected = False
is_king = False
def __init__(self, x, y, is_red, direction):
self.x = x
self.y = y
self.is_red = is_red
self.direction = direction
def is_north(self):
return self.direction == 'north'
def is_south(self):
return self.direction == 'south'
def get_move_locs(self, board):
moves = []
for x in range(9):
for y in range(10):
if (x,y) in board.pieces and board.pieces[x,y].is_red == self.is_red:
continue
if self.can_move(board, x-self.x, y-self.y):
moves.append((x,y))
return moves
def move(self, board, dx, dy):
nx, ny = self.x + dx, self.y + dy
if (nx, ny) in board.pieces:
board.remove(nx, ny)
board.remove(self.x, self.y)
#print 'Move a chessman from (%d,%d) to (%d,%d)'%(self.x, self.y, self.x+dx, self.y+dy)
self.x += dx
self.y += dy
board.pieces[self.x, self.y] = self
return True
def count_pieces(self, board, x, y, dx, dy):
sx = dx/abs(dx) if dx!=0 else 0
sy = dy/abs(dy) if dy!=0 else 0
nx, ny = x + dx, y + dy
x, y = x + sx, y + sy
cnt = 0
while x != nx or y != ny:
if (x, y) in board.pieces:
cnt += 1
x += sx
y += sy
return cnt