-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queen.py
81 lines (64 loc) · 3.01 KB
/
Queen.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
# Queen Chess Piece (Alison)
from ChessPiece import ChessPiece # remove later
class Queen(ChessPiece):
"""Defines a Queen chess piece for a chess game"""
def __init__(self, color, pos,startPos):
"""Creates a new Queen!
Args:
color (bool): True if white, False if black
pos (tuple): initial position of the pawn
"""
super().__init__(color, pos,startPos)
self.rules = ((0,1),(0,-1),(1,1),(1,-1),(1,0),(-1,1),(-1,-1),(-1,0))
def calculatePossibleMoves(self, gameState, pos):
"""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): # as far as the Queen wants bc powerful!
move = (pos[0]+rel[0]*i, pos[1]+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:
if (not self.checkCheck(gameState, pos, move, self.color)):
moves.append(move)
break # no need to continue in this direction
# if move does not cause a checkmate
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): # as far as the Queen wants bc powerful!
move = (pos[0]+rel[0]*i, pos[1]+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
def main():
# testing queen
win = GraphWin("testing", 800,700)
q1 = Queen(True, (1,1))
q1.draw(win)
print(q1.calculatePossibleMoves(True, (2,2)))
if __name__ == "__main__":
from graphics import *
main()