def set_new_game(self): """ Initializes pieces for a new chess game. Uses two for loops and if/else statements to set the pieces. """ board = [] A, B, C, D, E, F, G, H = range( 8 ), range(8), range(8), range(8), range(8), range(8), range(8), range( 8 ) # Feels unpythonic but can't do A = B = C = ... = range(8) since lists are mutable board.extend([A, B, C, D, E, F, G, H]) for row in xrange(8): for col in xrange(8): if col == 1: board[row][col] = Pawn(False, 'White') elif col == 6: board[row][col] = Pawn(False, 'Black') elif col in range(2, 7): board[row][col] = Piece() elif col == 0: if row == 0 or row == 7: board[row][col] = Rook(False, 'White') elif row == 1 or row == 6: board[row][col] = Knight(False, 'White') elif row == 2 or row == 5: board[row][col] = Bishop(False, 'White') elif row == 3: board[row][col] = Queen(False, 'White') else: board[row][col] = King(False, 'White') else: if row == 0 or row == 7: board[row][col] = Rook(False, 'Black') elif row == 1 or row == 6: board[row][col] = Knight(False, 'Black') elif row == 2 or row == 5: board[row][col] = Bishop(False, 'Black') elif row == 3: board[row][col] = Queen(False, 'Black') else: board[row][col] = King(False, 'Black') return board
def new_game(self): pieces = list() self.pieces = list() # Pawn initialisation for i in range(1, 9): if self.color == "black": pieces.append(Pawn(i, 7)) if self.color == "white": pieces.append(Pawn(i, 2)) # Rook initialisation if self.color == "black": pieces.append(Rook(1, 8)) pieces.append(Rook(8, 8)) if self.color == "white": pieces.append(Rook(1, 1)) pieces.append(Rook(8, 1)) # Knight initialisation if self.color == "black": pieces.append(Knight(2, 8)) pieces.append(Knight(7, 8)) if self.color == "white": pieces.append(Knight(2, 1)) pieces.append(Knight(7, 1)) # Bishop initialisation if self.color == "black": pieces.append(Bishop(3, 8)) pieces.append(Bishop(6, 8)) if self.color == "white": pieces.append(Bishop(3, 1)) pieces.append(Bishop(6, 1)) # Queen initialisation if self.color == "black": pieces.append(Queen(4, 8)) if self.color == "white": pieces.append(Queen(4, 1)) # King initialisation if self.color == "black": pieces.append(King(5, 8)) if self.color == "white": pieces.append(King(5, 1)) self.pieces = pieces
def initialize_starting_board(self): for letter in letters: self.set_piece_on_square(letter, 7, Pawn(BLACK)) self.set_piece_on_square(letter, 2, Pawn(WHITE)) for letter in ['a', 'h']: self.set_piece_on_square(letter, 1, Bishop(WHITE)) self.set_piece_on_square(letter, 8, Bishop(BLACK)) for letter in ['b', 'g']: self.set_piece_on_square(letter, 1, Knight(WHITE)) self.set_piece_on_square(letter, 8, Knight(BLACK)) for letter in ['c', 'f']: self.set_piece_on_square(letter, 1, Rook(WHITE)) self.set_piece_on_square(letter, 8, Rook(BLACK)) self.set_piece_on_square('d', 1, Queen(WHITE)) self.set_piece_on_square('e', 8, Queen(BLACK)) self.set_piece_on_square('e', 1, King(WHITE)) self.set_piece_on_square('d', 8, King(BLACK))
def _get_piece_from_formatted_string(self, formatted_string: str) -> Piece: piece_string = formatted_string[0] if Game.PAWN_STRING == piece_string: return Pawn(self._color) elif Game.KNIGHT_STRING == piece_string: return Knight(self._color) elif Game.BISHOP_STRING == piece_string: return Bishop(self._color) elif Game.ROOK_STRING == piece_string: return Rook(self._color) elif Game.QUEEN_STRING == piece_string: return Queen(self._color) else: return King(self._color)
def test_create_move(self): formatted_string = "PA2A3" move_counter = 0 move = Move(formatted_string, move_counter) expected_origin = (6, 0) expected_destination = (5, 0) expected_color = Game.WHITE expected_other_color = Game.BLACK expected_piece = Pawn(expected_color) self.assertEqual(expected_origin, move.get_origin()) self.assertEqual(expected_destination, move.get_destination()) self.assertEqual(expected_color, move.get_color()) self.assertEqual(expected_other_color, move.get_other_color()) self.assertEqual(expected_piece, move.get_piece()) self.assertEqual(formatted_string, move.get_formatted_string())
from Board import Board from Piece import Piece, Pawn, Rook, Knight, Bishop, Queen, King import chessAI # board = Board() # board.matrix[0][0] = Rook(0, 0, 0) board.matrix[0][1] = Knight(0, 0, 1) board.matrix[0][2] = Bishop(0, 0, 2) board.matrix[0][3] = Queen(0, 0, 3) board.matrix[0][4] = King(0, 0, 4) board.matrix[0][5] = Bishop(0, 0, 5) board.matrix[0][6] = Knight(0, 0, 6) board.matrix[0][7] = Rook(0, 0, 7) # board.matrix[1][0] = Pawn(0, 1, 0) board.matrix[1][1] = Pawn(0, 1, 1) board.matrix[1][2] = Pawn(0, 1, 2) board.matrix[1][3] = Pawn(0, 1, 3) board.matrix[1][4] = Pawn(0, 1, 4) board.matrix[1][5] = Pawn(0, 1, 5) board.matrix[1][6] = Pawn(0, 1, 6) board.matrix[1][7] = Pawn(0, 1, 7) # board.matrix[7][0] = Rook(1, 0, 0) board.matrix[7][1] = Knight(1, 0, 1) board.matrix[7][2] = Bishop(1, 0, 2) board.matrix[7][3] = Queen(1, 0, 3) board.matrix[7][4] = King(1, 0, 4) board.matrix[7][5] = Bishop(1, 0, 5) board.matrix[7][6] = Knight(1, 0, 6)
def test_board_get_destinations_from_origin(self): cases = [ { "name": "white_pawn_first_move", "origin": (6, 1), "layout": { (6, 1): Pawn(Game.WHITE) }, "expected_destinations": {(5, 1), (4, 1)} }, { "name": "white_pawn_not_first_move", "origin": (5, 1), "layout": { (5, 1): Pawn(Game.WHITE) }, "expected_destinations": {(4, 1)} }, { "name": "white_pawn_no_queen_promotion", "origin": (0, 1), "layout": { (0, 1): Pawn(Game.WHITE) }, "expected_destinations": set() }, { "name": "white_pawn_attack", "origin": (6, 1), "layout": { (6, 1): Pawn(Game.WHITE), (5, 0): Pawn(Game.BLACK), (5, 2): Pawn(Game.BLACK) }, "expected_destinations": {(5, 1), (4, 1), (5, 0), (5, 2)} }, { "name": "white_pawn_no_attack", "origin": (6, 1), "layout": { (6, 1): Pawn(Game.WHITE), (5, 0): Pawn(Game.WHITE), (5, 2): Pawn(Game.WHITE) }, "expected_destinations": {(5, 1), (4, 1)} }, { "name": "black_pawn_first_move", "origin": (1, 1), "layout": { (1, 1): Pawn(Game.BLACK) }, "expected_destinations": {(2, 1), (3, 1)} }, { "name": "black_pawn_not_first_move", "origin": (2, 1), "layout": { (2, 1): Pawn(Game.BLACK) }, "expected_destinations": {(3, 1)} }, { "name": "black_pawn_attack", "origin": (1, 1), "layout": { (1, 1): Pawn(Game.BLACK), (2, 0): Pawn(Game.WHITE), (2, 2): Pawn(Game.WHITE) }, "expected_destinations": {(2, 1), (3, 1), (2, 0), (2, 2)} }, { "name": "black_pawn_no_attack", "origin": (1, 1), "layout": { (1, 1): Pawn(Game.BLACK), (2, 0): Pawn(Game.BLACK), (2, 2): Pawn(Game.BLACK) }, "expected_destinations": {(2, 1), (3, 1)} }, { "name": "knight", "origin": (4, 3), "layout": { (4, 3): Knight(Game.WHITE) }, "expected_destinations": {(2, 2), (2, 4), (3, 5), (5, 5), (6, 4), (6, 2), (5, 1), (3, 1)} }, { "name": "knight_and_pieces", "origin": (4, 3), "layout": { (4, 3): Knight(Game.WHITE), (3, 1): Pawn(Game.BLACK), (3, 5): Pawn(Game.BLACK), (2, 2): Pawn(Game.WHITE), (2, 4): Pawn(Game.WHITE) }, "expected_destinations": {(3, 5), (5, 5), (6, 4), (6, 2), (5, 1), (3, 1)} }, { "name": "knight_board_edge", "origin": (7, 1), "layout": { (7, 1): Knight(Game.WHITE) }, "expected_destinations": {(5, 0), (5, 2), (6, 3)} }, { "name": "bishop", "origin": (4, 2), "layout": { (4, 2): Bishop(Game.WHITE) }, "expected_destinations": {(3, 3), (2, 4), (1, 5), (0, 6), (5, 3), (6, 4), (7, 5), (5, 1), (6, 0), (3, 1), (2, 0)} }, { "name": "bishop_and_pieces", "origin": (4, 2), "layout": { (4, 2): Bishop(Game.WHITE), (2, 0): Pawn(Game.WHITE), (2, 4): Pawn(Game.WHITE), (6, 4): Pawn(Game.BLACK), (6, 0): Pawn(Game.BLACK) }, "expected_destinations": {(3, 1), (3, 3), (5, 1), (5, 3), (6, 0), (6, 4)} }, { "name": "rook", "origin": (4, 2), "layout": { (4, 2): Rook(Game.WHITE) }, "expected_destinations": {(3, 2), (2, 2), (1, 2), (0, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (5, 2), (6, 2), (7, 2), (4, 1), (4, 0)} }, { "name": "rook_and_pieces", "origin": (4, 2), "layout": { (4, 2): Rook(Game.WHITE), (2, 2): Pawn(Game.WHITE), (4, 4): Pawn(Game.WHITE), (6, 2): Pawn(Game.BLACK), (4, 0): Pawn(Game.BLACK) }, "expected_destinations": {(3, 2), (4, 3), (5, 2), (6, 2), (4, 1), (4, 0)} }, { "name": "queen", "origin": (4, 2), "layout": { (4, 2): Queen(Game.WHITE) }, "expected_destinations": {(3, 3), (2, 4), (1, 5), (0, 6), (5, 3), (6, 4), (7, 5), (5, 1), (6, 0), (3, 1), (2, 0), (3, 2), (2, 2), (1, 2), (0, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (5, 2), (6, 2), (7, 2), (4, 1), (4, 0)} }, { "name": "queen_and_pieces", "origin": (4, 2), "layout": { (4, 2): Queen(Game.WHITE), (2, 0): Pawn(Game.WHITE), (2, 2): Pawn(Game.WHITE), (2, 4): Pawn(Game.WHITE), (4, 4): Pawn(Game.WHITE), (6, 4): Pawn(Game.BLACK), (6, 2): Pawn(Game.BLACK), (6, 0): Pawn(Game.BLACK), (4, 0): Pawn(Game.BLACK) }, "expected_destinations": {(3, 1), (3, 2), (3, 3), (4, 3), (5, 3), (6, 4), (5, 2), (6, 2), (5, 1), (6, 0), (4, 1), (4, 0)} }, { "name": "king", "origin": (4, 2), "layout": { (4, 2): King(Game.WHITE) }, "expected_destinations": {(3, 2), (3, 3), (4, 3), (5, 3), (5, 2), (5, 1), (4, 1), (3, 1)} }, { "name": "king_and_pieces", "origin": (4, 2), "layout": { (4, 2): King(Game.WHITE), (3, 1): Knight(Game.WHITE), (3, 2): Knight(Game.WHITE), (3, 3): Knight(Game.WHITE), (4, 3): Knight(Game.WHITE), (5, 3): Knight(Game.BLACK), (5, 2): Knight(Game.BLACK), (5, 1): Knight(Game.BLACK), (4, 1): Knight(Game.BLACK) }, "expected_destinations": {(5, 3), (5, 2), (5, 1), (4, 1)} } ] for case in cases: with self.subTest(case["name"]): board = Board(case["layout"]) actual_destinations = board._get_destinations_from_origin(case["origin"]) self.assertEqual(case["expected_destinations"], actual_destinations)
def __init__(self, layout: Dict[Tuple[int], Piece] = {}) -> None: default_layout = { (7, 0): Rook(Game.WHITE), (7, 1): Knight(Game.WHITE), (7, 2): Bishop(Game.WHITE), (7, 3): Queen(Game.WHITE), (7, 4): King(Game.WHITE), (7, 5): Bishop(Game.WHITE), (7, 6): Knight(Game.WHITE), (7, 7): Rook(Game.WHITE), (6, 0): Pawn(Game.WHITE), (6, 1): Pawn(Game.WHITE), (6, 2): Pawn(Game.WHITE), (6, 3): Pawn(Game.WHITE), (6, 4): Pawn(Game.WHITE), (6, 5): Pawn(Game.WHITE), (6, 6): Pawn(Game.WHITE), (6, 7): Pawn(Game.WHITE), (1, 0): Pawn(Game.BLACK), (1, 1): Pawn(Game.BLACK), (1, 2): Pawn(Game.BLACK), (1, 3): Pawn(Game.BLACK), (1, 4): Pawn(Game.BLACK), (1, 5): Pawn(Game.BLACK), (1, 6): Pawn(Game.BLACK), (1, 7): Pawn(Game.BLACK), (0, 0): Rook(Game.BLACK), (0, 1): Knight(Game.BLACK), (0, 2): Bishop(Game.BLACK), (0, 3): Queen(Game.BLACK), (0, 4): King(Game.BLACK), (0, 5): Bishop(Game.BLACK), (0, 6): Knight(Game.BLACK), (0, 7): Rook(Game.BLACK) } self._layout = layout or default_layout self._layout_memento = {} self._move = None self._move_memento = None self._moves = []
# [{A4}, {B4}, {C4}, {D4}, {E4}, {F4}, {G4}, {H4}], # [{A3}, {B3}, {C3}, {D3}, {E3}, {F3}, {G3}, {H3}], # [{A2}, {B2}, {C2}, {D2}, {E2}, {F2}, {G2}, {H2}], # [{A1}, {B1}, {C1}, {D1}, {E1}, {F1}, {G1}, {H1}]] import sys, pygame from Piece import Piece,Pawn, King, Queen, Bishop, Knight, Rook from Board import Board pygame.init() size = width, height = 500, 500 black = 0, 255, 255 screen = pygame.display.set_mode(size) board = Board() board_img = pygame.image.load("board.gif") board_img = pygame.transform.scale(board_img, (500, 500)) blackPawn0 = Pawn("blackpawn.png", 1, 0, False, True, "black") pawnImage0 = pygame.image.load(blackPawn0.getImage()) pawnImage0 = pygame.transform.scale(pawnImage0, (50,50)) board.update(1,0,True) blackPawn1 = Pawn("blackpawn.png", 1, 1, False, True, "black") pawnImage1 = pygame.image.load(blackPawn1.getImage()) pawnImage1 = pygame.transform.scale(pawnImage1, (50,50)) board.update(1,1,True) blackPawn2 = Pawn("blackpawn.png", 1, 2, False, True, "black") pawnImage2 = pygame.image.load(blackPawn2.getImage()) pawnImage2 = pygame.transform.scale(pawnImage2, (50,50)) board.update(1,2,True) blackPawn3 = Pawn("blackpawn.png", 1, 3, False, True, "black")