def instantiateAll(self, color): piecesAlive = [] if color == ChessConstants.COLOR[0]: for x in range(8): newPawn = Pawn(color, ChessConstants.WHITE_PAWN_POS[x]) piecesAlive.append(newPawn) for x in range(2): newKnight = Knight(color, ChessConstants.WHITE_KNIGHT_POS[x]) piecesAlive.append(newKnight) newBishop = Bishop(color, ChessConstants.WHITE_BISHOP_POS[x]) piecesAlive.append((newBishop)) newRook = Rook(color, ChessConstants.WHITE_ROOK_POS[x]) piecesAlive.append(newRook) newQueen = Queen(color, ChessConstants.WHITE_QUEEN_POS) piecesAlive.append(newQueen) newKing = King(color, ChessConstants.WHITE_KING_POS) piecesAlive.append(newKing) elif color == ChessConstants.COLOR[1]: for x in range(8): newPawn = Pawn(color, ChessConstants.BLACK_PAWN_POS[x]) piecesAlive.append(newPawn) for x in range(2): newKnight = Knight(color, ChessConstants.BLACK_KNIGHT_POS[x]) piecesAlive.append(newKnight) newBishop = Bishop(color, ChessConstants.BLACK_BISHOP_POS[x]) piecesAlive.append(newBishop) newRook = Rook(color, ChessConstants.BLACK_ROOK_POS[x]) piecesAlive.append(newRook) newQueen = Queen(color, ChessConstants.BLACK_QUEEN_POS) piecesAlive.append(newQueen) newKing = King(color, ChessConstants.BLACK_KING_POS) piecesAlive.append(newKing) return piecesAlive
def reset(self): self.pieces = [] for x in range(0, 8): self.pieces.append(Pawn.Pawn((x, 6), True)) self.pieces.append(Pawn.Pawn((x, 1), False)) # White Pieces self.pieces.append(Rook.Rook((7, 7), True)) self.pieces.append(Rook.Rook((0, 7), True)) self.pieces.append(Knight.Knight((1, 7), True)) self.pieces.append(Knight.Knight((6, 7), True)) self.pieces.append(Bishop.Bishop((2, 7), True)) self.pieces.append(Bishop.Bishop((5, 7), True)) self.pieces.append(Queen.Queen((3, 7), True)) self.pieces.append(King.King((4, 7), True)) # Black Pieces self.pieces.append(Rook.Rook((7, 0), False)) self.pieces.append(Rook.Rook((0, 0), False)) self.pieces.append(Knight.Knight((1, 0), False)) self.pieces.append(Knight.Knight((6, 0), False)) self.pieces.append(Bishop.Bishop((2, 0), False)) self.pieces.append(Bishop.Bishop((5, 0), False)) self.pieces.append(Queen.Queen((3, 0), False)) self.pieces.append(King.King((4, 0), False)) # Set turn self.whitesTurn = True
def loadMinorPieces(self): for i in range(8): black_pawn_piece = Pawn('BLACK', i, 1) self.matrix[i][1] = black_pawn_piece white_pawn_piece = Pawn('WHITE', i, 6) self.matrix[i][6] = white_pawn_piece self.black_pieces.append(black_pawn_piece) self.white_pieces.append(white_pawn_piece)
def setup_starting_positions(self): self.button_identities[0][0].config(image=self.pictures["Rook"]) self.button_identities[0][1].config(image=self.pictures["Knight"]) self.button_identities[0][2].config(image=self.pictures["Bishop"]) self.button_identities[0][3].config(image=self.pictures["Queen"]) self.button_identities[0][4].config(image=self.pictures["King"]) self.button_identities[0][5].config(image=self.pictures["Bishop"]) self.button_identities[0][6].config(image=self.pictures["Knight"]) self.button_identities[0][7].config(image=self.pictures["Rook"]) for x in range(8): self.button_identities[1][x].config(image=self.pictures["Pawn"]) self.button_identities[6][x].config(image=self.pictures["PawnFilled"]) self.button_identities[7][0].config(image=self.pictures["RookFilled"]) self.button_identities[7][1].config(image=self.pictures["KnightFilled"]) self.button_identities[7][2].config(image=self.pictures["BishopFilled"]) self.button_identities[7][3].config(image=self.pictures["KingFilled"]) self.button_identities[7][4].config(image=self.pictures["QueenFilled"]) self.button_identities[7][5].config(image=self.pictures["BishopFilled"]) self.button_identities[7][6].config(image=self.pictures["KnightFilled"]) self.button_identities[7][7].config(image=self.pictures["RookFilled"]) for x in range(2,6): for y in range(8): self.button_identities[x][y].config(image=self.pictures["Empty"]) self.piece_array[0][0] = Rook(PieceColour.WHITE,self.gameMaster) self.piece_array[0][1] = Knight(PieceColour.WHITE,self.gameMaster) self.piece_array[0][2] = Bishop(PieceColour.WHITE,self.gameMaster) self.piece_array[0][3] = Queen(PieceColour.WHITE,self.gameMaster) self.piece_array[0][4] = King(PieceColour.WHITE,self.gameMaster) self.piece_array[0][5] = Bishop(PieceColour.WHITE,self.gameMaster) self.piece_array[0][6] = Knight(PieceColour.WHITE,self.gameMaster) self.piece_array[0][7] = Rook(PieceColour.WHITE,self.gameMaster) for x in range(8): self.piece_array[1][x] = Pawn(PieceColour.WHITE,self.gameMaster) self.piece_array[7][0] = Rook(PieceColour.BLACK,self.gameMaster) self.piece_array[7][1] = Knight(PieceColour.BLACK,self.gameMaster) self.piece_array[7][2] = Bishop(PieceColour.BLACK,self.gameMaster) self.piece_array[7][3] = King(PieceColour.BLACK,self.gameMaster) self.piece_array[7][4] = Queen(PieceColour.BLACK,self.gameMaster) self.piece_array[7][5] = Bishop(PieceColour.BLACK,self.gameMaster) self.piece_array[7][6] = Knight(PieceColour.BLACK,self.gameMaster) self.piece_array[7][7] = Rook(PieceColour.BLACK,self.gameMaster) for x in range(8): self.piece_array[6][x] = Pawn(PieceColour.BLACK,self.gameMaster) self.manual_assign()
def test_promotion(): g = Game() g.clear() g.add_piece(Pawn("WHITE"), ("B", 7)) g.add_piece(King("WHITE"), ("E", 1)) g.add_piece(Pawn("BLACK"), ("G", 2)) g.add_piece(King("BLACK"), ("E", 7)) g.update_all_pieces() assert (g.is_legal_move("WHITE", ("B", 7), ("B", 8))) g.move(("B", 7), ("B", 8)) assert (g.board.piece_at(("B", 8)).piece_type == "Queen") g.update_all_pieces() assert (g.is_legal_move("BLACK", ("G", 2), ("G", 1))) g.move(("G", 2), ("G", 1)) assert (g.board.piece_at(("G", 1)).piece_type == "Queen")
def __init__(self, color, pieces, board, enemy=None): self.color = color self.board = board self.enemy = enemy self.king = None self.pieces = [] for piece in pieces: match = re.match(notation, piece) title = match.group('title') pos = match.group('pos') if title is None: self.pieces.append(Pawn(self, pos, board)) else: if title in 'Rr': self.pieces.append(Rook(self, pos, board)) elif title in 'Nn': self.pieces.append(Knight(self, pos, board)) elif title in 'Bb': self.pieces.append(Bishop(self, pos, board)) elif title in 'Qq': self.pieces.append(Queen(self, pos, board)) elif title in 'Kk': self.pieces.append(King(self, pos, board)) self.king = self.pieces[-1]
def test_king_blocked(): b = Board() for pos in [("C", 8), ("C", 7), ("E", 7), ("D", 6), ("E", 6)]: p = Pawn("BLACK") p.set_position(pos) b.pieces.append(p) k = King("BLACK") k.set_position(("D", 7)) b.pieces.append(k) k.find_available_moves(b) moves = k.available_moves assert (len(moves) == 3) assert (("D", 7) not in moves) assert (("D", 8) in moves) assert (("E", 8) in moves) assert (("C", 6) in moves)
def __init__(self, present, color='None', type='None'): self.present = present self.pos_x = 0 self.pos_y = 0 self.board_x = 0 self.board_y = 0 self.name = 'None' self.color = color if self.present == False: return if type == 'B': self.name = 'Bishop' self.type = Bishop.Bishop(self.color) elif type == 'R': self.name = 'Rook' self.type = Rook.Rook(self.color) elif type == 'K': self.name = 'King' self.type = King.King(self.color) elif type == 'N': self.name = 'Knight' self.type = Knight.Knight(self.color) elif type == 'P': self.name = 'Pawn' self.type = Pawn.Pawn(self.color) else: self.name = 'Queen' self.type = Queen.Queen(self.color)
def test_no_checkmate_king_takes(): """ Problem seen when game declared checkmate even though king could have taken the piece threatening it - check this is fixed. """ g = Game() g.clear() g.add_piece(King("BLACK"), ("H", 8)) g.add_piece(King("WHITE"), ("E", 1)) g.add_piece(Pawn("WHITE"), ("E", 2)) g.add_piece(Pawn("WHITE"), ("F", 2)) g.add_piece(Queen("BLACK"), ("D", 5)) g.update_all_pieces() g.next_player_turn() g.move(("D", 5), ("D", 1)) assert (not g.is_checkmate("WHITE"))
def undo_move(self, board): # for easier recognition frox, froy = self.move_from tox, toy = self.move_to if not self.scan_mode: print('undo') # if pawn Promotion is true if self.pawn_promo == 'completed': # convert piece back into pawn with all specifications pawn_id = getattr(board[tox][toy], 'id') pawn_move_count = getattr(board[tox][toy], 'move_count') pawn_move_history = getattr(board[tox][toy], 'move_num_history') board[tox][toy] = Pawn(colour=self.move_colour, piece_id=pawn_id, move_count=pawn_move_count, move_hist=pawn_move_history) # revert move piece's move count and move history board[tox][toy].increment_move_count(-1) board[tox][toy].delete_move() # revert move piece back to from location by switching to and from board[tox][toy], board[frox][froy] = board[frox][froy], board[tox][toy] # If the move was a left castle if self.move_type == 'lcastle': # delete rook move id and decrement move count if getattr(board[3][toy], 'str_rep') != '-': board[3][toy].increment_move_count(-1) board[3][toy].delete_move() # revert rook back to location board[0][toy], board[3][toy] = board[3][toy], board[0][toy] # else If the move was a right castle elif self.move_type == 'rcastle': # delete rook move id and decrement move count if getattr(board[5][toy], 'str_rep') != '-': board[5][toy].increment_move_count(-1) board[5][toy].delete_move() # revert rook back to location board[7][toy], board[5][toy] = board[5][toy], board[7][toy] # else If the move was an enpassant elif self.move_type == 'enpassant': # replace captured piece back to old location if self.move_colour == 'black': board[tox][toy - 1] = self.captured elif self.move_colour == 'white': board[tox][toy + 1] = self.captured # else if the move was a capture elif self.move_type == 'capture': # replace captured piece back to location board[tox][toy] = self.captured
def test_points_queen_escape(): g = Game() g.clear() g.add_piece(Pawn("BLACK"), ("C", 3)) g.add_piece(Queen("WHITE"), ("B", 2)) assert (len(g.board.pieces) == 2) g.update_all_pieces() player = BestNextPointsPlayer("WHITE") points = player.potential_points_for_move(g, "WHITE", (("B", 2), ("B", 3))) assert (points == 9)
def test_points_rook_threaten(): g = Game() g.clear() g.add_piece(Pawn("WHITE"), ("A", 3)) g.add_piece(Rook("BLACK"), ("B", 8)) assert (len(g.board.pieces) == 2) g.next_to_play = "BLACK" g.update_all_pieces() player = BestNextPointsPlayer("BLACK") points = player.potential_points_for_move(g, "BLACK", (("B", 8), ("B", 4))) assert (points == -5)
def reset(self): """ Start a new game """ self.board.pieces = [] for i in range(8): self.add_piece(Pawn("WHITE"), (COLNAMES[i], 2)) self.add_piece(Pawn("BLACK"), (COLNAMES[i], 7)) row_dict = {"WHITE": 1, "BLACK": 8} for colour, row in row_dict.items(): self.add_piece(King(colour), ("E", row)) self.add_piece(Queen(colour), ("D", row)) for col in ["A", "H"]: self.add_piece(Rook(colour), (col, row)) for col in ["B", "G"]: self.add_piece(Knight(colour), (col, row)) for col in ["C", "F"]: self.add_piece(Bishop(colour), (col, row)) self.next_to_play = "WHITE" self.history = [] self.update_all_pieces()
def test_black_pawn_init_moves(): b = Board() p = Pawn("BLACK") p.set_position(("A", 7)) p.find_available_moves(b) moves = p.available_moves assert (len(moves) == 2) assert (("A", 6) in moves) assert (("A", 5) in moves)
def test_white_pawn_init_moves(): b = Board() p = Pawn("WHITE") p.set_position(("A", 2)) p.find_available_moves(b) moves = p.available_moves assert (len(moves) == 2) assert (("A", 3) in moves) assert (("A", 4) in moves)
def set_board(self): # Create blank board self.board = [[Blank()] * 8 for i in range(8)] # set board by updating self.board self.board[0][0] = Rook('black') self.board[7][0] = Rook('black') self.board[1][0] = Knight('black') self.board[6][0] = Knight('black') self.board[2][0] = Bishop('black') self.board[5][0] = Bishop('black') self.board[3][0] = Queen('black') self.board[4][0] = King('black') self.board[0][1] = Pawn('black') self.board[1][1] = Pawn('black') self.board[2][1] = Pawn('black') self.board[3][1] = Pawn('black') self.board[4][1] = Pawn('black') self.board[5][1] = Pawn('black') self.board[6][1] = Pawn('black') self.board[7][1] = Pawn('black') self.board[0][7] = Rook('white') self.board[7][7] = Rook('white') self.board[1][7] = Knight('white') self.board[6][7] = Knight('white') self.board[2][7] = Bishop('white') self.board[5][7] = Bishop('white') self.board[3][7] = Queen('white') self.board[4][7] = King('white') self.board[0][6] = Pawn('white') self.board[1][6] = Pawn('white') self.board[2][6] = Pawn('white') self.board[3][6] = Pawn('white') self.board[4][6] = Pawn('white') self.board[5][6] = Pawn('white') self.board[6][6] = Pawn('white') self.board[7][6] = Pawn('white')
def test_pawn_end_of_board(): """ test that a pawn can't go off the end of the board. """ b = Board() wp = Pawn("WHITE") wp.set_position(("A", 8)) wp.find_available_moves(b) assert (len(wp.available_moves) == 0) bp = Pawn("BLACK") bp.set_position(("B", 1)) bp.find_available_moves(b) assert (len(bp.available_moves) == 0)
def test_pawn_threatens(): """ test that a pawn will threaten the two positions diagonally ahead """ b = Board() wp = Pawn("WHITE") wp.set_position(("A", 5)) b.pieces.append(wp) wp.find_positions_threatened(b) assert (len(wp.threatens) == 1) assert (("B", 6) in wp.threatens) bp = Pawn("BLACK") bp.set_position(("C", 7)) b.pieces.append(bp) bp.find_positions_threatened(b) assert (len(bp.threatens) == 2) assert (("B", 6) in bp.threatens) assert (("D", 6) in bp.threatens)
def test_pawn_take(): """ test that pawns diagonally ahead of one another have the potential take square in their list of available moves. """ b = Board() wp = Pawn("WHITE") wp.set_position(("A", 4)) wp2 = Pawn("WHITE") wp2.set_position(("A", 5)) bp = Pawn("BLACK") bp.set_position(("B", 5)) b.pieces.append(wp) b.pieces.append(wp2) b.pieces.append(bp) wp.find_available_moves(b) bp.find_available_moves(b) wmoves = wp.available_moves assert (len(wmoves) == 1) assert (("B", 5) in wmoves) bmoves = bp.available_moves assert (len(bmoves) == 3) assert (("A", 4) in bmoves)
def test_pawn_blocked(): b = Board() wp = Pawn("WHITE") wp.set_position(("A", 4)) bp = Pawn("BLACK") bp.set_position(("A", 5)) b.pieces.append(wp) b.pieces.append(bp) wp.find_available_moves(b) wmoves = wp.available_moves assert (len(wmoves) == 0) bp.find_available_moves(b) bmoves = bp.available_moves assert (len(bmoves) == 0)
def __init__(self, rows, cols): self.rows = rows self.cols = cols self.board = [[0 for x in range(cols)] for y in range(rows)] self.board[0][0] = Rook(0, 0, "b") self.board[0][1] = Knight(0, 1, "b") self.board[0][2] = Bishop(0, 2, "b") self.board[0][3] = Queen(0, 3, "b") self.board[0][4] = King(0, 4, "b") self.board[0][5] = Bishop(0, 5, "b") self.board[0][6] = Knight(0, 6, "b") self.board[0][7] = Rook(0, 7, "b") self.board[1][0] = Pawn(1, 0, "b") self.board[1][1] = Pawn(1, 1, "b") self.board[1][2] = Pawn(1, 2, "b") self.board[1][3] = Pawn(1, 3, "b") self.board[1][4] = Pawn(1, 4, "b") self.board[1][5] = Pawn(1, 5, "b") self.board[1][6] = Pawn(1, 6, "b") self.board[1][7] = Pawn(1, 7, "b") self.board[7][0] = Rook(7, 0, "w") self.board[7][1] = Knight(7, 1, "w") self.board[7][2] = Bishop(7, 2, "w") self.board[7][3] = Queen(7, 3, "w") self.board[7][4] = King(7, 4, "w") self.board[7][5] = Bishop(7, 5, "w") self.board[7][6] = Knight(7, 6, "w") self.board[7][7] = Rook(7, 7, "w") self.board[6][0] = Pawn(6, 0, "w") self.board[6][1] = Pawn(6, 1, "w") self.board[6][2] = Pawn(6, 2, "w") self.board[6][3] = Pawn(6, 3, "w") self.board[6][4] = Pawn(6, 4, "w") self.board[6][5] = Pawn(6, 5, "w") self.board[6][6] = Pawn(6, 6, "w") self.board[6][7] = Pawn(6, 7, "w") self.p1Name = "Player 1" self.p2Name = "Player 2" self.turn = "w"
def __init__(self): self.white_checked = False self.black_checked = False self.board = [[0 for j in range(8)] for i in range(8)] self.white_pieces = [] self.black_pieces = [] self.white_king_position = () self.black_king_position = () # making a chess board with 8x8(64) total squares for index in range(1, 9): for letter in range(1, 9): self.board[index - 1][letter - 1] = Square((letter, index)) # making a board with black and white chess pieces if index == 1: if letter in [1, 8]: self.board[index - 1][letter - 1].set_figure( Rook(True, (letter, index))) self.white_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter in [2, 7]: self.board[index - 1][letter - 1].set_figure( Knight(True, (letter, index))) self.white_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter in [3, 6]: self.board[index - 1][letter - 1].set_figure( Bishop(True, (letter, index))) self.white_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter == 4: self.board[index - 1][letter - 1].set_figure( Queen(True, (letter, index))) self.white_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter == 5: self.board[index - 1][letter - 1].set_figure( King(True, (letter, index))) self.white_king_position = (index, letter) self.white_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif index == 8: if letter in [1, 8]: self.board[index - 1][letter - 1].set_figure( Rook(False, (letter, index))) self.black_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter in [2, 7]: self.board[index - 1][letter - 1].set_figure( Knight(False, (letter, index))) self.black_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter in [3, 6]: self.board[index - 1][letter - 1].set_figure( Bishop(False, (letter, index))) self.black_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter == 4: self.board[index - 1][letter - 1].set_figure( Queen(False, (letter, index))) self.black_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif letter == 5: self.board[index - 1][letter - 1].set_figure( King(False, (letter, index))) self.black_king_position = (index, letter) self.black_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif index == 2: self.board[index - 1][letter - 1].set_figure( Pawn(True, (letter, index))) self.white_pieces.append( self.board[index - 1][letter - 1].get_figure()) elif index == 7: self.board[index - 1][letter - 1].set_figure( Pawn(False, (letter, index))) self.black_pieces.append( self.board[index - 1][letter - 1].get_figure())
def draw(self, screen): #Fill in with "None", for the reset button. if you remove this part, things become interesting #for i in range (0, 8): # for j in range (0, 8): # if self.pieces[i][j] != None: # del self.pieces[i][j] # self.pieces[i][j] = None self.pieces[0][0] = Rook('b', [0,0]) self.pieces[0][1] = Knight('b', [0, 1]) self.pieces[0][2] = Bishop('b', [0,2]) self.pieces[0][3] = Queen('b') self.pieces[0][4] = King('b') self.pieces[0][5] = Bishop('b', [0,5]) self.pieces[0][6] = Knight('b', [0,6]) self.pieces[0][7] = Rook('b', [0,7]) self.pieces[1][0] = Pawn('b', [1,0]) self.pieces[1][1] = Pawn('b', [1,1]) self.pieces[1][2] = Pawn('b', [1,2]) self.pieces[1][3] = Pawn('b', [1,3]) self.pieces[1][4] = Pawn('b', [1,4]) self.pieces[1][5] = Pawn('b', [1,5]) self.pieces[1][6] = Pawn('b', [1,6]) self.pieces[1][7] = Pawn('b', [1,7]) self.pieces[7][0] = Rook('w', [7,0]) self.pieces[7][1] = Knight('w', [7,1]) self.pieces[7][2] = Bishop('w', [7,2]) self.pieces[7][3] = Queen('w') self.pieces[7][4] = King('w') self.pieces[7][5] = Bishop('w', [7,5]) self.pieces[7][6] = Knight('w', [7,6]) self.pieces[7][7] = Rook('w', [7,7]) self.pieces[6][0] = Pawn('w', [6,0]) self.pieces[6][1] = Pawn('w', [6,1]) self.pieces[6][2] = Pawn('w', [6,2]) self.pieces[6][3] = Pawn('w', [6,3]) self.pieces[6][4] = Pawn('w', [6,4]) self.pieces[6][5] = Pawn('w', [6,5]) self.pieces[6][6] = Pawn('w', [6,6]) self.pieces[6][7] = Pawn('w', [6,7]) for r in self.board: #Draw board for s in r: screen.fill(s.color, s.pygameSquare) for c in self.pieces: #Fill in pieces for p in c: if p != None: screen.blit(p.symbol, (self.squareSize*c.index(p), self.squareSize*self.pieces.index(c)))
if x - oldx == 2: board[y][x - 1] = 2 * (piece // abs(piece)) board[y][x - 4] = 0 elif oldx - x == 2: board[y][x + 1] = 2 * (piece // abs(piece)) board[y][x + 3] = 0 # Gameloop rookcheck = Rook() bishopcheck = Bishop() knightcheck = Knight() kingcheck = King() pawncheck = Pawn() def main(): crashed = False clock = pygame.time.Clock() selected = None pos1, pos2 = None, None curr_piece = 0 incheck = False global gameboard, counter, white_king, black_king while not crashed: displayBoard(gameboard)
def pawns(color: Color) -> list: row = [] for i in range(8): row.append(Pawn(color, i + 8)) return row