MCPcopy Create free account
hub / github.com/ndleah/python-mini-project / GameState

Class GameState

Chess_Game/ChessEngine.py:2–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1
2class GameState:
3
4 def __init__(self):
5 self.board = [
6 ["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
7 ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
8 ["--", "--", "--", "--", "--", "--", "--", "--"],
9 ["--", "--", "--", "--", "--", "--", "--", "--"],
10 ["--", "--", "--", "--", "--", "--", "--", "--"],
11 ["--", "--", "--", "--", "--", "--", "--", "--"],
12 ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
13 ["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]]
14 self.moveFunctions = {'p': self.getPawnMoves, 'R': self.getRookMoves, 'N': self.getKnightMoves,
15 'B': self.getBishopMoves, 'Q': self.getQueenMoves, 'K': self.getKingMoves}
16 self.whiteToMove = True,
17 self.moveLog = []
18 self.whiteKingLocation = (7, 4)
19 self.blackKingLocation = (0, 4)
20 self.checkMate = False
21 self.staleMate = False
22
23 def makeMove(self, move):
24 self.board[move.startRow][move.startCol] = "--"
25 self.board[move.endRow][move.endCol] = move.pieceMoved
26 self.moveLog.append(move)
27 self.whiteToMove = not self.whiteToMove
28 if move.pieceMoved == "wK":
29 self.whiteKingLocation = (move.endRow, move.endCol)
30 elif move.pieceMoved == "bK":
31 self.blackKingLocation = (move.endRow, move.endCol)
32
33 if move.isPawnPromotion:
34 self.board[move.endRow][move.endCol] = move.pieceMoved[0] + "Q"
35
36
37 def undoMove(self):
38 if len(self.moveLog) != 0:
39 move = self.moveLog.pop()
40 self.board[move.startRow][move.startCol] = move.pieceMoved
41 self.board[move.endRow][move.endCol] = move.pieceCaptured
42 self.whiteToMove = not self.whiteToMove
43 if move.pieceMoved == "wK":
44 self.whiteKingLocation = (move.startRow, move.startCol)
45 if move.pieceMoved == "bK":
46 self.blackKingLocation = (move.startRow, move.startCol)
47 """
48 All move considering checks
49 """
50 def getValidMoves(self):
51 moves = self.getAllPossibleMoves()
52 for i in range(len(moves)-1, -1, -1):
53 self.makeMove(moves[i])
54 self.whiteToMove = not self.whiteToMove
55 if self.inCheck():
56 moves.remove(moves[i])
57 self.whiteToMove = not self.whiteToMove
58 self.undoMove()
59 if len(moves) == 0:
60 if self.inCheck():

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected