-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessEngine.py
122 lines (103 loc) · 4.51 KB
/
ChessEngine.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
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
# This class is responsible for storing all the information about the current state of a chess game.
# It will also be responsible for determining the valid moves at the current state.
# It will also keep a move log.
class GameState():
def __init__(self):
# board is 8x8 2d list, each element has 2 characters.
# first character represents the colour.
# second represents the piece type.
# "--" represents an empty space with no piece.
self.board = [
["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]
]
self.whiteToMove = True
self.moveLog = []
'''
Takes a Move as a parameter and executes it( this will not work for castling, pawn promotion and en passant)
'''
def makeMove(self, move):
self.board[move.startRow][move.startCol] = "--"
self.board[move.endRow][move.endCol] = move.pieceMoved
self.moveLog.append(move)
self.whiteToMove = not self.whiteToMove # swaps players
'''
Undo the last move made
'''
def undoMove(self):
if len(self.moveLog) != 0:
move = self.moveLog.pop()
self.board[move.startRow][move.startCol] = move.pieceMoved
self.board[move.endRow][move.endCol] = move.pieceCaptured
self.whiteToMove = not self.whiteToMove # switch turns back
'''
All moves considering checks
'''
def getValidMoves(self):
return self.getAllPossibleMoves() # for now we will not worry about checks
'''
All moves without considering checks
'''
def getAllPossibleMoves(self):
moves = [Move((6, 4), (4, 4), self.board)]
for r in range(len(self.board)): # number of rows
for c in range(len(self.board[r])): # number of cols in row
turn = self.board[r][c][0]
if (turn == 'w' and self.whiteToMove) or (turn == 'b' and not self.whiteToMove):
piece = self.board[r][c][1]
if piece == 'p':
self.getPawnMoves(r, c, moves)
elif piece == 'R':
self.getRookMoves(r, c, moves)
return moves
'''
Get all the pawn moves for the pawn located at row, col and add these moves to the list
'''
def getPawnMoves(self, r, c, moves):
if self.whiteToMove: # white pawn moves
if self.board[r - 1][c] == "--": # 1 sq pawn advance
moves.append(Move((r, c), (r - 1, c), self.board))
if r == 6 and self.board[r - 2][c] == "--": # 2 sq pawn advance
moves.append(Move((r, c), (r - 2, c), self.board))
if c - 1 >= 0: #captures to the left
if self.board[r-1][c-1][0] == 'b':
moves.append(Move((r-1,c-1),(r-1,c-1),self.board))
if c + 1 <= 7:
if self.board
def getRookMoves(self, r, c, moves):
pass
class Move():
# maps keys to values
# key : value
ranksToRows = {"1": 7, "2": 6, "3": 5, "4": 4,
"5": 3, "6": 2, "7": 1, "8": 0}
rowsToRanks = {v: k for k, v in ranksToRows.items()}
filesToCols = {"a": 0, "b": 1, "c": 2, "d": 3,
"e": 4, "f": 5, "g": 6, "h": 7}
colsToFiles = {v: k for k, v in filesToCols.items()}
def __init__(self, startSq, endSq, board):
self.startRow = startSq[0]
self.startCol = startSq[1]
self.endRow = endSq[0]
self.endCol = endSq[1]
self.pieceMoved = board[self.startRow][self.startCol]
self.pieceCaptured = board[self.endRow][self.endCol]
self.moveID = self.startRow * 1000 + self.startCol * 100 + self.endRow * 10 + self.endCol
print(self.moveID)
'''
Overriding the equals method
'''
def __eq__(self, other):
if isinstance(other, Move):
return self.moveID == other.moveID
return False
def getChessNotation(self):
return self.getRankFile(self.startRow, self.startCol) + self.getRankFile(self.endRow, self.endCol)
def getRankFile(self, r, c):
return self.colsToFiles[c] + self.rowsToRanks[r]