def __init__(self, rows, cols): self.rows = rows self.cols = cols self.board = [[0 for x in range(8)] for _ 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')
def __init__(self): self.pieces = [] self.selected_idx = None self._player_name = None for i in range(8): self.pieces += [Pawn(i, 1, 'black')] self.pieces += [Pawn(i, 6, 'white')] self.pieces += [ Rook(0, 0, 'black'), Rook(7, 0, 'black'), Knight(1, 0, 'black'), Knight(6, 0, 'black'), Bishop(2, 0, 'black'), Bishop(5, 0, 'black'), Queen(3, 0, 'black'), King(4, 0, 'black'), Rook(0, 7, 'white'), Rook(7, 7, 'white'), Knight(1, 7, 'white'), Knight(6, 7, 'white'), Bishop(2, 7, 'white'), Bishop(5, 7, 'white'), Queen(3, 7, 'white'), King(4, 7, 'white'), ]
def test_castling_forbidden_rook_moved(self): '''Test that castling is forbidden once the rook moved.''' board = Board(10, 10) king = King(board.white) rook_l = Rook(board.white) rook_r = Rook(board.white) board[0, 7].piece = rook_l board[7, 7].piece = rook_r board[4, 7].piece = king moves = king.get_moves((4, 7), board) self.assertTrue(Castling("left",board.white) in moves) self.assertTrue(Castling("right",board.white) in moves) move = Move((0,7),(1,7)) self.assertTrue(move in rook_l.get_moves((0,7),board)) move.perform(board) moves = king.get_moves((4, 7), board) self.assertFalse(Castling("left",board.white) in moves) self.assertTrue(Castling("right",board.white) in moves) move = Move((7,7),(6,7)) self.assertTrue(move in rook_r.get_moves((7,7),board)) move.perform(board) moves = king.get_moves((4, 7), board) self.assertFalse(Castling("left",board.white) in moves) self.assertFalse(Castling("right",board.white) in moves)
def test_moves_blocked_ally(self): rook = Rook(self.white, (0, 0)) king = King(self.white, (0, 1)) self.board.add_piece(rook) self.board.add_piece(king) moves = list(rook.moves(self.board)) self.assertEquals(len(moves), 7, moves)
def test_moves_blocked_enemy(self): rook = Rook(self.white, (0, 0)) knight = Knight(self.black, (0, 1)) self.board.add_piece(rook) self.board.add_piece(knight) moves = list(rook.moves(self.board)) self.assertEquals(len(moves), 8, moves)
def test_moves_corner(self): king = King(self.white, (3, 3)) rook = Rook(self.white, (0, 0)) self.board.add_piece(king) self.board.add_piece(rook) moves = list(rook.moves(self.board)) self.assertEquals(len(moves), 14, moves)
def __init__(self): #setup board and pieces self.blackPieces = [] self.whitePieces = [] self.blackPieces.append(King(4, 0, BLACK)) self.blackPieces.append(Queen(3, 0, BLACK)) self.blackPieces.append(Bishop(2, 0, BLACK)) self.blackPieces.append(Bishop(5, 0, BLACK)) self.blackPieces.append(Knight(1, 0, BLACK)) self.blackPieces.append(Knight(6, 0, BLACK)) self.blackPieces.append(Rook(0, 0, BLACK)) self.blackPieces.append(Rook(7, 0, BLACK)) for i in range(0, 8): self.blackPieces.append(Pawn(i, 1, BLACK)) self.whitePieces.append(King(4, 7, WHITE)) self.whitePieces.append(Queen(3, 7, WHITE)) self.whitePieces.append(Bishop(2, 7, WHITE)) self.whitePieces.append(Bishop(5, 7, WHITE)) self.whitePieces.append(Knight(1, 7, WHITE)) self.whitePieces.append(Knight(6, 7, WHITE)) self.whitePieces.append(Rook(0, 7, WHITE)) self.whitePieces.append(Rook(7, 7, WHITE)) for i in range(0, 8): self.whitePieces.append(Pawn(i, 6, WHITE))
def test_cant_capture_diagonally(self): rook = Rook(color='WHITE') is_valid_capture = rook.validate_capture((4,2), (5,3)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (3,1)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (3,3)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (5,1)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (3,3)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (3,1)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (5,3)) self.assertFalse(is_valid_capture) is_valid_capture = rook.validate_capture((4,2), (3,1)) self.assertFalse(is_valid_capture)
def __init__(self): self.rows = common.DIMENSION self.cols = common.DIMENSION self.current_color = "white" self.board_inst = {} for row in range(self.rows): for col in range(self.cols): self.board_inst[row, col] = Empty(row, col, None) self.board_inst[0, 0] = Rook(0, 0, 'black') self.board_inst[0, 1] = Knight(0, 1, 'black') self.board_inst[0, 2] = Bishop(0, 2, 'black') self.board_inst[0, 3] = Queen(0, 3, 'black') self.board_inst[0, 4] = King(0, 4, 'black') self.board_inst[0, 5] = Bishop(0, 5, 'black') self.board_inst[0, 6] = Knight(0, 6, 'black') self.board_inst[0, 7] = Rook(0, 7, 'black') self.board_inst[7, 0] = Rook(7, 0, 'white') self.board_inst[7, 1] = Knight(7, 1, 'white') self.board_inst[7, 2] = Bishop(7, 2, 'white') self.board_inst[7, 3] = Queen(7, 3, 'white') self.board_inst[7, 4] = King(7, 4, 'white') self.board_inst[7, 5] = Bishop(7, 5, 'white') self.board_inst[7, 6] = Knight(7, 6, 'white') self.board_inst[7, 7] = Rook(7, 7, 'white') for i in range(common.DIMENSION): self.board_inst[1, i] = Pawn(1, i, 'black') self.board_inst[6, i] = Pawn(6, i, 'white')
def __init__(self, rows, cols): self.rows = rows self.cols = cols self.ready = False self.last = None self.copy = True self.board = [[0 for x in range(8)] for _ 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.turn = "w" self.time1 = 900 self.time2 = 900 self.storedTime1 = 0 self.storedTime2 = 0 self.winner = None self.startTime = time.time()
def __init__(self): self.state = [[EMPTY_SQUARE for y in range(BOARD_SIZE)] for x in range(BOARD_SIZE)] self.white_pieces = ROOK + HORSE + BISHOP + QUEEN + KING + PAWN self.black_pieces = self.white_pieces.lower() # White self.state[0] = [Rook(0, 0), Knight(0, 1), Bishop(0, 2), Queen(0, 3), King(0, 4), Bishop(0, 5), Knight(0, 6), Rook(0, 7)] self.state[1] = [Pawn(1, 0), Pawn(1, 1), Pawn(1, 2), Pawn(1, 3), Pawn(1, 4), Pawn(1, 5), Pawn(1, 6), Pawn(1, 7)] # Black self.state[6] = [Pawn(7, 0, False), Pawn(7, 1, False), Pawn(7, 2, False), Pawn(7, 3, False), Pawn(7, 4, False), Pawn(7, 5, False), Pawn(7, 6, False), Pawn(7, 7, False)] self.state[7] = [Rook(6, 0, False), Knight(6, 1, False), Bishop(6, 2, False), Queen(6, 3, False), King(6, 4, False), Bishop(6, 5, False), Knight(6, 6, False), Rook(6, 7, False)]
def __init__(self, player1, player2, graphic_interface, verbose = False) : self.board = [ Rook(Color.BLACK), Knight(Color.BLACK), Bishop(Color.BLACK), Queen(Color.BLACK), King(Color.BLACK), Bishop(Color.BLACK), Knight(Color.BLACK), Rook(Color.BLACK) ] self.board += [Pawn(Color.BLACK) for _ in range(8)] self.board += [Null() for _ in range(32)] self.board += [Pawn(Color.WHITE) for _ in range(8)] self.board += [ Rook(Color.WHITE), Knight(Color.WHITE), Bishop(Color.WHITE), Queen(Color.WHITE), King(Color.WHITE), Bishop(Color.WHITE), Knight(Color.WHITE), Rook(Color.WHITE) ] self.board_with_boundaries = [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, -1, -1, 8, 9, 10, 11, 12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, 23, -1, -1, 24, 25, 26, 27, 28, 29, 30, 31, -1, -1, 32, 33, 34, 35, 36, 37, 38, 39, -1, -1, 40, 41, 42, 43, 44, 45, 46, 47, -1, -1, 48, 49, 50, 51, 52, 53, 54, 55, -1, -1, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1 ,-1 ,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ] self.player1 = player1 self.player2 = player2 self.graphic_interface = graphic_interface self.selected_case = None self.selected_case_2 = None self.turn = Color.WHITE self.special_moves_authorization = [ True, #White castle True, #White castle (queen side) True, #Black castle True, #Black castle (queen side) -1, #White "en passant" capture -1 #Black "en passant" capture ] self.moves_played=[] self.end = False self.verbose = verbose
def __init__(self): self.board = ChessBoard(self) self.pieces = [Pawn(self, "white", item) for item in [i + "2" for i in "abcdefgh"]] self.pieces.extend([Pawn(self, "black", item) for item in [i + "7" for i in "abcdefgh"]]) self.pieces.extend([Rook(self, "white", item) for item in ("a1", "h1")]) self.pieces.extend([Rook(self, "black", item) for item in ("a8", "h8")]) self.pieces.extend([Bishop(self, "white", item) for item in ("c1", "f1")]) self.pieces.extend([Bishop(self, "black", item) for item in ("c8", "f8")]) self.pieces.extend([Knight(self, "white", item) for item in ("b1", "g1")]) self.pieces.extend([Knight(self, "black", item) for item in ("b8", "g8")]) self.pieces.extend([King(self, "white", "e1"), King(self, "black", "e8"), Queen(self, "white", "d1"), Queen(self, "black", "d8")]) self.players = []
def test_rook_moves(self): '''Test the rook's moves unblocked''' board = Board(10, 10) rook = Rook(board.white) i, j = 4, 4 board[i, j].piece = rook moves = rook.get_moves((i, j), board) self.assertTrue(Move((i,j),(i,0)) in moves) self.assertTrue(Move((i,j),(i,7)) in moves) self.assertTrue(Move((i,j),(0,j)) in moves) self.assertTrue(Move((i,j),(7,j)) in moves)
def __init__(self, rows, cols): self.rows = rows self.cols = cols self.turn = "w" self.winner = None self.last = None #player time self.time1 = 900 self.time2 = 900 """Places all the pieces on 2D Array Board & generate the board""" self.board = [[0 for x in range(8)] for _ 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[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") #faster way of setting pawns for colum in range(0, 8): self.board[1][colum] = Pawn(1, colum, "b") self.board[6][colum] = Pawn(6, colum, "w") #keep track of pieces move self.moveStack = [] self.turn = "w" self.time1 = 900 self.time2 = 900 self.storedTime1 = 0 self.storedTime2 = 0 self.winner = None self.startTime = time.time()
def __init__(self, grid, newBoard=False, whiteInCheck=False, blackInCheck=False): if (newBoard == True): self.mobility = 0 self.bMobility = 0 self.grid = [[None for x in range(0, 8)] for y in range(0, 8)] for i in range(0, 8): for j in range(0, 8): if (grid[i][j] == '_' or grid[i][j] == '-'): self.grid[i][j] = None elif (grid[i][j] == 'P'): self.grid[i][j] = Pawn(i, j, 'w', 'P') elif (grid[i][j] == 'p'): self.grid[i][j] = Pawn(i, j, 'b', 'p') elif (grid[i][j] == 'N'): self.grid[i][j] = Knight(i, j, 'w', 'N') elif (grid[i][j] == 'n'): self.grid[i][j] = Knight(i, j, 'b', 'n') elif (grid[i][j] == 'B'): self.grid[i][j] = Bishop(i, j, 'w', 'B') elif (grid[i][j] == 'b'): self.grid[i][j] = Bishop(i, j, 'b', 'b') elif (grid[i][j] == 'Q'): self.grid[i][j] = Queen(i, j, 'w', 'Q') elif (grid[i][j] == 'q'): self.grid[i][j] = Queen(i, j, 'b', 'q') elif (grid[i][j] == 'K'): self.grid[i][j] = King(i, j, 'w', 'K') elif (grid[i][j] == 'k'): self.grid[i][j] = King(i, j, 'b', 'k') elif (grid[i][j] == 'R'): self.grid[i][j] = Rook(i, j, 'w', 'R') elif (grid[i][j] == 'r'): self.grid[i][j] = Rook(i, j, 'b', 'r') self.whiteInCheck = whiteInCheck # change later maybe? self.blackInCheck = blackInCheck # change later maybe? else: self.mobility = 0 self.bMobility = 0 self.grid = grid self.whiteInCheck = whiteInCheck # change later maybe? self.blackInCheck = blackInCheck # change later maybe? self.evalValue = self.evaluationFunction()
def test_remove_piece(): square = Square('a', '1') piece = Rook(Colour.WHITE) square.set_piece(piece) square.remove_piece() assert square.piece is None assert piece.square is None
def __init__(self): self.cols = COLS self.rows = ROWS self.captured_piece = None self.turn = "white" self.en_passant_possibility = None self.board = [[0 for col in range(COLS)] for rows in range(ROWS)] # Black pieces self.board[0][0] = Rook(0, 0, "black") self.board[0][1] = Knight(0, 1, "black") self.board[0][2] = Bishop(0, 2, "black") self.board[0][3] = Queen(0, 3, "black") self.board[0][4] = King(0, 4, "black") self.board[0][5] = Bishop(0, 5, "black") self.board[0][6] = Knight(0, 6, "black") self.board[0][7] = Rook(0, 7, "black") # Black pawns self.board[1][0] = Pawn(1, 0, "black") self.board[1][1] = Pawn(1, 1, "black") self.board[1][2] = Pawn(1, 2, "black") self.board[1][3] = Pawn(1, 3, "black") self.board[1][4] = Pawn(1, 4, "black") self.board[1][5] = Pawn(1, 5, "black") self.board[1][6] = Pawn(1, 6, "black") self.board[1][7] = Pawn(1, 7, "black") # White pieces self.board[7][0] = Rook(7, 0, "white") self.board[7][1] = Knight(7, 1, "white") self.board[7][2] = Bishop(7, 2, "white") self.board[7][3] = Queen(7, 3, "white") self.board[7][4] = King(7, 4, "white") self.board[7][5] = Bishop(7, 5, "white") self.board[7][6] = Knight(7, 6, "white") self.board[7][7] = Rook(7, 7, "white") # White pawns self.board[6][0] = Pawn(6, 0, "white") self.board[6][1] = Pawn(6, 1, "white") self.board[6][2] = Pawn(6, 2, "white") self.board[6][3] = Pawn(6, 3, "white") self.board[6][4] = Pawn(6, 4, "white") self.board[6][5] = Pawn(6, 5, "white") self.board[6][6] = Pawn(6, 6, "white") self.board[6][7] = Pawn(6, 7, "white")
def generateGrid(): return { "whites": [Pawn(Vector(i, 6), "whites") for i in range(8)] + [Rook(Vector(0, 7), "whites"), Rook(Vector(7, 7), "whites")] + [Bishop(Vector(2, 7), "whites"), Bishop(Vector(5, 7), "whites")] + [Queen(Vector(3, 7), "whites")] + [Knight(Vector(1, 7), "whites"), Knight(Vector(6, 7), "whites")] + [King(Vector(4, 7), "whites")], "blacks": [Pawn(Vector(i, 1), "blacks") for i in range(8)] + [Rook(Vector(0, 0), "blacks"), Rook(Vector(7, 0), "blacks")] + [Bishop(Vector(2, 0), "blacks"), Bishop(Vector(5, 0), "blacks")] + [Queen(Vector(3, 0), "blacks")] + [Knight(Vector(1, 0), "blacks"), Knight(Vector(6, 0), "blacks")] + [King(Vector(4, 0), "blacks")] }
def colour_pieces(colour): pieces = [Pawn(colour) for _ in range(8)] pieces += [Rook(colour) for _ in range(2)] pieces += [Knight(colour) for _ in range(2)] pieces += [Bishop(colour) for _ in range(2)] pieces += [Queen(colour)] pieces += [King(colour)] return pieces
def promotion(self, pawnToPromote): from piece import Rook, Bishop, Queen, Knight print("Pawn {} on [{},{}] is going to be promoted".format(pawnToPromote.name, pawnToPromote.x_position, pawnToPromote.y_position)) if pawnToPromote.color == "White": while True: pieceType = input("Choose which type it will become: Queen, Knight, Rook, Bishop: ").upper() if pieceType in ["QUEEN", "KNIGHT", "ROOK", "BISHOP"]: break else: print("Incorrect type") else: pieceType = "QUEEN" if pieceType == "ROOK": numOfPieces = self.countPieceByType("Rook", pawnToPromote.color) if pawnToPromote.color == "White": piece = Rook("Rook", "WR"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) else: piece = Rook("Rook", "BR"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) if pieceType == "QUEEN": numOfPieces = self.countPieceByType("Queen", pawnToPromote.color) if pawnToPromote.color == "White": piece = Queen("Queen", "WQ"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) else: piece = Queen("Queen", "BQ"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) if pieceType == "BISHOP": numOfPieces = self.countPieceByType("Bishop", pawnToPromote.color) if pawnToPromote.color == "White": piece = Bishop("Bishop", "WB"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) else: piece = Bishop("Bishop", "BB"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) if pieceType == "KNIGHT": numOfPieces = self.countPieceByType("Knight", pawnToPromote.color) if pawnToPromote.color == "White": piece = Knight("Knight", "WK"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) else: piece = Knight("Knight", "BK"+str(numOfPieces+1), pawnToPromote.color, pawnToPromote.x_position, pawnToPromote.y_position, self, False, True) self.addPiece(piece) pawnToPromote.isActive = False
def __init__(self, rows, cols): self.rows = rows self.cols = cols self.board = [[0 for x in range(9)] for _ in range(rows)] self.board[0][0] = Rook(0, 0, "b") self.board[0][8] = Rook(0, 8, "b") self.board[0][1] = Knight(0, 1, "b") self.board[0][7] = Knight(0, 7, "b") self.board[0][2] = BlackElephant(0, 2, "b") self.board[0][6] = BlackElephant(0, 6, "b") self.board[0][3] = BlackGuard(0, 3, "b") self.board[0][5] = BlackGuard(0, 5, "b") self.board[0][4] = BlackKing(0, 4, "b") self.board[2][1] = Cannon(2, 1, "b") self.board[2][7] = Cannon(2, 7, "b") self.board[3][0] = BlackPawn(3, 0, "b") self.board[3][2] = BlackPawn(3, 2, "b") self.board[3][4] = BlackPawn(3, 4, "b") self.board[3][6] = BlackPawn(3, 6, "b") self.board[3][8] = BlackPawn(3, 8, "b") self.board[9][0] = Rook(9, 0, "r") self.board[9][8] = Rook(9, 8, "r") self.board[9][1] = Knight(9, 1, "r") self.board[9][7] = Knight(9, 7, "r") self.board[9][2] = RedElephant(9, 2, "r") self.board[9][6] = RedElephant(9, 6, "r") self.board[9][3] = RedGuard(9, 3, "r") self.board[9][5] = RedGuard(9, 5, "r") self.board[9][4] = RedKing(9, 4, "r") self.board[7][1] = Cannon(7, 1, "r") self.board[7][7] = Cannon(7, 7, "r") self.board[6][0] = RedPawn(6, 0, "r") self.board[6][2] = RedPawn(6, 2, "r") self.board[6][4] = RedPawn(6, 4, "r") self.board[6][6] = RedPawn(6, 6, "r") self.board[6][8] = RedPawn(6, 8, "r")
def test_castling_permitted_after_undo(self): board = Board(10, 10) king = King(board.white) rook_l = Rook(board.white) rook_r = Rook(board.white) board[0, 7].piece = rook_l board[7, 7].piece = rook_r board[4, 7].piece = king moves = king.get_moves((4, 7), board) self.assertTrue(Castling("left",board.white) in moves) self.assertTrue(Castling("right",board.white) in moves) move = Move((0,7),(1,7)) self.assertTrue(move in rook_l.get_moves((0,7),board)) move.perform(board) move.undo(board) moves = king.get_moves((4, 7), board) self.assertTrue(Castling("left",board.white) in moves) self.assertTrue(Castling("right",board.white) in moves)
def __init__(self): self.board = [[0 for x in range(8)] for _ in range(8)] self.board[0][0] = Rook("a8", "b", "ROOKB1") self.board[0][1] = Knight("b8", "b", "KNIGHTB1") self.board[0][2] = Bishop("c8", "b", "BISHOPB1") self.board[0][3] = Queen("d8", "b", "QUEENB") self.board[0][4] = King("e8", "b", "KINGB") self.board[0][5] = Bishop("f8", "b", "BISHOPB2") self.board[0][6] = Knight("g8", "b", "KNIGHTB2") self.board[0][7] = Rook("h8", "b", "ROOKB2") self.board[1][0] = Pawn("a7", "b", "PAWNBA") self.board[1][1] = Pawn("b7", "b", "PAWNBB") self.board[1][2] = Pawn("c7", "b", "PAWNBC") self.board[1][3] = Pawn("d7", "b", "PAWNBD") self.board[1][4] = Pawn("e7", "b", "PAWNBE") self.board[1][5] = Pawn("f7", "b", "PAWNBF") self.board[1][6] = Pawn("g7", "b", "PAWNBG") self.board[1][7] = Pawn("h7", "b", "PAWNBH") self.board[7][0] = Rook("a1", "w", "ROOKW1") self.board[7][1] = Knight("b1", "w", "KNIGHTW1") self.board[7][2] = Bishop("c1", "w", "BISHOPW1") self.board[7][3] = Queen("d1", "w", "QUEENW") self.board[7][4] = King("e1", "w", "KINGW") self.board[7][5] = Bishop("f1", "w", "BISHOPW2") self.board[7][6] = Knight("g1", "w", "KNIGHTW2") self.board[7][7] = Rook("h1", "w", "ROOKW2") self.board[6][0] = Pawn("a2", "w", "PAWNWA") self.board[6][1] = Pawn("b2", "w", "PAWNWB") self.board[6][2] = Pawn("c2", "w", "PAWNWC") self.board[6][3] = Pawn("d2", "w", "PAWNWD") self.board[6][4] = Pawn("e2", "w", "PAWNWE") self.board[6][5] = Pawn("f2", "w", "PAWNWF") self.board[6][6] = Pawn("g2", "w", "PAWNWG") self.board[6][7] = Pawn("h2", "w", "PAWNWH")
def piece_setup(self): for i in [0, 1]: for k in range(8): self.place(Pawn(0, k, 6)) self.place(Pawn(1, k, 1)) if i == 0: j = 7 else: j = 0 self.place(Rook(i, 0, j)) self.place(Rook(i, 7, j)) self.place(Knight(i, 1, j)) self.place(Knight(i, 6, j)) self.place(Bishop(i, 2, j)) self.place(Bishop(i, 5, j)) self.place(Queen(i, 3, j)) self.place(King(i, 4, j)) self.move_stack = []
def test_rook(): current_square = Square('e', '4') piece = Rook(Colour.WHITE) current_square.set_piece(piece) to_square = Square('e', '8') assert piece.valid_change(to_square, is_capture=False) to_square = Square('e', '1') assert piece.valid_change(to_square, is_capture=False) to_square = Square('a', '4') assert piece.valid_change(to_square, is_capture=False) to_square = Square('h', '4') assert piece.valid_change(to_square, is_capture=False) to_square = Square('f', '5') assert not piece.valid_change(to_square, is_capture=False)
def test_rook_cant_move_L_shapes(self): rook = Rook('WHITE') is_valid_move = rook.validate_move((1,3),(3,2)) self.assertFalse(is_valid_move) is_valid_move = rook.validate_move((1,3),(2,5)) self.assertFalse(is_valid_move) is_valid_move = rook.validate_move((2,3), (1,5)) self.assertFalse(is_valid_move) is_valid_move = rook.validate_move((2,3), (3,5)) self.assertFalse(is_valid_move) is_valid_move = rook.validate_move((2,3), (4,4)) self.assertFalse(is_valid_move)
def factory(piece, color): piece = piece.lower() color = color.lower() color = 'w' if color == 'white' else 'b' if piece == 'pawn': return Pawn(color) elif piece == 'rook': return Rook(color) elif piece == 'knight': return Knight(color) elif piece == 'bishop': return Bishop(color) elif piece == 'queen': return Queen(color) elif piece == 'king': return King(color)
def loadGame(self): import json from piece import Piece, Pawn, Rook, Bishop, Queen, Knight, King with open("save.json", "r") as saveFile: data = json.load(saveFile) for key in data: if key == "turns": self.cb.turns = data[key] if key == "lastMove": self.cb.lastMove["pieceName"] = data[key]["pieceName"] self.cb.lastMove["type"] = data[key]["type"] self.cb.lastMove["color"] = data[key]["color"] self.cb.lastMove["origX"] = data[key]["origX"] self.cb.lastMove["origY"] = data[key]["origY"] self.cb.lastMove["futureX"] = data[key]["futureX"] self.cb.lastMove["futureY"] = data[key]["futureY"] if key != "turns" and key != "lastMove": if data[key]["type"] == "Pawn": piece = Pawn(data[key]["type"], data[key]["name"], data[key]["color"], data[key]["x_position"], data[key]["y_position"], self.cb, data[key]["initialMoveNotDone"], data[key]["isActive"]) if data[key]["type"] == "Rook": piece = Rook(data[key]["type"], data[key]["name"], data[key]["color"], data[key]["x_position"], data[key]["y_position"], self.cb, data[key]["initialMoveNotDone"], data[key]["isActive"]) if data[key]["type"] == "Knight": piece = Knight(data[key]["type"], data[key]["name"], data[key]["color"], data[key]["x_position"], data[key]["y_position"], self.cb, data[key]["initialMoveNotDone"], data[key]["isActive"]) if data[key]["type"] == "Bishop": piece = Bishop(data[key]["type"], data[key]["name"], data[key]["color"], data[key]["x_position"], data[key]["y_position"], self.cb, data[key]["initialMoveNotDone"], data[key]["isActive"]) if data[key]["type"] == "Queen": piece = Queen(data[key]["type"], data[key]["name"], data[key]["color"], data[key]["x_position"], data[key]["y_position"], self.cb, data[key]["initialMoveNotDone"], data[key]["isActive"]) if data[key]["type"] == "King": piece = King(data[key]["type"], data[key]["name"], data[key]["color"], data[key]["x_position"], data[key]["y_position"], self.cb, data[key]["initialMoveNotDone"], data[key]["isActive"]) self.cb.addPiece(piece)
def copy(self): return Rook(self.pos, self.color, self.rotate)
def __init__(self, pos, color, size, rotate=False): img = 'rook-{}.png'.format(color) Rook.__init__(self, pos, color, rotate) DrawablePiece.__init__(self, pos, color, img, size, rotate)
def test_attacked_locations(self): rook = Rook(Location(5, 'd'), Player.BLACK, self.board) results = {Location(4, 'd'), Location(3, 'd'), Location(2, 'd'), Location(1, 'd'), Location(6, 'd'), Location(7, 'd'), Location(8, 'd'), Location(5, 'c'), Location(5, 'b'), Location(5, 'a'), Location(5, 'e'), Location(5, 'f'), Location(5, 'g'), Location(5, 'h')} self.assertEquals(set(rook.attacked_locations()), results)
def test_can_capture_backward(self): rook = Rook(color='WHITE') is_valid_capture = rook.validate_capture((4,2), (7,2)) self.assertTrue(is_valid_capture)
def test_can_capture_sideways(self): rook = Rook(color='BLACK') is_valid_capture = rook.validate_capture((7,2), (7,4)) self.assertTrue(is_valid_capture)
def test_can_move_forward(self): rook = Rook(color='BLACK') is_valid_move = rook.validate_move((7,2),(4,2)) self.assertTrue(is_valid_move)
def _row_of_rook_bishop(color): return ["", Rook(color), "", "", "", "", "", Bishop(color), ""]
def test_set_piece(): square = Square('a', '1') piece = Rook(Colour.WHITE) square.set_piece(piece) assert square.piece is piece assert piece.square is square