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 pawn_queen_check(self,new_row,new_col): # Pawns turning into Queens if new_row == 7 and isinstance(self.piece_array[new_row][new_col],Pawn) and self.piece_array[new_row][new_col].color == PieceColour.WHITE: self.piece_array[new_row][new_col] = Queen(PieceColour.WHITE,self.gameMaster) self.button_identities[7][new_col].config(image=self.pictures["Queen"]) elif new_row == 0 and isinstance(self.piece_array[new_row][new_col],Pawn) and self.piece_array[new_row][new_col].color == PieceColour.BLACK: self.piece_array[new_row][new_col] = Queen(PieceColour.BLACK,self.gameMaster) self.button_identities[0][new_col].config(image=self.pictures["QueenFilled"])
def which_move(self, player_location, new_location): player = self.chessboard[player_location[1]][player_location[0]] temp_pos = [player.xpos, player.ypos] obj_or_not = self.chessboard[new_location[1]][new_location[0]] if isinstance( obj_or_not, Piece) and obj_or_not.colour == player.colour: # This is wrong return [False] if player.is_move_valid(before=temp_pos, after=new_location, board=self.chessboard): if isinstance(obj_or_not, Piece): # If it is king that is killed, Game over if obj_or_not.name == "king": self.game_over = [True, player.name, player.colour] self.killed_pieces.append(obj_or_not.icon) self.chess_pieces.remove(obj_or_not) if player.name == "pawn": if player.colour == "white": if new_location[1] == 8: self.chess_pieces.append( Queen(colour="white", xpos=new_location[0], ypos=new_location[1])) self.chess_pieces.remove(player) self.update_board() return [True, "queen", "white"] else: if new_location[1] == 1: self.chess_pieces.append( Queen(colour="black", xpos=new_location[0], ypos=new_location[1])) self.chess_pieces.remove(player) self.update_board() return [True, "queen", "black"] player.xpos, player.ypos = new_location[0], new_location[ 1] # update board self.update_board() return [True, player.name, player.colour] else: return [False]
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 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 movePiece(self, piece, initPos, finalPos): #Aupassau, there is a bug, you can't do aupassau anymore if type(piece) == Pawn and self.pieces[finalPos[0]][finalPos[1]] == None: if piece.color == 'w': otherPiece = self.pieces[finalPos[0]+1][finalPos[1]] if type(otherPiece) == Pawn: if otherPiece.aupassauAvail: self.pieces[finalPos[0]+1][finalPos[1]] = None else: otherPiece = self.pieces[finalPos[0]-1][finalPos[1]] if type(otherPiece) == Pawn: if otherPiece.aupassauAvail: self.pieces[finalPos[0]-1][finalPos[1]] = None #Disable aupassau on all other pieces for r in self.pieces: for p in r: if type(p) == Pawn and p != piece: p.aupassauAvail = False #Castling if type(piece) == King and abs(finalPos[1] - initPos[1]) > 1: #Right if finalPos[1] - initPos[1] > 0: rook = self.pieces[piece.pos[0]][7] rook.move([piece.pos[0],5]) self.pieces[piece.pos[0]][5] = rook self.pieces[piece.pos[0]][7] = None #Left else: rook = self.pieces[piece.pos[0]][0] rook.move([piece.pos[0],3]) self.pieces[piece.pos[0]][3] = rook self.pieces[piece.pos[0]][0] = None takenPiece = self.pieces[finalPos[0]][finalPos[1]] self.pieces[finalPos[0]][finalPos[1]] = piece self.pieces[initPos[0]][initPos[1]] = None piece.move(finalPos) #Promotion if type(piece) == Pawn and (piece.pos[0] == 0 or piece.pos[0] == 7): q = Queen(piece.color) self.pieces[finalPos[0]][finalPos[1]] = q q.move(finalPos) return takenPiece
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_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_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 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 promote(self, start_pos, end_pos): """ Replace a pawn by a queen. Assume we have already checked it is a legal move. """ if self.board.is_empty(start_pos): return False pawn = self.board.piece_at(start_pos) colour = pawn.colour self.board.pieces.remove(pawn) self.add_piece(Queen(colour), end_pos) return True
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_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 test_queen_corner(): b = Board() for colour in ["WHITE", "BLACK"]: q = Queen(colour) q.set_position(("A", 1)) q.find_available_moves(b) moves = q.available_moves assert (len(moves) == 21) assert (("B", 2) in moves) assert (("A", 8) in moves) assert (("H", 1) 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 test_queen_middle(): b = Board() for colour in ["WHITE", "BLACK"]: q = Queen(colour) q.set_position(("D", 4)) q.find_available_moves(b) moves = q.available_moves assert (len(moves) == 27) assert (("A", 1) in moves) assert (("H", 8) in moves) assert (("G", 1) in moves) assert (("A", 7) in moves) assert (("A", 4) in moves) assert (("H", 4) in moves) assert (("D", 1) in moves) assert (("D", 8) in moves)
def __init__(self): words = ["a", "b", "c", "d", "e", "f", "g", "h"] i = 0 while i < 8: self.width[words[i]] = i i = i + 1 self.grid.insert( 0, Rooks("w", self.gridtochess(0)[0], self.gridtochess(0)[1], self.grid)) self.grid.insert( 1, Knights("w", self.gridtochess(1)[0], self.gridtochess(1)[1], self.grid)) self.grid.insert( 2, Bishops("w", self.gridtochess(2)[0], self.gridtochess(2)[1], self.grid)) self.grid.insert( 3, Queen("w", self.gridtochess(3)[0], self.gridtochess(3)[1], self.grid)) self.grid.insert( 4, King("w", self.gridtochess(4)[0], self.gridtochess(4)[1], self.grid)) self.grid.insert( 5, Bishops("w", self.gridtochess(5)[0], self.gridtochess(5)[1], self.grid)) self.grid.insert( 6, Knights("w", self.gridtochess(6)[0], self.gridtochess(6)[1], self.grid)) self.grid.insert( 7, Rooks("w", self.gridtochess(7)[0], self.gridtochess(7)[1], self.grid)) i = 8 while i < 16: self.grid.insert( i, Pawns("w", self.gridtochess(i)[0], self.gridtochess(i)[1], self.grid)) # print(Pieces.gridtochess(i)) i = i + 1 while i < 48: self.grid.insert( i, BoardPiece( self.gridtochess(i)[0], self.gridtochess(i)[1], self.grid)) i = i + 1 while i < 56: self.grid.insert( i, Pawns("b", self.gridtochess(i)[0], self.gridtochess(i)[1], self.grid)) i = i + 1 self.grid.insert( 56, Rooks("b", self.gridtochess(56)[0], self.gridtochess(56)[1], self.grid)) self.grid.insert( 57, Knights("b", self.gridtochess(57)[0], self.gridtochess(57)[1], self.grid)) self.grid.insert( 58, Bishops("b", self.gridtochess(58)[0], self.gridtochess(58)[1], self.grid)) self.grid.insert( 59, Queen("b", self.gridtochess(59)[0], self.gridtochess(59)[1], self.grid)) self.grid.insert( 60, King("b", self.gridtochess(60)[0], self.gridtochess(60)[1], self.grid)) self.grid.insert( 61, Bishops("b", self.gridtochess(61)[0], self.gridtochess(61)[1], self.grid)) self.grid.insert( 62, Knights("b", self.gridtochess(62)[0], self.gridtochess(62)[1], self.grid)) self.grid.insert( 63, Rooks("b", self.gridtochess(63)[0], self.gridtochess(63)[1], self.grid))
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_queen_take_vert(): b = Board() wq = Queen("WHITE") wq.set_position(("D", 4)) bq = Queen("BLACK") bq.set_position(("B", 4)) b.pieces += [wq, bq] wq.find_available_moves(b) bq.find_available_moves(b) wmoves = wq.available_moves bmoves = bq.available_moves assert (len(wmoves) == 26) assert (("A", 4) not in wmoves) assert (("A", 1) in wmoves) assert (("H", 8) in wmoves) assert (("G", 1) in wmoves) assert (("A", 7) in wmoves) assert (len(bmoves) == 19) assert (("D", 4) in bmoves) assert (("C", 4) in bmoves) assert (("C", 3) in bmoves) assert (("A", 4) in bmoves) assert (("A", 3) in bmoves)
def test_queen_block(): b = Board() wq1 = Queen("WHITE") wq1.set_position(("A", 1)) wq2 = Queen("WHITE") wq2.set_position(("B", 2)) b.pieces += [wq1, wq2] wq1.find_available_moves(b) wq2.find_available_moves(b) assert (len(wq1.available_moves) == 14) assert (("A", 8) in wq1.available_moves) assert (("H", 1) in wq1.available_moves) assert (len(wq2.available_moves) == 22) assert (("H", 8) in wq2.available_moves) assert (("C", 3) in wq2.available_moves) assert (("A", 3) in wq2.available_moves) assert (("B", 8) in wq2.available_moves) assert (("H", 2) in wq2.available_moves)