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 loadMajorPieces(self): self.matrix[0][0] = Rook('BLACK', 0, 0) self.matrix[7][0] = Rook('BLACK', 7, 0) self.matrix[1][0] = Knight('BLACK', 1, 0) self.matrix[6][0] = Knight('BLACK', 6, 0) self.matrix[2][0] = Bishop('BLACK', 2, 0) self.matrix[5][0] = Bishop('BLACK', 5, 0) self.matrix[3][0] = Queen('BLACK', 3, 0) self.matrix[4][0] = King('BLACK', 4, 0) self.matrix[0][7] = Rook('WHITE', 0, 7) self.matrix[7][7] = Rook('WHITE', 7, 7) self.matrix[1][7] = Knight('WHITE', 1, 7) self.matrix[6][7] = Knight('WHITE', 6, 7) self.matrix[2][7] = Bishop('WHITE', 2, 7) self.matrix[5][7] = Bishop('WHITE', 5, 7) self.matrix[3][7] = Queen('WHITE', 3, 7) self.matrix[4][7] = King('WHITE', 4, 7) self.black_king = self.matrix[4][0] self.white_king = self.matrix[4][7] for i in range(8): black_piece = self.matrix[i][0] white_piece = self.matrix[i][7] self.black_pieces.append(black_piece) self.white_pieces.append(white_piece)
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 preset1(self): preset1 = [] preset1.append(King.King((0, 0), False)) preset1.append(Rook.Rook((7, 1), True)) preset1.append(King.King((7, 7), True)) preset1.append(Rook.Rook((4, 4), True)) return preset1
def first_row(color: Color) -> list: return [ Rook(color, 0), Knight(color, 1), Bishop(color, 2), Queen(color, 3), King(color, 4), Bishop(color, 5), Knight(color, 6), Rook(color, 7) ]
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 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)))
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 __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 promote(self, selectedPiece, currentPlayer): select = "" print("\nPromoting", selectedPiece.pos, "Pawn:") print( "1) Promote to Knight.\n2) Promote to Bishop\n3) Promote to Rook\n4) Promote to Queen" ) while not select: try: select = int(input(": ")) if not 1 <= select <= 4: raise ValueError except ValueError: print("Invalid input. Must be an integer between 1 and 4.\n") select = "" if select == 1: newPiece = Knight(selectedPiece.color, selectedPiece.pos) newPiece.timesMoved = selectedPiece.timesMoved elif select == 2: newPiece = Bishop(selectedPiece.color, selectedPiece.pos) newPiece.timesMoved = selectedPiece.timesMoved elif select == 3: newPiece = Rook(selectedPiece.color, selectedPiece.pos) newPiece.timesMoved = selectedPiece.timesMoved else: newPiece = Queen(selectedPiece.color, selectedPiece.pos) newPiece.timesMoved = selectedPiece.timesMoved currentPlayer.piecesAlive.remove(selectedPiece) currentPlayer.piecesAlive.append(newPiece)
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 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 test_checkmate(): g = Game() g.clear() g.add_piece(King("BLACK"), ("H", 8)) g.add_piece(Queen("WHITE"), ("G", 7)) g.add_piece(Rook("WHITE"), ("G", 6)) assert (len(g.board.pieces) == 3) g.update_all_pieces() g.next_player_turn() assert (g.is_check("BLACK")) assert (g.is_checkmate("BLACK"))
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_rook_on_edge(): b = Board() for colour in ["WHITE", "BLACK"]: r = Rook(colour) r.set_position(("F", 1)) r.find_available_moves(b) moves = r.available_moves assert (len(moves) == 14) assert (("A", 1) in moves) assert (("H", 1) in moves) assert (("F", 8) in moves)
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 make_pawn_promo(self, piece_type, board): # Set as for easier use tox, toy = self.move_to # Make a new piece with same ID, moves history, move history count if piece_type == 'Queen': new_piece = Queen(colour=self.move_colour, move_count=getattr(board[tox][toy], 'move_count'), move_hist=getattr(board[tox][toy], 'move_num_history'), piece_id=getattr(board[tox][toy], 'id')) elif piece_type == 'Rook': new_piece = Rook(colour=self.move_colour, move_count=getattr(board[tox][toy], 'move_count'), move_hist=getattr(board[tox][toy], 'move_num_history'), piece_id=getattr(board[tox][toy], 'id')) elif piece_type == 'Knight': new_piece = Knight(colour=self.move_colour, move_count=getattr(board[tox][toy], 'move_count'), move_hist=getattr(board[tox][toy], 'move_num_history'), piece_id=getattr(board[tox][toy], 'id')) elif piece_type == 'Bishop': new_piece = Bishop(colour=self.move_colour, move_count=getattr(board[tox][toy], 'move_count'), move_hist=getattr(board[tox][toy], 'move_num_history'), piece_id=getattr(board[tox][toy], 'id')) else: print("Error, wrong piece choice") return -1 # Place in old piece's spot board[tox][toy] = new_piece self.pawn_promo = 'completed'
return True def castle(y, x, oldx, piece, board): 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
def test_rook_take(): b = Board() r1 = Rook("WHITE") r1.set_position(("B", 2)) r2 = Rook("BLACK") r2.set_position(("F", 2)) b.pieces += [r1, r2] r1.find_available_moves(b) r2.find_available_moves(b) assert (len(r1.available_moves) == 12) assert (len(r2.available_moves) == 13)
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())