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 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 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 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 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 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 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, 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 __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, 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 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_bishop_middle(): b = Board() for colour in ["WHITE", "BLACK"]: bish = Bishop(colour) bish.set_position(("D", 4)) bish.find_available_moves(b) moves = bish.available_moves assert (len(moves) == 13) assert (("A", 1) in moves) assert (("H", 8) in moves) assert (("G", 1) in moves) assert (("A", 7) in moves)
def test_bishop_corner(): b = Board() for colour in ["WHITE", "BLACK"]: bish = Bishop(colour) bish.set_position(("A", 1)) bish.find_available_moves(b) moves = bish.available_moves assert (len(moves) == 7) assert (("B", 2) in moves) assert (("C", 3) in moves) assert (("D", 4) in moves) assert (("E", 5) in moves) assert (("F", 6) in moves) assert (("G", 7) in moves) assert (("H", 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'
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 __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 test_bishop_block(): b = Board() wb1 = Bishop("WHITE") wb1.set_position(("A", 1)) wb2 = Bishop("WHITE") wb2.set_position(("B", 2)) b.pieces += [wb1, wb2] wb1.find_available_moves(b) wb2.find_available_moves(b) assert (len(wb1.available_moves) == 0) assert (len(wb2.available_moves) == 8) assert (("H", 8) in wb2.available_moves) assert (("C", 3) in wb2.available_moves) assert (("A", 3) in wb2.available_moves)
def test_bishop_take(): b = Board() wb = Bishop("WHITE") wb.set_position(("D", 4)) bb = Bishop("BLACK") bb.set_position(("B", 2)) b.pieces += [wb, bb] wb.find_available_moves(b) bb.find_available_moves(b) wmoves = wb.available_moves bmoves = bb.available_moves assert (len(wmoves) == 12) assert (("A", 1) not in wmoves) assert (("H", 8) in wmoves) assert (("G", 1) in wmoves) assert (("A", 7) in wmoves) assert (len(bmoves) == 5) assert (("D", 4) in bmoves) assert (("C", 1) in bmoves) assert (("C", 3) in bmoves) assert (("A", 1) in bmoves) assert (("A", 3) in bmoves)