-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckBoard.py
More file actions
130 lines (114 loc) · 5.79 KB
/
checkBoard.py
File metadata and controls
130 lines (114 loc) · 5.79 KB
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
import piece
# reference lists
blackSymbols = ['♜', '♞', '♝', '♛', '♚', '♟']
whiteSymbols = ['♖', '♘', '♗', '♕', '♔', '♙']
colours = ["white", "black"]
# function iterates through entire board, returns position of the king
def findKing(chessboard, currentTurn):
kingPosition = False
for searchY in range(0, 8):
for searchX in range(0, 8):
if (chessboard[searchY][searchX] == "♔" and currentTurn == "white") or \
(chessboard[searchY][searchX] == "♚" and currentTurn == "black"):
kingPosition = [searchX, searchY]
return kingPosition
# function that checks for a directional piece in a direction, i.e. bishop, rook, queen
def checkThreateningSurrounding(chessboard, initialX, initialY, pieceColour,
xDirection, yDirection, xEnd, yEnd, threateningPiece):
kingThreatened = False
pieceNotFound = True
xTarget = initialX + xDirection
yTarget = initialY + yDirection
while pieceNotFound and xTarget != xEnd and yTarget != yEnd:
targetPiece = piece.Piece(chessboard[yTarget][xTarget])
if targetPiece.colour:
if targetPiece.colour != pieceColour and targetPiece.type == threateningPiece:
kingThreatened = True
pieceNotFound = False
xTarget = xTarget + xDirection
yTarget = yTarget + yDirection
return kingThreatened
# function looping through all directions, looking for rooks, bishops and queens threatening
def directionalCheck(chessboard, kingX, kingY, currentTurn):
directionalThreatening = False
# list with all the directions to check for, and what pieces to check for in those directions
directionalPieces = [[-1, -1, -1, -1, "bishop"], [-1, 1, -1, 8, "bishop"], [1, -1, 8, -1, "bishop"],
[1, 1, 8, 8, "bishop"], [-1, 0, -1, 10, "rook"], [1, 0, 8, 10, "rook"],
[0, -1, 10, -1, "rook"], [0, 1, 10, 8, "rook"], [-1, -1, -1, -1, "queen"],
[-1, 1, -1, 8, "queen"], [1, -1, 8, -1, "queen"], [1, 1, 8, 8, "queen"],
[-1, 0, -1, 10, "queen"], [1, 0, 8, 10, "queen"], [0, -1, 10, -1, "queen"],
[0, 1, 10, 8, "queen"]]
for directionToCheck in directionalPieces:
xDirection = directionToCheck[0]
yDirection = directionToCheck[1]
xEnd = directionToCheck[2]
yEnd = directionToCheck[3]
threateningPiece = directionToCheck[4]
if checkThreateningSurrounding(chessboard, kingX, kingY, currentTurn,
xDirection, yDirection, xEnd, yEnd, threateningPiece):
directionalThreatening = True
return directionalThreatening
# checks if the other king is in the 8 tiles surrounding
def kingCheck(chessboard, kingX, kingY, currentTurn):
kingThreatening = False
for yMovement in [-1, 0, 1]:
for xMovement in [-1, 0, 1]:
if not (yMovement == 0 and xMovement == 0) and kingY + yMovement >= 0 and kingX + xMovement >= 0:
try:
targetPiece = piece.Piece(chessboard[kingY + yMovement][kingX + xMovement])
if targetPiece.colour and targetPiece.type == "king" and targetPiece.colour != currentTurn:
kingThreatening = True
except IndexError:
pass
return kingThreatening
# checks the 2 diagonal pieces ahead for if they are enemy pawns
def pawnCheck(chessboard, kingX, kingY, currentTurn):
pawnThreatening = False
pawnCheckDirection = 1
if currentTurn == "white":
pawnCheckDirection = -1
for leftRight in [-1, 1]:
try:
diagonalPiece = piece.Piece(chessboard[kingY + pawnCheckDirection][kingX + leftRight])
if diagonalPiece.colour and diagonalPiece.colour != currentTurn and diagonalPiece.type == "pawn":
pawnThreatening = True
except IndexError:
pass
return pawnThreatening
# function checks for opposing knights in L shapes from the king
def knightCheck(chessboard, kingX, kingY, currentTurn):
knightThreatening = False
for xPlusMinus in ["+", "-"]:
for yPlusMinus in ["+", "-"]:
for length in [[1, 2], [2, 1]]:
if xPlusMinus == "+":
targetX = kingX + length[0]
else:
targetX = kingX - length[0]
if yPlusMinus == "+":
targetY = kingY + length[1]
else:
targetY = kingY - length[1]
# try except used so that it only detects tiles within board
try:
targetPiece = piece.Piece(chessboard[targetY][targetX])
if targetPiece.colour and targetPiece.colour != currentTurn and \
targetPiece.type == "knight" and targetY >= 0 \
and targetX >= 0:
knightThreatening = True
except IndexError:
pass
return knightThreatening
# checks the state of the game, whether the current king is under check
def checkState(chessboard, currentTurn):
# gets the king's position
kingPosition = findKing(chessboard, currentTurn)
kingX = kingPosition[0]
kingY = kingPosition[1]
# calls all the check functions, combines them to return a boolean whether the king is under check
directionalThreatening = directionalCheck(chessboard, kingX, kingY, currentTurn)
kingThreatening = kingCheck(chessboard, kingX, kingY, currentTurn)
pawnThreatening = pawnCheck(chessboard, kingX, kingY, currentTurn)
knightThreatening = knightCheck(chessboard, kingX, kingY, currentTurn)
inCheck = directionalThreatening or kingThreatening or pawnThreatening or knightThreatening
return inCheck