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 test_cannot_move_diagonally(self): knight = Knight(color='WHITE') is_valid_move = knight.validate_move((4, 2), (5, 3)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (3, 1)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (3, 3)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (5, 1)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (1, 3)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (7, 1)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (7, 3)) self.assertFalse(is_valid_move) is_valid_move = knight.validate_move((4, 2), (1, 1)) self.assertFalse(is_valid_move)
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 __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): 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 __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 test_knight_near_edge(self): '''Test a knight's moves when near an edge of the board''' board = Board(10, 10) knight = Knight(board.white) i, j = 0, 4 board[i, j].piece = knight moves = knight.get_moves((i, j), board) self.assertEquals(len(moves), 4)
def test_basic_knight_moves(self): '''Test a knight's basic moves''' board = Board(10, 10) knight = Knight(board.white) i, j = 3, 4 board[i, j].piece = knight moves = knight.get_moves((i, j), board) self.assertEquals(len(moves), 8)
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 _row_of_king(color): return [ Lance(color), Knight(color), SilverGeneral(color), GoldGeneral(color), King(color), GoldGeneral(color), SilverGeneral(color), Knight(color), Lance(color) ]
class PieceKnight(unittest.TestCase): def setUp(self) -> None: self.k = Knight('b') def test_knight_color(self): self.assertEqual(self.k.color, Color.BLACK) def test_knight_validate_vector(self): self.assertFalse(self.k.validate_vector([1, 4])) self.assertFalse(self.k.validate_vector([1, 3])) self.assertTrue(self.k.validate_vector([1, 2])) self.assertTrue(self.k.validate_vector([2, 1])) self.assertTrue(self.k.validate_vector([1, -2]))
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 __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 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 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 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 __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_knight_can_capture(self): knight = Knight('WHITE') is_valid_capture = knight.validate_capture((1, 3), (3, 2)) self.assertTrue(is_valid_capture) is_valid_capture = knight.validate_capture((1, 3), (2, 5)) self.assertTrue(is_valid_capture) is_valid_capture = knight.validate_capture((2, 3), (1, 5)) self.assertTrue(is_valid_capture) is_valid_capture = knight.validate_capture((2, 3), (3, 5)) self.assertTrue(is_valid_capture) is_valid_capture = knight.validate_capture((2, 3), (4, 4)) self.assertTrue(is_valid_capture)
def test_knight_can_move_L_shapes(self): knight = Knight('WHITE') is_valid_move = knight.validate_move((1, 3), (3, 2)) self.assertTrue(is_valid_move) is_valid_move = knight.validate_move((1, 3), (2, 5)) self.assertTrue(is_valid_move) is_valid_move = knight.validate_move((2, 3), (1, 5)) self.assertTrue(is_valid_move) is_valid_move = knight.validate_move((2, 3), (3, 5)) self.assertTrue(is_valid_move) is_valid_move = knight.validate_move((2, 3), (4, 4)) self.assertTrue(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 test_cant_move_backward(self): knight = Knight(color='BLACK') is_valid_move = knight.validate_move((4, 2), (7, 2)) self.assertFalse(is_valid_move)
def test_cant_move_sideways(self): knight = Knight(color='BLACK') is_valid_move = knight.validate_move((7, 2), (7, 4)) self.assertFalse(is_valid_move)
def test_knight_can_jump(self): self.k = Knight('w') self.assertTrue(self.k.can_jump())
def test_moves_corner(self): knight = Knight(self.white, (0, 0)) moves = list(knight.moves(self.board)) self.assertEquals(len(moves), 2)
def setUp(self) -> None: self.k = Knight('b')
def copy(self): return Knight(self.pos, self.color, self.rotate)
def __init__(self, pos, color, size, rotate=False): img = 'knight-{}.png'.format(color) Knight.__init__(self, pos, color, rotate) DrawablePiece.__init__(self, pos, color, img, size, rotate)
def test_attacked_locations(self): knight = Knight(Location(5, 'd'), Player.BLACK, self.board) results = {Location(7, 'c'), Location(6, 'b'), Location(4, 'b'), Location(3, 'c'), Location(3, 'e'), Location(4, 'f'), Location(6, 'f'), Location(7, 'e')} self.assertEquals(set(knight.attacked_locations()), results)
def test_moves_center(self): knight = Knight(self.white, (4, 4)) moves = list(knight.moves(self.board)) self.assertEquals(len(moves), 8)
class PieceCanJump(unittest.TestCase): def test_knight_can_jump(self): self.k = Knight('w') self.assertTrue(self.k.can_jump())
def test_moves_blocked_enemy(self): knight = Knight(self.white, (0, 0)) bishop = Bishop(self.black, (1, 2)) self.board.add_piece(bishop) moves = list(knight.moves(self.board)) self.assertEquals(len(moves), 2)
def test_can_reach_normal(self): knight = Knight(self.white, (0, 0)) self.assertTrue(knight.can_reach(self.board, (1, 2)))