Beispiel #1
0
    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
Beispiel #2
0
    def test_can_capture_king_attacker(self):
        cases = [
            {
                "name": "can_capture_king_attacker",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Knight(Game.BLACK),
                    (6, 0): Rook(Game.WHITE)
                },
                "formatted_string": "RB2A2",
                "expected_can_capture": True
            },
            {
                "name": "cannot_capture_king_attacker",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Knight(Game.BLACK),
                    (6, 0): Rook(Game.WHITE),
                    (6, 3): Bishop(Game.WHITE)
                },
                "formatted_string": "RB2A2",
                "expected_can_capture": False
            },
            {
                "name": "cannot_capture_king_attacker_makes_check",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Knight(Game.BLACK),
                    (6, 0): Rook(Game.WHITE),
                    (6, 3): Bishop(Game.WHITE)
                },
                "formatted_string": "RB2A2",
                "expected_can_capture": False
            },
            {
                "name": "cannot_capture_king_attacker_two_attackers",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Knight(Game.BLACK),
                    (6, 0): Rook(Game.WHITE),
                    (4, 2): Knight(Game.WHITE)
                },
                "formatted_string": "NA3C4",
                "expected_can_capture": False
            }
        ]
        for case in cases:
            with self.subTest(case["name"]):
                board = Board(case["layout"])
                board.create_move(case["formatted_string"])

                actual_can_capture = board.can_capture_king_attacker()

                self.assertEqual(case["expected_can_capture"], actual_can_capture)
Beispiel #3
0
    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
Beispiel #4
0
 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))
Beispiel #5
0
    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 = []
Beispiel #6
0
 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)
Beispiel #7
0
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 10 23:52:45 2020
sudoku yap
@author: ozan
"""

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)
#
Beispiel #8
0
    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)
Beispiel #9
0
    def test_can_block_king_attacker(self):
        cases = [
            {
                "name": "can_block_king_attacker_up_vertical",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (0, 3): Rook(Game.WHITE),
                    (1, 4): Queen(Game.BLACK)
                },
                "formatted_string": "RC8D8",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_up_right_diagonal",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (0, 6): Bishop(Game.WHITE),
                    (1, 4): Queen(Game.BLACK)
                },
                "formatted_string": "BH7G8",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_right_horizontal",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (3, 6): Rook(Game.WHITE),
                    (1, 4): Queen(Game.BLACK)
                },
                "formatted_string": "RG6G5",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_down_right_diagonal",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (6, 6): Bishop(Game.WHITE),
                    (1, 4): Queen(Game.BLACK)
                },
                "formatted_string": "BF1G2",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_down_vertical",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (6, 3): Rook(Game.WHITE),
                    (5, 2): Queen(Game.BLACK)
                },
                "formatted_string": "RB2A2",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_down_left_diagonal",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (6, 0): Bishop(Game.WHITE),
                    (5, 2): Queen(Game.BLACK)
                },
                "formatted_string": "BB1A2",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_left_horizontal",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (3, 0): Rook(Game.WHITE),
                    (5, 2): Queen(Game.BLACK)
                },
                "formatted_string": "RA4A5",
                "expected_can_block": True
            },
            {
                "name": "can_block_king_attacker_up_left_diagonal",
                "layout": {
                    (3, 3): King(Game.BLACK),
                    (0, 0): Queen(Game.WHITE),
                    (5, 2): Queen(Game.BLACK)
                },
                "formatted_string": "QA7A8",
                "expected_can_block": True
            },
            {
                "name": "cannot_block_king_attacker",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Bishop(Game.BLACK),
                    (6, 0): Rook(Game.WHITE)
                },
                "formatted_string": "RB2A2",
                "expected_can_block": False
            },
            {
                "name": "cannot_block_king_attacker_makes_check",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Knight(Game.BLACK),
                    (6, 0): Rook(Game.WHITE),
                    (6, 3): Bishop(Game.WHITE)
                },
                "formatted_string": "RB2A2",
                "expected_can_block": False
            },
            {
                "name": "cannot_block_king_attacker_two_attackers",
                "layout": {
                    (3, 0): King(Game.BLACK),
                    (5, 2): Knight(Game.BLACK),
                    (6, 0): Rook(Game.WHITE),
                    (4, 2): Knight(Game.WHITE)
                },
                "formatted_string": "NA3C4",
                "expected_can_block": False
            }
        ]
        for case in cases:
            with self.subTest(case["name"]):
                board = Board(case["layout"])
                board.create_move(case["formatted_string"])

                actual_can_block = board.can_block_king_attacker()

                self.assertEqual(case["expected_can_block"], actual_can_block)
Beispiel #10
0
blackQueen = Queen("blackqueen.png", 0, 3, False, True, "black")
blackQueenImage = pygame.image.load(blackQueen.getImage())
blackQueenImage = pygame.transform.scale(blackQueenImage, (50, 50))
board.update(0,3,True)

whiteKing = King("whiteking.png",7, 4, False, True, "white")
whiteKingImage = pygame.image.load(whiteKing.getImage())
whiteKingImage = pygame.transform.scale(whiteKingImage, (50,50))
board.update(7,4,True)

whiteQueen = Queen("whitequeen.png", 7, 3, False, True, "white")
whiteQueenImage = pygame.image.load(whiteQueen.getImage())
whiteQueenImage = pygame.transform.scale(whiteQueenImage, (50, 50))
board.update(7,3,True)

blackBishopL = Bishop("blackbishop.png",0, 2, False, True, "black")
blackBishopLImage = pygame.image.load(blackBishopL.getImage())
blackBishopLImage = pygame.transform.scale(blackBishopLImage, (50,50))
board.update(0,2,True)

blackBishopR = Bishop("blackbishop.png", 0,5,False,True,"black")
blackBishopRImage = pygame.image.load(blackBishopR.getImage())
blackBishopRImage = pygame.transform.scale(blackBishopRImage, (50,50))
board.update(0,5,True)

blackKnightL = Knight("blackknight.png", 0,1,False,True,"black")
blackKnightLImage = pygame.image.load(blackKnightL.getImage())
blackKnightLImage = pygame.transform.scale(blackKnightLImage, (50,50))
board.update(0,1,True)

blackKnightR = Knight("blackknight.png", 0,6,False,True,"black")