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_moves_blocked_enemy(self): bishop = Bishop(self.white, (0, 0)) knight = Knight(self.black, (1, 1)) self.board.add_piece(bishop) self.board.add_piece(knight) moves = list(bishop.moves(self.board)) self.assertEquals(len(moves), 1, moves)
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 test_can_capture_diagonally(self): bishop = Bishop(color='WHITE') is_valid_capture = bishop.validate_capture((4, 2), (5, 3)) self.assertTrue(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (3, 1)) self.assertTrue(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (3, 3)) self.assertTrue(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (5, 1)) self.assertTrue(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (3, 1)) self.assertTrue(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (7, 1)) self.assertFalse(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (7, 3)) self.assertFalse(is_valid_capture) is_valid_capture = bishop.validate_capture((4, 2), (3, 1)) self.assertTrue(is_valid_capture)
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, 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 __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): 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_bishop(): current_square = Square('e', '4') piece = Bishop(Colour.WHITE) current_square.set_piece(piece) to_square = Square('h', '1') assert piece.valid_change(to_square, is_capture=False) to_square = Square('b', '1') assert piece.valid_change(to_square, is_capture=False) to_square = Square('a', '8') assert piece.valid_change(to_square, is_capture=False) to_square = Square('h', '7') assert piece.valid_change(to_square, is_capture=False) to_square = Square('g', '7') assert not piece.valid_change(to_square, is_capture=False)
def test_bishop_cant_move_L_shapes(self): bishop = Bishop('WHITE') is_valid_move = bishop.validate_move((1, 3), (3, 2)) self.assertFalse(is_valid_move) is_valid_move = bishop.validate_move((1, 3), (2, 5)) self.assertFalse(is_valid_move) is_valid_move = bishop.validate_move((2, 3), (1, 5)) self.assertFalse(is_valid_move) is_valid_move = bishop.validate_move((2, 3), (3, 5)) self.assertFalse(is_valid_move) is_valid_move = bishop.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 test_moves_corner(self): bishop = Bishop(self.white, (0, 0)) self.board.add_piece(bishop) moves = list(bishop.moves(self.board)) self.assertEquals(len(moves), 7, moves)
def newGame(self): from piece import Piece, Pawn, Rook, Bishop, Queen, Knight, King # Whites WP1 = Pawn("Pawn", "WP1", "White", 0, 1, self.cb, True, True) WP2 = Pawn("Pawn", "WP2", "White", 1, 1, self.cb, True, True) WP3 = Pawn("Pawn", "WP3", "White", 2, 1, self.cb, True, True) WP4 = Pawn("Pawn", "WP4", "White", 3, 1, self.cb, True, True) WP5 = Pawn("Pawn", "WP5", "White", 4, 1, self.cb, True, True) WP6 = Pawn("Pawn", "WP6", "White", 5, 1, self.cb, True, True) WP7 = Pawn("Pawn", "WP7", "White", 6, 1, self.cb, True, True) WP8 = Pawn("Pawn", "WP8", "White", 7, 1, self.cb, True, True) WKK = King("King", "WKK", "White", 4, 0, self.cb, True, True) WQ1 = Queen("Queen", "WQ1", "White", 3, 0, self.cb, True, True) WR1 = Rook("Rook", "WR1", "White", 0, 0, self.cb, True, True) WR2 = Rook("Rook", "WR2", "White", 7, 0, self.cb, True, True) WK1 = Knight("Knight", "WK1", "White", 1, 0, self.cb, True, True) WK2 = Knight("Knight", "WK2", "White", 6, 0, self.cb, True, True) WB1 = Bishop("Bishop", "WB1", "White", 2, 0, self.cb, True, True) WB2 = Bishop("Bishop", "WB2", "White", 5, 0, self.cb, True, True) self.cb.addPiece(WP1) self.cb.addPiece(WP2) self.cb.addPiece(WP3) self.cb.addPiece(WP4) self.cb.addPiece(WP5) self.cb.addPiece(WP6) self.cb.addPiece(WP7) self.cb.addPiece(WP8) self.cb.addPiece(WKK) self.cb.addPiece(WQ1) self.cb.addPiece(WR1) self.cb.addPiece(WR2) self.cb.addPiece(WB1) self.cb.addPiece(WB2) self.cb.addPiece(WK1) self.cb.addPiece(WK2) # Blacks BP1 = Pawn("Pawn", "BP1", "Black", 0, 6, self.cb, True, True) BP2 = Pawn("Pawn", "BP2", "Black", 1, 6, self.cb, True, True) BP3 = Pawn("Pawn", "BP3", "Black", 2, 6, self.cb, True, True) BP4 = Pawn("Pawn", "BP4", "Black", 3, 6, self.cb, True, True) BP5 = Pawn("Pawn", "BP5", "Black", 4, 6, self.cb, True, True) BP6 = Pawn("Pawn", "BP6", "Black", 5, 6, self.cb, True, True) BP7 = Pawn("Pawn", "BP7", "Black", 6, 6, self.cb, True, True) BP8 = Pawn("Pawn", "BP8", "Black", 7, 6, self.cb, True, True) BKK = King("King", "BKK", "Black", 4, 7, self.cb, True, True) BQ1 = Queen("Queen", "BQ1", "Black", 3, 7, self.cb, True, True) BR1 = Rook("Rook", "BR1", "Black", 0, 7, self.cb, True, True) BR2 = Rook("Rook", "BR2", "Black", 7, 7, self.cb, True, True) BK1 = Knight("Knight", "BK1", "Black", 1, 7, self.cb, True, True) BK2 = Knight("Knight", "BK2", "Black", 6, 7, self.cb, True, True) BB1 = Bishop("Bishop", "BB1", "Black", 2, 7, self.cb, True, True) BB2 = Bishop("Bishop", "BB2", "Black", 5, 7, self.cb, True, True) self.cb.addPiece(BP1) self.cb.addPiece(BP2) self.cb.addPiece(BP3) self.cb.addPiece(BP4) self.cb.addPiece(BP5) self.cb.addPiece(BP6) self.cb.addPiece(BP7) self.cb.addPiece(BP8) self.cb.addPiece(BKK) self.cb.addPiece(BQ1) self.cb.addPiece(BR1) self.cb.addPiece(BR2) self.cb.addPiece(BB1) self.cb.addPiece(BB2) self.cb.addPiece(BK1) self.cb.addPiece(BK2)
def test_cant_move_forward(self): bishop = Bishop(color='BLACK') is_valid_move = bishop.validate_move((7, 2), (4, 2)) self.assertFalse(is_valid_move)
def test_cant_capture_backward(self): bishop = Bishop(color='WHITE') is_valid_capture = bishop.validate_capture((4, 2), (7, 2)) self.assertFalse(is_valid_capture)
def test_cant_capture_sideways(self): bishop = Bishop(color='BLACK') is_valid_capture = bishop.validate_capture((7, 2), (7, 4)) self.assertFalse(is_valid_capture)
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 test_attacked_locations(self): bishop = Bishop(Location(5, 'd'), Player.BLACK, self.board) results = {Location(4, 'c'), Location(3, 'b'), Location(2, 'a'), Location(4, 'e'), Location(3, 'f'), Location(2, 'g'), Location(1, 'h'), Location(6, 'c'), Location(7, 'b'), Location(8, 'a'), Location(6, 'e'), Location(7, 'f'), Location(8, 'g')} self.assertEquals(set(bishop.attacked_locations()), results)
def copy(self): return Bishop(self.pos, self.color, self.rotate)
def __init__(self, pos, color, size, rotate=False): img = 'bishop-{}.png'.format(color) Bishop.__init__(self, pos, color, rotate) DrawablePiece.__init__(self, pos, color, img, size, rotate)
def test_moves_center(self): bishop = Bishop(self.white, (4, 4)) self.board.add_piece(bishop) moves = list(bishop.moves(self.board)) self.assertEquals(len(moves), 13, moves)
def _row_of_rook_bishop(color): return ["", Rook(color), "", "", "", "", "", Bishop(color), ""]