def knight_take_test(): print("========================================") print("Performing the knight take test") print("========================================") board = Board() white_knight = Knight(Colour.WHITE) current_location = Location('b', 1) board.__add_piece__(white_knight, current_location) white_pawn = Pawn(Colour.WHITE) black_pawn = Pawn(Colour.BLACK) board.__add_piece__(white_pawn, Location('a', 3)) board.__add_piece__(black_pawn, Location('c', 3)) print("Added a knight and 2 pawns to the board...") print(board) allowed_moves = white_knight.allowed_moves(current_location, board) print("For a {} {} starting at position {} the moves are:".format( white_knight.colour, white_knight.name, current_location)) print(allowed_moves) new_location = allowed_moves[0] board.move_piece(white_knight, current_location, new_location) current_location = new_location print("Moved the {} to position {}".format(white_knight.name, new_location)) print(board) print("For a {} {} at position {} the moves are:".format( white_knight.colour, white_knight.name, current_location)) print(white_knight.allowed_moves(current_location, board))
def switchOutPawn(currPiece, screen): screen.blit(switchText, (75, 280)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: if currPiece.color: return Queen(currPiece.x, currPiece.y, "images/whiteQueen.png", 1) else: return Queen(currPiece.x, currPiece.y, "images/blackQueen.png", 0) if event.key == pygame.K_r: if currPiece.color: return Rook(currPiece.x, currPiece.y, "images/whiteRook.png", 1) else: return Rook(currPiece.x, currPiece.y, "images/blackRook.png", 0) if event.key == pygame.K_b: if currPiece.color: return Bishop(currPiece.x, currPiece.y, "images/whiteBishop.png", 1) else: return Bishop(currPiece.x, currPiece.y, "images/blackBishop.png", 0) if event.key == pygame.K_k: if currPiece.color: return Knight(currPiece.x, currPiece.y, "images/whiteKnight.png", 1) else: return Knight(currPiece.x, currPiece.y, "images/blackKnight.png", 0)
def __init__(self): self.board = [[], [], [], [], [], [], [], []] self.board[0].append(Rook(0, 0, "black")) self.board[1].append(Knight(1, 0, "black")) self.board[2].append(Bishop(2, 0, "black")) self.board[3].append(Queen(3, 0, "black")) self.board[4].append(King(4, 0, "black")) self.board[5].append(Bishop(5, 0, "black")) self.board[6].append(Knight(6, 0, "black")) self.board[7].append(Rook(7, 0, "black")) for i in range(8): self.board[i].append(Pawn(i, 1, "black")) for i in range(4): for j in range(8): self.board[j].append(NoPiece(i + 2, j)) for i in range(8): self.board[i].append(Pawn(i, 6, "white")) self.board[0].append(Rook(0, 7, "white")) self.board[1].append(Knight(1, 7, "white")) self.board[2].append(Bishop(2, 7, "white")) self.board[3].append(Queen(3, 7, "white")) self.board[4].append(King(4, 7, "white")) self.board[5].append(Bishop(5, 7, "white")) self.board[6].append(Knight(6, 7, "white")) self.board[7].append(Rook(7, 7, "white"))
def init_pieces(width): return [ Rook(0, 0, PieceColor.BLACK, width), Knight(0, 1, PieceColor.BLACK, width), Bishop(0, 2, PieceColor.BLACK, width), Queen(0, 3, PieceColor.BLACK, width), King(0, 4, PieceColor.BLACK, width), Bishop(0, 5, PieceColor.BLACK, width), Knight(0, 6, PieceColor.BLACK, width), Rook(0, 7, PieceColor.BLACK, width), Pawn(1, 0, PieceColor.BLACK, width), Pawn(1, 1, PieceColor.BLACK, width), Pawn(1, 2, PieceColor.BLACK, width), Pawn(1, 3, PieceColor.BLACK, width), Pawn(1, 4, PieceColor.BLACK, width), Pawn(1, 5, PieceColor.BLACK, width), Pawn(1, 6, PieceColor.BLACK, width), Pawn(1, 7, PieceColor.BLACK, width), Pawn(6, 0, PieceColor.WHITE, width), Pawn(6, 1, PieceColor.WHITE, width), Pawn(6, 2, PieceColor.WHITE, width), Pawn(6, 3, PieceColor.WHITE, width), Pawn(6, 4, PieceColor.WHITE, width), Pawn(6, 5, PieceColor.WHITE, width), Pawn(6, 6, PieceColor.WHITE, width), Pawn(6, 7, PieceColor.WHITE, width), Rook(7, 0, PieceColor.WHITE, width), Knight(7, 1, PieceColor.WHITE, width), Bishop(7, 2, PieceColor.WHITE, width), Queen(7, 3, PieceColor.WHITE, width), King(7, 4, PieceColor.WHITE, width), Bishop(7, 5, PieceColor.WHITE, width), Knight(7, 6, PieceColor.WHITE, width), Rook(7, 7, PieceColor.WHITE, width), ]
def custom_piece_placement(self): color = "white" p = Pawn(color) n = Knight(color) r = Rook(color) q = Queen(color) b = Bishop(color) k = King(color) self.board[7][0].add_piece(r) self.board[4][0].add_piece(k) self.board[0][0].add_piece(r) self.board[0][1].add_piece(p) color = "black" p = Pawn(color) n = Knight(color) r = Rook(color) q = Queen(color) b = Bishop(color) k = King(color) self.board[2][3].add_piece(p) self.board[5][6].add_piece(p) self.board[6][7].add_piece(q) self.board[1][5].add_piece(p)
def setupBoard(self): self.tiles.list = [ [ Rook(0, 0, self.tiles, 0), Knight(1, 0, self.tiles, 0), Bishop(2, 0, self.tiles, 0), Queen(3, 0, self.tiles, 0), self.kings[0], Bishop(5, 0, self.tiles, 0), Knight(6, 0, self.tiles, 0), Rook(7, 0, self.tiles, 0) ], [Pawn(x, 1, self.tiles, 0) for x in range(8)], ['', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', ''], [Pawn(x, 6, self.tiles, 1) for x in range(8)], [ Rook(0, 7, self.tiles, 1), Knight(1, 7, self.tiles, 1), Bishop(2, 7, self.tiles, 1), Queen(3, 7, self.tiles, 1), self.kings[1], Bishop(5, 7, self.tiles, 1), Knight(6, 7, self.tiles, 1), Rook(7, 7, self.tiles, 1) ], ]
def place_pieces(self, color): column = 0 color_string = "black" if color == Color.WHITE: column = 7 color_string = "white" self.board[column][0] = Rook(color, column, 0, images[color_string + "_rook"]) self.board[column][1] = Knight(color, column, 1, images[color_string + "_knight"]) self.board[column][2] = Bishop(color, column, 2, images[color_string + "_bishop"]) self.board[column][3] = Queen(color, column, 3, images[color_string + "_queen"]) self.board[column][4] = King(color, column, 4, images[color_string + "_king"]) self.board[column][5] = Bishop(color, column, 5, images[color_string + "_bishop"]) self.board[column][6] = Knight(color, column, 6, images[color_string + "_knight"]) self.board[column][7] = Rook(color, column, 7, images[color_string + "_rook"]) if color == Color.BLACK: column = 1 else: column = 6 for i in range(0, NUM_SQUARES): self.board[column][i] = Pawn(color, column, i, images[color_string + "_pawn"])
def loadPieces(self, n, p): if (p == 'K'): self.gameTiles[n] = Tile(n, King("Black", n)) elif (p == 'k'): self.gameTiles[n] = Tile(n, King("White", n)) elif (p == 'Q'): self.gameTiles[n] = Tile(n, Queen("Black", n)) elif (p == 'q'): self.gameTiles[n] = Tile(n, Queen("White", n)) elif (p == 'N'): self.gameTiles[n] = Tile(n, Knight("Black", n)) elif (p == 'n'): self.gameTiles[n] = Tile(n, Knight("White", n)) elif (p == 'B'): self.gameTiles[n] = Tile(n, Bishop("Black", n)) elif (p == 'b'): self.gameTiles[n] = Tile(n, Bishop("White", n)) elif (p == 'R'): self.gameTiles[n] = Tile(n, Rook("Black", n)) elif (p == 'r'): self.gameTiles[n] = Tile(n, Rook("White", n)) elif (p == 'P'): self.gameTiles[n] = Tile(n, Pawn("Black", n)) elif (p == 'p'): self.gameTiles[n] = Tile(n, Pawn("White", n)) else: self.gameTiles[n] = Tile(n, NullPiece())
def parse(matrix: List[List[str]]) -> List[List[Field]]: """ Parses matrix of strings to matrix of fields and pieces on chessboard. :param matrix: matrix of strings containing string representations of pieces and fields. :return: matrix of fields and chess pieces representing chessboard. """ chessboard = list() for i in range(len(matrix)): chessboard.append(list()) for j in range(len(matrix[i])): chessboard[i].append({ '--': Field(i, j), 'wW': King(i, j, Color.WHITE), 'bW': King(i, j, Color.BLACK), 'wq': Queen(i, j, Color.WHITE), 'bq': Queen(i, j, Color.BLACK), 'wb': Bishop(i, j, Color.WHITE), 'bb': Bishop(i, j, Color.BLACK), 'wk': Knight(i, j, Color.WHITE), 'bk': Knight(i, j, Color.BLACK), 'wr': Rook(i, j, Color.WHITE), 'br': Rook(i, j, Color.BLACK), 'wp': Pawn(i, j, Color.WHITE), 'bp': Pawn(i, j, Color.BLACK) }[matrix[i][j]]) return chessboard
def test_taking_king_not_allowed_knight(self): c5_knight = Knight(ChessCoord('C', '5'), black) b3_king = King(ChessCoord('B', '3'), white) pieces = [c5_knight , b3_king] move_inspect_result = c5_knight.inspect_move(pieces, ChessCoord('B', '3')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [], b3_king))
def _populate_major_and_minor(self, row, color): self.grid[row][4] = King(self, [row, 4], color) self.grid[row][3] = Queen(self, [row, 3], color) self.grid[row][2] = Bishop(self, [row, 2], color) self.grid[row][5] = Bishop(self, [row, 5], color) self.grid[row][0] = Rook(self, [row, 0], color) self.grid[row][7] = Rook(self, [row, 7], color) self.grid[row][1] = Knight(self, [row, 1], color) self.grid[row][6] = Knight(self, [row, 6], color)
def __init__(self): self.state = [ [ Lance(p1), Knight(p1), Silver(p1), Gold(p1), King(p1), Gold(p1), Silver(p1), Knight(p1), Lance(p1) ], [None, Rook(p1), None, None, None, None, None, Bishop(p1), None], [ Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1) ], [None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None], [ Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2) ], [None, Bishop(p2), None, None, None, None, None, Rook(p2), None], [ Lance(p2), Knight(p2), Silver(p2), Gold(p2), King(p2), Gold(p2), Silver(p2), Knight(p2), Lance(p2) ], ]
def create_board(self): # A chess board is constitued of 64 tiles for tile in range(64): self.gameTiles[tile] = Tile(tile, NullPiece()) # Placing the pieces on the board # Placing black pices # First Line self.gameTiles[0] = Tile(0, Rook("Black", 0)) self.gameTiles[1] = Tile(1, Knight("Black", 1)) self.gameTiles[2] = Tile(2, Bishop("Black", 2)) self.gameTiles[3] = Tile(3, Queen("Black", 3)) self.gameTiles[4] = Tile(4, King("Black", 4)) self.gameTiles[5] = Tile(5, Bishop("Black", 5)) self.gameTiles[6] = Tile(6, Knight("Black", 6)) self.gameTiles[7] = Tile(7, Rook("Black", 7)) self.gameTiles[8] = Tile(8, Pawn("Black", 8)) self.gameTiles[9] = Tile(9, Pawn("Black", 9)) # Second Line self.gameTiles[10] = Tile(10, Pawn("Black", 10)) self.gameTiles[11] = Tile(11, Pawn("Black", 11)) self.gameTiles[12] = Tile(12, Pawn("Black", 12)) self.gameTiles[13] = Tile(13, Pawn("Black", 13)) self.gameTiles[14] = Tile(14, Pawn("Black", 14)) self.gameTiles[15] = Tile(15, Pawn("Black", 15)) # Placing the white pieces # First line self.gameTiles[48] = Tile(48, Pawn("White", 48)) self.gameTiles[49] = Tile(49, Pawn("White", 49)) self.gameTiles[50] = Tile(50, Pawn("White", 50)) self.gameTiles[51] = Tile(51, Pawn("White", 51)) self.gameTiles[52] = Tile(52, Pawn("White", 52)) self.gameTiles[53] = Tile(53, Pawn("White", 53)) self.gameTiles[54] = Tile(54, Pawn("White", 54)) self.gameTiles[55] = Tile(55, Pawn("White", 55)) # Second line self.gameTiles[56] = Tile(56, Rook("White", 56)) self.gameTiles[57] = Tile(57, Knight("White", 57)) self.gameTiles[58] = Tile(58, Bishop("White", 58)) self.gameTiles[59] = Tile(59, Queen("White", 59)) self.gameTiles[60] = Tile(60, King("White", 60)) self.gameTiles[61] = Tile(61, Bishop("White", 61)) self.gameTiles[62] = Tile(62, Knight("White", 62)) self.gameTiles[63] = Tile(63, Rook("White", 63))
def createBoard(self): square_color = ["light", "dark"] x = 0 for i in range(64): self.squares[i] = Square(square_color[x % 2], i, nullPiece()) if i % 8 == 0 and i != 0: x += 2 else: x += 1 self.squares[0] = Square("light", 0, Rook("Black", 0)) self.squares[1] = Square("dark", 1, Knight("Black", 1)) self.squares[2] = Square("light", 2, Bishop("Black", 2)) self.squares[3] = Square("dark", 3, Queen("Black", 3)) self.squares[4] = Square("light", 4, self.black_king) self.squares[5] = Square("dark", 5, Bishop("Black", 5)) self.squares[6] = Square("light", 6, Knight("Black", 6)) self.squares[7] = Square("dark", 7, Rook("Black", 7)) self.squares[8] = Square("dark", 8, Pawn("Black", 8)) self.squares[9] = Square("light", 9, Pawn("Black", 9)) self.squares[10] = Square("dark", 10, Pawn("Black", 10)) self.squares[11] = Square("light", 11, Pawn("Black", 11)) self.squares[12] = Square("dark", 12, Pawn("Black", 12)) self.squares[13] = Square("light", 13, Pawn("Black", 13)) self.squares[14] = Square("dark", 14, Pawn("Black", 14)) self.squares[15] = Square("light", 15, Pawn("Black", 15)) self.squares[16] = Square("light", 16, nullPiece()) self.squares[24] = Square("dark", 24, nullPiece()) self.squares[32] = Square("light", 32, nullPiece()) self.squares[40] = Square("dark", 40, nullPiece()) self.squares[48] = Square("light", 48, Pawn("White", 48)) self.squares[49] = Square("dark", 49, Pawn("White", 49)) self.squares[50] = Square("light", 50, Pawn("White", 50)) self.squares[51] = Square("dark", 51, Pawn("White", 51)) self.squares[52] = Square("light", 52, Pawn("White", 52)) self.squares[53] = Square("dark", 53, Pawn("White", 53)) self.squares[54] = Square("light", 54, Pawn("White", 54)) self.squares[55] = Square("dark", 55, Pawn("White", 55)) self.squares[56] = Square("dark", 56, Rook("White", 56)) self.squares[57] = Square("light", 57, Knight("White", 57)) self.squares[58] = Square("dark", 58, Bishop("White", 58)) self.squares[59] = Square("light", 59, Queen("White", 59)) self.squares[60] = Square("dark", 60, self.white_king) self.squares[61] = Square("light", 61, Bishop("White", 61)) self.squares[62] = Square("dark", 62, Knight("White", 62)) self.squares[63] = Square("light", 63, Rook("White", 63))
def createBoard(self): for c in range(65): self.gameTiles.append(None) for tile in range(65): self.gameTiles[tile] = Tile(tile, NullPiece()) self.gameTiles[1] = Tile(1, Rook(1, "Black")) self.gameTiles[2] = Tile(2, Knight(2, "Black")) self.gameTiles[3] = Tile(3, Bishop(3, "Black")) self.gameTiles[4] = Tile(4, Queen(4, "Black")) self.gameTiles[5] = Tile(5, King(5, "Black")) self.gameTiles[6] = Tile(6, Bishop(6, "Black")) self.gameTiles[7] = Tile(7, Knight(7, "Black")) self.gameTiles[8] = Tile(8, Rook(8, "Black")) self.gameTiles[9] = Tile(9, Pawn(9, "Black")) self.gameTiles[10] = Tile(10, Pawn(10, "Black")) self.gameTiles[11] = Tile(11, Pawn(11, "Black")) self.gameTiles[12] = Tile(12, Pawn(12, "Black")) self.gameTiles[13] = Tile(13, Pawn(13, "Black")) self.gameTiles[14] = Tile(14, Pawn(14, "Black")) self.gameTiles[15] = Tile(15, Pawn(15, "Black")) self.gameTiles[16] = Tile(16, Pawn(16, "Black")) #self.gameTiles[35] = Tile(35, Bishop(35, "White")) #self.gameTiles[14] = Tile(14,Queen(14, "White")) self.gameTiles[49] = Tile(49, Pawn(49, "White")) self.gameTiles[50] = Tile(50, Pawn(50, "White")) self.gameTiles[51] = Tile(51, Pawn(51, "White")) self.gameTiles[52] = Tile(52, Pawn(52, "White")) self.gameTiles[53] = Tile(53, Pawn(53, "White")) self.gameTiles[54] = Tile(54, Pawn(54, "White")) self.gameTiles[55] = Tile(55, Pawn(55, "White")) self.gameTiles[56] = Tile(56, Pawn(56, "White")) self.gameTiles[57] = Tile(57, Rook(57, "White")) self.gameTiles[58] = Tile(58, Knight(58, "White")) self.gameTiles[59] = Tile(59, Bishop(59, "White")) self.gameTiles[60] = Tile(60, Queen(60, "White")) self.gameTiles[61] = Tile(61, King(61, "White")) self.gameTiles[62] = Tile(62, Bishop(62, "White")) self.gameTiles[63] = Tile(63, Knight(63, "White")) self.gameTiles[64] = Tile(64, Rook(64, "White"))
def setUp(self): self.queen_white = Queen(ChessCoord('E', '6'), white) self.queen_black = Queen(ChessCoord('G', '5'), black) self.rook_white = Rook(ChessCoord('D', '1'), white) self.knight_white = Knight(ChessCoord('G', '1'), white) self.pieces = [self.queen_white, self.queen_black, self.rook_white, self.knight_white]
def test_knight(self): b = board.Board() b.set_piece((1, 1), board.Piece(board.PieceType.KNIGHT, True)) self.assertItemsEqual([(3, 2), (2, 3), (0, 3), (3, 0)], Knight.get_moves((1, 1), b[(1, 1)], b))
def __init__(self): pygame.font.init() self.width = 400 self.height = 500 self.win = pygame.display.set_mode((self.width, self.height)) self.squares = [[0 for x in range(8)] for y in range(8)] self.square_width = 50 self.square_height = 50 self.white_pieces = [Pawn(0) for x in range(8)] + [Rook(0), Knight(0), Bishop(0), Queen(0), King(0), Bishop(0), Knight(0), Rook(0)] self.black_pieces = [Pawn(1) for x in range(8)] + [Rook(1), Knight(1), Bishop(1), Queen(1), King(1), Bishop(1), Knight(1), Rook(1)] self.current_player = 0 self.chosen = None self.big_font = pygame.font.SysFont('Comic Sans MS', 30, bold=True) self.small_font = pygame.font.SysFont('Comic Sans MS', 20, bold=True) pygame.display.set_caption("pyChess") pygame.display.set_icon(pygame.transform.scale(pygame.image.load("assets/pieces/black/black_king.png"), (32, 32)))
def test_knight_threat_squares(self): pawn_black = Pawn(ChessCoord('A', '2'), black, go_south) knight_white = Knight(ChessCoord('C', '1'), white) pieces = [pawn_black, knight_white] expected_squares_chess = [ ChessCoord('B', '3'), ChessCoord('D', '3'), ChessCoord('E', '2'), ] knight_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('C', '1')) expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, knight_white.is_threat_to_these_squares))
def test_knight_get_possible_moves(self): matrix = PieceTest.initialize_empty_grid() knight = Knight('white', 'white_knight_1') positions = [(0, 0), (0, 7), (3, 4), (7, 0), (7, 7)] expectations = { (0, 0): [(2, 1, 'mov'), (1, 2, 'mov')], (0, 7): [(2, 6, 'mov'), (1, 5, 'mov')], (3, 4): [(1, 3, 'mov'), (2, 2, 'mov'), (1, 5, 'mov'), (2, 6, 'mov'), (5, 3, 'mov'), (4, 2, 'mov'), (5, 5, 'mov'), (4, 6, 'mov')], (7, 0): [(5, 1, 'mov'), (6, 2, 'mov')], (7, 7): [(5, 6, 'mov'), (6, 5, 'mov')] } for pos in positions: possible_moves = knight.get_possible_moves(pos, matrix) self.assertCountEqual(possible_moves, expectations[pos])
def promote(self, x, y, alliance, piece): if (piece == "Q"): self.board[x][y] = Tile(Queen(alliance, x, y)) if (piece == "B"): self.board[x][y] = Tile(Bishop(alliance, x, y)) if (piece == "R"): self.board[x][y] = Tile(Rook(alliance, x, y)) if (piece == "N"): self.board[x][y] = Tile(Knight(alliance, x, y))
def build_white_pieces(): white_pieces = [] # rooks white_pieces.append(Rook(WHITE, Coordinate(0, 7))) white_pieces.append(Rook(WHITE, Coordinate(7, 7))) # knights white_pieces.append(Knight(WHITE, Coordinate(1, 7))) white_pieces.append(Knight(WHITE, Coordinate(6, 7))) # bishops white_pieces.append(Bishop(WHITE, Coordinate(2, 7))) white_pieces.append(Bishop(WHITE, Coordinate(5, 7))) # queen white_pieces.append(Queen(WHITE, Coordinate(3, 7))) # king white_pieces.append(King(WHITE, Coordinate(4, 7))) # pawns for x in range(8): white_pieces.append(Pawn(WHITE, Coordinate(x, 6))) return white_pieces
def build_black_pieces(): black_pieces = [] # rooks black_pieces.append(Rook(BLACK, Coordinate(0, 0))) black_pieces.append(Rook(BLACK, Coordinate(7, 0))) # knights black_pieces.append(Knight(BLACK, Coordinate(1, 0))) black_pieces.append(Knight(BLACK, Coordinate(6, 0))) # bishops black_pieces.append(Bishop(BLACK, Coordinate(2, 0))) black_pieces.append(Bishop(BLACK, Coordinate(5, 0))) # queen black_pieces.append(Queen(BLACK, Coordinate(3, 0))) # king black_pieces.append(King(BLACK, Coordinate(4, 0))) # pawns for x in range(8): black_pieces.append(Pawn(BLACK, Coordinate(x, 1))) return black_pieces
def reset_pieces(self): # p2 = Pawn("black") # b.board[5][6].add_piece(p) # WHITE PAWNS color = "white" for i in range(Config.BOARD_SIZE): p = Pawn(color) self.board[i][6].add_piece(p) # Other white pieces pieces = [ Rook(color), Knight(color), Bishop(color), Queen(color), King(color), Bishop(color), Knight(color), Rook(color) ] for i in range(len(pieces)): self.board[i][7].add_piece(pieces[i]) # BLACK PAWNS color = "black" for i in range(Config.BOARD_SIZE): p = Pawn(color) self.board[i][1].add_piece(p) # Other black pieces pieces = [ Rook(color), Knight(color), Bishop(color), Queen(color), King(color), Bishop(color), Knight(color), Rook(color) ] for i in range(len(pieces)): self.board[i][0].add_piece(pieces[i])
def populate_board(self): # self.board[0][0] = Rook(0, 0, "black") # self.board[1][0] = Knight(1, 0, "black") # self.board[2][0] = Bishop(2, 0, "black") # self.board[4][0] = Queen(4, 0, "black") # self.board[3][0] = King(3, 0, "black") # self.board[5][0] = Bishop(5, 0, "black") # self.board[6][0] = Knight(6, 0, "black") # self.board[7][0] = Rook(7, 0, "black") # for i in range(8): # self.board[i][1] = Pawn(i, 1, "black") # self.board[i][6] = Pawn(i, 6, "white") # self.board[0][7] = Rook(0, 7, "white") # self.board[1][7] = Knight(1, 7, "white") # self.board[2][7] = Bishop(2, 7, "white") # self.board[4][7] = Queen(4, 7, "white") # self.board[3][7] = King(3, 7, "white") # self.board[5][7] = Bishop(5, 7, "white") # self.board[6][7] = Knight(6, 7, "white") # self.board[7][7] = Rook(7, 7, "white") 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") for i in range(8): self.board[1][i] = Pawn(1, i, "black") self.board[6][i] = Pawn(6, i, "white") 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")
def __add_inital_pieces__(board, colour): first_row = 1 if colour == Colour.WHITE else 8 second_row = 2 if colour == Colour.WHITE else 7 board.__add_piece__(Rook(colour), Location('a', first_row)) board.__add_piece__(Knight(colour), Location('b', first_row)) board.__add_piece__(Bishop(colour), Location('c', first_row)) board.__add_piece__(Queen(colour), Location('d', first_row)) board.__add_piece__(King(colour), Location('e', first_row)) board.__add_piece__(Bishop(colour), Location('f', first_row)) board.__add_piece__(Knight(colour), Location('g', first_row)) board.__add_piece__(Rook(colour), Location('h', first_row)) board.__add_piece__(Pawn(colour), Location('a', second_row)) board.__add_piece__(Pawn(colour), Location('b', second_row)) board.__add_piece__(Pawn(colour), Location('c', second_row)) board.__add_piece__(Pawn(colour), Location('d', second_row)) board.__add_piece__(Pawn(colour), Location('e', second_row)) board.__add_piece__(Pawn(colour), Location('f', second_row)) board.__add_piece__(Pawn(colour), Location('g', second_row)) board.__add_piece__(Pawn(colour), Location('h', second_row))
def createBoard(self): for rows in range(8): for cols in range(8): self.board[rows][cols] = Tile(nullPiece()) #Pawn create for cols in range(8): self.board[1][cols] = Tile(Pawn("B", 1, cols)) for cols in range(8): self.board[6][cols] = Tile(Pawn("W", 6, cols)) #create King self.board[0][4] = Tile(King("B", 0, 4)) self.board[7][4] = Tile(King("W", 7, 4)) # create Queen self.board[0][3] = Tile(Queen("B", 0, 3)) self.board[7][3] = Tile(Queen("W", 7, 3)) # create Rock self.board[0][0] = Tile(Rook("B", 0, 0)) self.board[0][7] = Tile(Rook("B", 0, 7)) self.board[7][0] = Tile(Rook("W", 7, 0)) self.board[7][7] = Tile(Rook("W", 7, 7)) # create Bishop self.board[0][2] = Tile(Bishop("B", 0, 2)) self.board[0][5] = Tile(Bishop("B", 0, 5)) self.board[7][2] = Tile(Bishop("W", 7, 2)) self.board[7][5] = Tile(Bishop("W", 7, 5)) # create Knight self.board[0][1] = Tile(Knight("B", 0, 1)) self.board[0][6] = Tile(Knight("B", 0, 6)) self.board[7][1] = Tile(Knight("W", 7, 1)) self.board[7][6] = Tile(Knight("W", 7, 6))
def createBoard(self): for square in range(64): self.squares[square] = Square(square, EmptyPiece()) self.squares[0] = Square(0, Rook("Black", 0)) self.squares[1] = Square(1, Knight("Black", 1)) self.squares[2] = Square(2, Bishop("Black", 2)) self.squares[3] = Square(3, Queen("Black", 3)) self.squares[4] = Square(4, King("Black", 4)) self.squares[5] = Square(5, Bishop("Black", 5)) self.squares[6] = Square(6, Knight("Black", 6)) self.squares[7] = Square(7, Rook("Black", 7)) self.squares[8] = Square(8, Pawn("Black", 8)) self.squares[9] = Square(9, Pawn("Black", 9)) self.squares[10] = Square(10, Pawn("Black", 10)) self.squares[11] = Square(11, Pawn("Black", 11)) self.squares[12] = Square(12, Pawn("Black", 12)) self.squares[13] = Square(13, Pawn("Black", 13)) self.squares[14] = Square(14, Pawn("Black", 14)) self.squares[15] = Square(15, Pawn("Black", 15)) self.squares[56] = Square(56, Rook("White", 56)) self.squares[57] = Square(57, Knight("White", 57)) self.squares[58] = Square(58, Bishop("White", 58)) self.squares[59] = Square(59, Queen("White", 59)) self.squares[60] = Square(60, King("White", 60)) self.squares[61] = Square(61, Bishop("White", 61)) self.squares[62] = Square(62, Knight("White", 62)) self.squares[63] = Square(63, Rook("White", 63)) self.squares[48] = Square(48, Pawn("White", 48)) self.squares[49] = Square(49, Pawn("White", 49)) self.squares[50] = Square(50, Pawn("White", 50)) self.squares[51] = Square(51, Pawn("White", 51)) self.squares[52] = Square(52, Pawn("White", 52)) self.squares[53] = Square(53, Pawn("White", 53)) self.squares[54] = Square(54, Pawn("White", 54)) self.squares[55] = Square(55, Pawn("White", 55))
def createPiece(self): chessboard[(6, 0)] = BlackPawn(6, 0, True) chessboard[(6, 1)] = BlackPawn(6, 1, True) chessboard[(6, 2)] = BlackPawn(6, 2, True) chessboard[(6, 3)] = BlackPawn(6, 3, True) chessboard[(6, 4)] = BlackPawn(6, 4, True) chessboard[(6, 5)] = BlackPawn(6, 5, True) chessboard[(6, 6)] = BlackPawn(6, 6, True) # chessboard[(6, 7)] = BlackPawn(6, 7, True) chessboard[(7, 0)] = Rook(7, 0, True) chessboard[(7, 1)] = Knight(7, 1, True) chessboard[(7, 2)] = Bishop(7, 2, True) chessboard[(7, 3)] = King(7, 3, True) self.white_king = chessboard[(7, 3)] chessboard[(7, 4)] = Queen(7, 4, True) chessboard[(7, 5)] = Bishop(7, 5, True) chessboard[(7, 6)] = Knight(7, 6, True) chessboard[(7, 7)] = Rook(7, 7, True) chessboard[(0, 0)] = Rook(0, 0, False) chessboard[(0, 1)] = Knight(0, 1, False) chessboard[(0, 2)] = Bishop(0, 2, False) chessboard[(0, 3)] = King(0, 3, False) self.black_king = chessboard[(0, 3)] chessboard[(0, 4)] = Queen(0, 4, False) chessboard[(0, 5)] = Bishop(0, 5, False) chessboard[(0, 6)] = Knight(0, 6, False) chessboard[(0, 7)] = Rook(0, 7, False) # chessboard[(1, 0)] = WhitePawn(1, 0, False) chessboard[(1, 1)] = WhitePawn(1, 1, False) chessboard[(1, 2)] = WhitePawn(1, 2, False) chessboard[(1, 3)] = WhitePawn(1, 3, False) chessboard[(1, 4)] = WhitePawn(1, 4, False) chessboard[(1, 5)] = WhitePawn(1, 5, False) chessboard[(1, 6)] = WhitePawn(1, 6, False) chessboard[(1, 7)] = WhitePawn(1, 7, False)
def basic_knight_test(): print("========================================") print("Performing the basic knight movement test") print("========================================") board = Board() white_knight = Knight(Colour.WHITE) current_location = Location('b', 1) board.__add_piece__(white_knight, current_location) print(board) allowed_moves = white_knight.allowed_moves(current_location, board) print("For a {} {} starting at position {} the moves are:".format( white_knight.colour, white_knight.name, current_location)) print(allowed_moves) new_location = allowed_moves[0] board.move_piece(white_knight, current_location, new_location) current_location = new_location print("Moved the {} to position {}".format(white_knight.name, new_location)) print(board) print("For a {} {} at position {} the moves are:".format( white_knight.colour, white_knight.name, current_location)) print(white_knight.allowed_moves(current_location, board))
def __init__(self, colour, piece_type, file, rank): self.colour = colour self.file = file self.rank = rank self.type = piece_type if self.type == 'pawn': self.typemgr = Pawn(colour=colour, file=file, rank=rank) elif self.type == 'knight': self.typemgr = Knight(colour=colour, file=file, rank=rank) elif self.type == 'bishop': self.typemgr = Bishop(colour=colour, file=file, rank=rank) elif self.type == 'rook': self.typemgr = Rook(colour=colour, file=file, rank=rank) elif self.type == 'queen': self.typemgr = Queen(colour=colour, file=file, rank=rank) elif self.type == 'king': self.typemgr = King(colour=colour, file=file, rank=rank)
def __init__(self, board, turn, boards): self.turn = turn objBoard = [] kings = [] #Define a board filled with piece objects y_Pos = 0 for y in board: x_ObjBoard = [] x_Pos = 0 for x in y: if x != "": piece = Piece( x[0], x[1], (y_Pos, x_Pos), None ) #Why did I define it as (y,x)? I have no idea... But I regret doing so... deeply.... x_ObjBoard.append(piece) if x[1] == "R": movement = Rook(x[0], x[1], (y_Pos, x_Pos), x[2]) elif x[1] == "B": movement = Bishop(x[0], x[1], (y_Pos, x_Pos)) elif x[1] == "Q": movement = Queen(x[0], x[1], (y_Pos, x_Pos)) elif x[1] == "k": movement = Knight(x[0], x[1], (y_Pos, x_Pos)) elif x[1] == "P": movement = Pawn(x[0], x[1], (y_Pos, x_Pos), x[2]) elif x[1] == "K": movement = King(x[0], x[1], (y_Pos, x_Pos), x[2]) kings.append(piece) piece.setMove( movement ) #Append the unique movement code to the piece object else: x_ObjBoard.append("") x_Pos += 1 objBoard.append(x_ObjBoard) y_Pos += 1 self.promote = [False, False] #If a pawn is promoting (White,Black) self.colour = "R" self.boards = boards self.board = objBoard self.kings = kings #King objects stored for checking checks
def __get_take_promotions__(self, take): moves = [] moves += [Location(take.letter, take.number, promotion=True, promotion_piece=Queen(self.colour), take=take.take, take_piece=take.take_piece, take_piece_location=take.take_piece_location)] moves += [Location(take.letter, take.number, promotion=True, promotion_piece=Rook(self.colour), take=take.take, take_piece=take.take_piece, take_piece_location=take.take_piece_location)] moves += [Location(take.letter, take.number, promotion=True, promotion_piece=Bishop(self.colour), take=take.take, take_piece=take.take_piece, take_piece_location=take.take_piece_location)] moves += [Location(take.letter, take.number, promotion=True, promotion_piece=Knight(self.colour), take=take.take, take_piece=take.take_piece, take_piece_location=take.take_piece_location)] return moves
def setUp(self): chess_coord_white = ChessCoord('C', '2') self.knight_white = Knight(chess_coord_white, white) chess_coord_black = ChessCoord('F', '5') self.knight_black = Knight(chess_coord_black, black)
class ThreatTests(unittest.TestCase): def setUp(self): self.queen_white = Queen(ChessCoord('E', '6'), white) self.queen_black = Queen(ChessCoord('G', '5'), black) self.rook_white = Rook(ChessCoord('D', '1'), white) self.knight_white = Knight(ChessCoord('G', '1'), white) self.pieces = [self.queen_white, self.queen_black, self.rook_white, self.knight_white] def test_queens_threatening_each_other(self): self.queen_white.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('E', '5')) self.failUnless(self.queen_black.is_threat_to_these_pieces == [self.queen_white, self.knight_white]) self.failUnless(self.queen_white.is_threat_to_these_pieces == [self.queen_black]) def test_queen_threatening_rook_but_not_vice_versa(self): self.queen_black.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('G', '4')) self.failUnless(self.queen_black.is_threat_to_these_pieces == [self.queen_white, self.rook_white, self.knight_white]) self.failUnless(self.queen_white.is_threat_to_these_pieces == [self.queen_black]) self.failUnless(self.rook_white.is_threat_to_these_pieces == []) def test_knight_threatening_queen_but_not_vice_versa(self): self.knight_white.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('F', '3')) self.failUnless(self.knight_white.is_threat_to_these_pieces == [self.queen_black]) self.failUnless(self.queen_black.is_threat_to_these_pieces == []) def test_knight_threatening_none(self): pieces = self.pieces + [Pawn(ChessCoord('C', '3'), black, go_north), Knight(ChessCoord('C', '1'), black), ] self.knight_white.update_coord(ChessCoord('A', '4')) self.knight_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('B', '2')) self.failUnless(self.knight_white.is_threat_to_these_pieces == []) def test_knight_threatening_none_with_all_pieces(self): pieces = copy.deepcopy(starting_pieces) g8_knight = select_piece(ChessCoord('G', '8'), pieces) e2_pawn = select_piece(ChessCoord('E', '2'), pieces) e2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '4')) self.failUnless(g8_knight.is_threat_to_these_pieces == []) def test_king_only_threatens_for_one_step_with_all_pieces(self): pieces = copy.deepcopy(starting_pieces) e1_king = select_piece(ChessCoord('E', '1'), pieces) e1_king.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '6')) pawns_d7_e7_f7 = select_pieces([ChessCoord('D', '7'), ChessCoord('E', '7'), ChessCoord('F', '7')], pieces) self.failUnless(util.compare_lists(e1_king.is_threat_to_these_pieces, pawns_d7_e7_f7)) def test_pieces_threatens_none_from_start_position(self): pieces = copy.deepcopy(starting_pieces) map(lambda piece: piece.analyze_threats_on_board_for_new_move(pieces, piece.chess_coord ), pieces) all_threats = map(lambda piece: piece.is_threat_to_these_pieces, pieces) should_be_all_empty = filter(lambda threat_list: threat_list is not [], all_threats) should_be_all_empty_filter_away_empty = map(lambda sbae: sbae is not [], should_be_all_empty) self.failIf(False in should_be_all_empty_filter_away_empty) def test_pawn_threatens_none_from_start_position(self): pieces = copy.deepcopy(starting_pieces) b7_pawn = select_piece(ChessCoord('B', '7'), pieces) b7_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('B', '7')) self.failUnless(b7_pawn.is_threat_to_these_pieces == []) def test_pawn_threatens_appropriate_pieces_in_front_of_it(self): pieces = copy.deepcopy(starting_pieces) f2_pawn = select_piece(ChessCoord('F', '2'), pieces) f2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '6')) pawns_e7_g7 = select_pieces([ChessCoord('E', '7'), ChessCoord('G', '7')], pieces) self.failUnless(util.compare_lists(f2_pawn.is_threat_to_these_pieces, pawns_e7_g7)) def test_pawn_threatens_appropriate_pieces_with_en_passant_in_front_of_it(self): pieces = copy.deepcopy(starting_pieces) g7_pawn = select_piece(ChessCoord('G', '7'), pieces) g7_pawn.update_coord(ChessCoord('G', '5')) f2_pawn = select_piece(ChessCoord('F', '2'), pieces) f2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '5')) self.failUnless(util.compare_lists(f2_pawn.is_threat_to_these_pieces, [g7_pawn]))
class KnightTests(unittest.TestCase): def setUp(self): chess_coord_white = ChessCoord('C', '2') self.knight_white = Knight(chess_coord_white, white) chess_coord_black = ChessCoord('F', '5') self.knight_black = Knight(chess_coord_black, black) def test_constructor_white(self): self.failUnless(self.knight_white.letter == 'Kn') self.failUnless(self.knight_white.chess_coord == ChessCoord('C', '2')) self.failUnless(self.knight_white.grid_coord == GridCoord(2, 1)) self.failUnless(self.knight_white.colour == white) self.failUnless(self.knight_white.symbol == '♘') self.failUnless(compare_lists(self.knight_white.move_directions, directions.move_directions_knight())) def test_constructor_black(self): self.failUnless(self.knight_black.letter == 'Kn') self.failUnless(self.knight_black.chess_coord == ChessCoord('F', '5')) self.failUnless(self.knight_black.grid_coord == GridCoord(5, 4)) self.failUnless(self.knight_black.colour == black) self.failUnless(self.knight_black.symbol == '♞') self.failUnless(compare_lists(self.knight_black.move_directions, directions.move_directions_knight())) def test_white_knight_can_move_north2_east1(self): pieces = [] self.failUnless(self.knight_white.inspect_move(pieces, ChessCoord('D', '4')).is_valid_move) def test_white_knight_can_move_south1_east2(self): pieces = [] self.failUnless(self.knight_white.inspect_move(pieces, ChessCoord('A', '1')).is_valid_move) def test_white_knight_should_not_be_allowed_invalid_move(self): pieces = [] self.failIf(self.knight_white.inspect_move(pieces, ChessCoord('A', '2')).is_valid_move) def test_white_knight_should_not_change_coordinates_after_valid_move_check(self): pieces = [] self.failUnless(self.knight_white.inspect_move(pieces, ChessCoord('D', '4')).is_valid_move) def test_white_knight_should_move_when_coordinates_updated(self): self.knight_white.update_coord(ChessCoord('D', '6')) self.failUnless(self.knight_white.chess_coord == ChessCoord('D', '6')) def test_white_knight_should_not_be_allowed_invalid_move_after_update(self): pieces = [] self.knight_white.update_coord(ChessCoord('D', '6')) self.failIf(self.knight_white.inspect_move(pieces, ChessCoord('A', '2')).is_valid_move) self.failUnless(self.knight_white.chess_coord == ChessCoord('D', '6')) def test_white_knight_is_blocked_by_other_piece(self): pieces = [Pawn(ChessCoord('D', '4'), white, directions.go_north)] move_inspect_result = self.knight_white.inspect_move(pieces, ChessCoord('D', '4')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [], pieces[0])) def test_white_knight_is_valid_to_take_enemy_piece(self): pieces = [Pawn(ChessCoord('F', '3'), black, directions.go_south)] self.knight_white.update_coord(ChessCoord('D', '4')) move_inspect_result = self.knight_white.inspect_move(pieces, ChessCoord('F', '3')) self.failUnless(move_inspect_result == MoveInspectResult(True, False, [], pieces[0])) def test_black_knight_can_not_take_enemy_queen_from_start(self): all_pieces = copy.deepcopy(starting_pieces) select_piece(ChessCoord('A', '2'), all_pieces)\ .update_coord(ChessCoord('A', '4')) g8_knight = select_piece(ChessCoord('G', '8'), all_pieces) inspect_move_result = g8_knight.inspect_move(all_pieces, ChessCoord('D', '1')) self.failIf(inspect_move_result.is_valid_move)