-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bishop.py
66 lines (52 loc) · 2.55 KB
/
Bishop.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
# Bishop Chess Piece (Reid)
from ChessPiece import ChessPiece
class Bishop(ChessPiece):
def __init__(self, color, pos, startPos):
super().__init__(color, pos,startPos)
self.rules = ((1,1),
(1,-1),
(-1,1),
(-1,-1))
def calculatePossibleMoves(self, gameState: list, pos: tuple) -> list:
"""Calculates all the possible moves given the game state. Returns a list of tuples representing possible moves.
Args:
gameState (list): a 8x8x3 matrix [rectangle object, litUp, chessPiece object]
pos (tuple): current position
"""
moves = []
for rel in self.rules:
for i in range(1,8): # up to 8 squares
move = self._toGlobal(pos, (rel[0]*i, rel[1]*i)) #convert to absolute position, scaled
#if within bounds and cur space is different color/empty, we can move into it
if self.withinBounds(move):
if gameState[move[0]][move[1]][2] != None:
color = gameState[move[0]][move[1]][2].color
if color != self.color:
if (not self.checkCheck(gameState, pos, move, self.color)):
moves.append(move)
break #stop this direction
# checkcheckcheckcheck
if not self.checkCheck(gameState, pos, move, self.color):
moves.append(move)
return moves
def getAllMoves(self, gameState, pos):
"""Returns all possible moves
Args:
gameState: the current game state, a list of shape (8, 8, 3)
pos (tuble): current position
Returns:
list of moves (filters out of bounds)
"""
moves = []
for rel in self.rules:
for i in range(1,8): # up to 8 squares
move = self._toGlobal(pos, (rel[0]*i, rel[1]*i)) #convert to absolute position
# if both withiin bounds and overtakes an empty or diifferent color piece
if self.withinBounds(move):
if gameState[move[0]][move[1]][2] != None:
color = gameState[move[0]][move[1]][2].color
if color != self.color:
moves.append(move)
break # no need to continue in this direction
moves.append(move)
return moves