Beispiel #1
0
    def __init__(self):
        self.board = [[None for j in range(8)] for i in range(8)]
        self.pieces = []
        
        # set white board
        pieces = []
        for i in range(8):
            pieces.append(Pawn(i, 1, COLOR.WHITE))
        pieces.append(Rook(0, 0, COLOR.WHITE))
        pieces.append(Rook(7 ,0, COLOR.WHITE))
        pieces.append(Knight(1, 0, COLOR.WHITE))
        pieces.append(Knight(6, 0, COLOR.WHITE))
        pieces.append(Bishop(2, 0, COLOR.WHITE))
        pieces.append(Bishop(5, 0, COLOR.WHITE))
        pieces.append(King(3, 0, COLOR.WHITE))
        pieces.append(Queen(4, 0, COLOR.WHITE))

        #set black peices
        for i in range(8):
            pieces.append(Pawn(i, 6, COLOR.BLACK))
        pieces.append(Rook(0, 7, COLOR.BLACK))
        pieces.append(Rook(7 ,7, COLOR.BLACK))
        pieces.append(Knight(1, 7, COLOR.BLACK))
        pieces.append(Knight(6, 7, COLOR.BLACK))
        pieces.append(Bishop(2, 7, COLOR.BLACK))
        pieces.append(Bishop(5, 7, COLOR.BLACK))
        pieces.append(King(3, 7, COLOR.BLACK))
        pieces.append(Queen(4, 7, COLOR.BLACK))

        for _piece in pieces:
            self.pieces.append(_piece)
            self.board[_piece._x][_piece._y] = _piece
Beispiel #2
0
 def test_valid_moves_seventh_row_white(self):
     board = Board()
     board.set_config(flat=EMPTY * 64)  # it doesn't matter much
     pawn = Pawn(board, Square(1, 0), WHITE)
     expected = [Square(0, 0)]
     result = pawn.valid_moves()
     self.assertEqual(expected, result)
Beispiel #3
0
 def test_valid_moves_second_row_black(self):
     board = Board()
     board.set_config(flat=EMPTY * 64)  # it doesn't matter much
     pawn = Pawn(board, Square(6, 0), BLACK)
     expected = [Square(7, 0)]
     result = pawn.valid_moves()
     self.assertEqual(expected, result)
Beispiel #4
0
 def test_valid_moves_seventh_row_white(self):
     board = Board()
     board.set_config(flat=EMPTY*64) # it doesn't matter much
     pawn = Pawn(board, Square(1, 0), WHITE)
     expected = [Square(0, 0)]
     result = pawn.valid_moves()
     self.assertEqual(expected, result)
Beispiel #5
0
    def test_place_pawn(self, test_board):
        """
        Explicitly tests the place() method without relying on the
        test_pawn fixture.
        """

        # Check that the intended Space is on the Board with the expected attributes.
        expected_file = random.choice(FILES)
        expected_rank = random.choice(RANKS)
        expected_name = expected_file + str(expected_rank)

        target_space = test_board.get_space(expected_file, expected_rank)

        assert target_space
        assert target_space.file == expected_file
        assert target_space.rank == expected_rank
        assert target_space.name == expected_name

        # Place a Pawn on the intended Space
        expected_color = PieceColor.WHITE
        test_pawn = Pawn(expected_color)

        # Check that the Pawn was created
        assert test_pawn

        test_pawn.place(target_space)

        # Check that the Pawn is on the intended Space
        assert test_pawn.current_space is target_space
        assert test_pawn.moved is False
Beispiel #6
0
 def test_knight_attacks(self):
     knight1 = Knight(Colour.WHITE, (4, 4))
     pawn1 = Pawn(Colour.WHITE, (3, 6))
     pawn2 = Pawn(Colour.BLACK, (3, 2))
     pawn3 = Pawn(Colour.BLACK, (3, 3))
     self.assertEquals(set(knight1.list_attacks([pawn1, pawn2, pawn3])),
                           set([(3, 2)]))
Beispiel #7
0
    def test_white_capture_right(self, test_board, white_test_pawn):
        assert test_board
        assert white_test_pawn
        assert white_test_pawn.current_space.rank < MAX_RANK

        opposing_pawn = Pawn(-(white_test_pawn.color.value))
        assert opposing_pawn

        # Place the opposing Pawn one space in front of and one space to the right of the test Pawn
        target_file = chr(ord(white_test_pawn.current_space.file) + 1)
        target_rank = white_test_pawn.current_space.rank + 1
        target_space = test_board.get_space(target_file, target_rank)
        opposing_pawn.place(target_space)

        # Capture the opposing Pawn with the test Pawn
        white_test_pawn.capture(target_space)

        # Check that the test Pawn is on the opposing Pawn's previous Space
        assert white_test_pawn.current_space is target_space

        # Check that the opposing Pawn has been removed from the board
        assert opposing_pawn.current_space is None

        # Check that the test Pawn is its Space's current piece
        assert target_space.current_piece is white_test_pawn
Beispiel #8
0
    def init_board(self):
        new_b = []
        for i in range(64):
            # white
            if i == 0 or i == 7:
                new_b.append(Tile(Rook('W')))
            elif i == 1 or i == 6:
                new_b.append(Tile(Knight('W')))
            elif i == 2 or i == 5:
                new_b.append(Tile(Bishop('W')))
            elif i == 3:
                new_b.append(Tile(Queen('W')))
            elif i == 4:
                new_b.append(Tile(King('W')))
            elif i >= 8 and i <= 15:
                new_b.append(Tile(Pawn('W')))
            # black
            elif i == 56 or i == 63:
                new_b.append(Tile(Rook('B')))
            elif i == 57 or i == 62:
                new_b.append(Tile(Knight('B')))
            elif i == 58 or i == 61:
                new_b.append(Tile(Bishop('B')))
            elif i == 59:
                new_b.append(Tile(Queen('B')))
            elif i == 60:
                new_b.append(Tile(King('B')))
            elif i >= 48 and i <= 55:
                new_b.append(Tile(Pawn('B')))
            # empty
            else:
                new_b.append(Tile())

        return new_b
Beispiel #9
0
 def add_platform(self, pawn_file, cord, move):
     pawn = Pawn(pawn_file, self.level)
     pawn.rect.x = cord[0]
     pawn.rect.y = cord[1]
     pawn.set_movement(move[0], move[1])
     self.moving_platforms_list.add(pawn)
     return True
Beispiel #10
0
    def fill_board(self):
        for key in self.__letter_mapping:
            self[key + '7'] = Pawn(BLACK, self)
            self[key + '2'] = Pawn(WHITE, self)

        self['A1'] = Rook(WHITE, self)
        self['H1'] = Rook(WHITE, self)
        self['A8'] = Rook(BLACK, self)
        self['H8'] = Rook(BLACK, self)

        self['B1'] = Knight(WHITE, self)
        self['G1'] = Knight(WHITE, self)
        self['B8'] = Knight(BLACK, self)
        self['G8'] = Knight(BLACK, self)

        self['C1'] = Bishop(WHITE, self)
        self['F1'] = Bishop(WHITE, self)
        self['C8'] = Bishop(BLACK, self)
        self['F8'] = Bishop(BLACK, self)

        self['D1'] = Queen(WHITE, self)
        self['D8'] = Queen(BLACK, self)

        self['E1'] = King(WHITE, self)
        self['E8'] = King(BLACK, self)
Beispiel #11
0
 def test_bishop_attacks(self):
     bishop1 = Bishop(Colour.WHITE, (3, 3))
     pawn1 = Pawn(Colour.WHITE, (2, 2))
     pawn2 = Pawn(Colour.BLACK, (4, 4))
     pawn3 = Pawn(Colour.BLACK, (5, 5))
     self.assertEquals(set(bishop1.list_attacks([pawn1, pawn2, pawn3])),
                       set([((4, 4))]))
Beispiel #12
0
 def test_valid_moves_second_row_black(self):
     board = Board()
     board.set_config(flat=EMPTY*64) # it doesn't matter much
     pawn = Pawn(board, Square(6, 0), BLACK)
     expected = [Square(7, 0)]
     result = pawn.valid_moves()
     self.assertEqual(expected, result)
Beispiel #13
0
    def add_pieces(self):
        """ Appends the proper pieces and locations to each team list.
        
        Team lists are used to get the possible moves and easily
        check the capture status of a piece.
        """

        self.wp.append(King(4, 7, True))
        self.wp.append(Queen(3, 7, True))
        self.wp.append(Rook(0, 7, True))
        self.wp.append(Rook(7, 7, True))
        self.wp.append(Knight(1, 7, True))
        self.wp.append(Knight(6, 7, True))
        self.wp.append(Bishop(2, 7, True))
        self.wp.append(Bishop(5, 7, True))
        for i in range(8):
            self.wp.append(Pawn(i, 6, True))

        self.bp.append(King(4, 0, False))
        self.bp.append(Queen(3, 0, False))
        self.bp.append(Rook(0, 0, False))
        self.bp.append(Rook(7, 0, False))
        self.bp.append(Knight(1, 0, False))
        self.bp.append(Knight(6, 0, False))
        self.bp.append(Bishop(2, 0, False))
        self.bp.append(Bishop(5, 0, False))
        for i in range(8):
            self.bp.append(Pawn(i, 1, False))
Beispiel #14
0
    def test_attacks_right_border_white(self):
        board = Board()
        pawn = Pawn(board, Square(6, 7), WHITE)
        board.matrix[5][6] = "P"

        expected = [Square(5, 6)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #15
0
    def test_valid_moves_with_attacks(self):
        board = Board()
        pawn = Pawn(board, Square(2, 0), BLACK)
        board.matrix[3][1] = "p"

        expected = [Square(3, 0), Square(4, 0), Square(3, 1)]
        result = pawn.valid_moves()
        self.assertEqual(expected, result)
Beispiel #16
0
    def test_attacks_right_border_black(self):
        board = Board()
        pawn = Pawn(board, Square(2, 0), BLACK)
        board.matrix[3][1] = "p"

        expected = [Square(3, 1)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #17
0
    def test_attacks_right_border_black(self):
        board = Board()
        pawn = Pawn(board, Square(2, 0), BLACK)
        board.matrix[3][1] = "p"

        expected = [Square(3, 1)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #18
0
 def test_king_moves(self):
     king1 = King(Colour.WHITE, (4, 4))
     pawn1 = Pawn(Colour.WHITE, (4, 5))
     pawn2 = Pawn(Colour.WHITE, (4, 3))
     pawn3 = Pawn(Colour.WHITE, (3, 3))
     pawn4 = Pawn(Colour.WHITE, (5, 5))
     self.assertEquals(set(king1.list_moves([pawn1, pawn2, pawn3, pawn4])),
                       set([(3, 5), (5, 3), (5, 4), (3, 4)]))
Beispiel #19
0
    def test_valid_moves_with_attacks(self):
        board = Board()
        pawn = Pawn(board, Square(2, 0), BLACK)
        board.matrix[3][1] = "p"

        expected = [Square(3, 0), Square(4, 0), Square(3, 1)]
        result = pawn.valid_moves()
        self.assertEqual(expected, result)
Beispiel #20
0
    def test_attacks_right_border_white(self):
        board = Board()
        pawn = Pawn(board, Square(6, 7), WHITE)
        board.matrix[5][6] = "P"

        expected = [Square(5, 6)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #21
0
 def test_translate_x_should_move_the_pawn_to_the_right(self):
     # Given
     expected = Pawn(1, 4, Orientation.WEST)
     # When
     actual = translate_x(Pawn(0, 4, Orientation.WEST), 1)
     # Then
     self.assertEqual(actual, expected,
                      "The pawn should be placed one square after")
Beispiel #22
0
 def test_translate_y_should_move_the_pawn_to_the_bottom(self):
     # Given
     expected = Pawn(0, 5, Orientation.WEST)
     # When
     actual = translate_y(Pawn(0, 4, Orientation.WEST), 1)
     # Then
     self.assertEqual(actual, expected,
                      "The pawn should be placed one square on the bottom")
Beispiel #23
0
 def test_act_should_move_the_pawn_one_square_to_the_right(self):
     # Given
     pawn = Pawn(0, 8, Orientation.WEST)
     expected = Pawn(2, 8, Orientation.WEST)
     # When
     actual = game.act(Action.RIGHT, pawn)
     # Then
     self.assertEqual(actual, expected,
                      "The pawn should move one square to the right")
Beispiel #24
0
    def test_attacks_middle_position_white(self):
        board = Board()
        pawn = Pawn(board, Square(6, 3), WHITE)
        board.matrix[5][2] = "P"
        board.matrix[5][4] = "P"

        expected = [Square(5, 2), Square(5, 4)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #25
0
    def test_attacks_middle_position_black(self):
        board = Board()
        pawn = Pawn(board, Square(2, 3), BLACK)
        board.matrix[3][2] = "p"
        board.matrix[3][4] = "p"

        expected = [Square(3, 2), Square(3, 4)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #26
0
 def test_pawn_moves(self):
     pawn1 = Pawn(Colour.WHITE, (0, 0))
     pawn2 = Pawn(Colour.BLACK, (0, 1))
     pawn3 = Pawn(Colour.BLACK, (0, 0))
     self.assertEquals(pawn1.list_moves([]), [(0, 1), (0, 2)])
     pawn1.position = (0, 0)
     self.assertEquals(pawn1.list_moves([]), [(0, 1)])
     self.assertEquals(pawn1.list_moves([pawn2]), [])
     self.assertEquals(pawn3.list_moves([]), [])
Beispiel #27
0
    def test_attacks_middle_position_black(self):
        board = Board()
        pawn = Pawn(board, Square(2, 3), BLACK)
        board.matrix[3][2] = "p"
        board.matrix[3][4] = "p"

        expected = [Square(3, 2), Square(3, 4)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #28
0
    def test_attacks_middle_position_white(self):
        board = Board()
        pawn = Pawn(board, Square(6, 3), WHITE)
        board.matrix[5][2] = "P"
        board.matrix[5][4] = "P"

        expected = [Square(5, 2), Square(5, 4)]
        result = pawn.attacking_moves()
        self.assertEqual(expected, result)
Beispiel #29
0
 def test_init(self):
     # test valido
     xs = [chr(ord('a') + i) for i in range(0, 8)]
     ys = [i for i in range(1, 8)]
     for x, y in product(xs, ys):
         p = Pawn(x, y)
         try:
             self.assertTrue([(x, y + 1)] == p.possible_poss())
         except:
             print(f'{x},{y} should be valid with {x}{y+1}')
Beispiel #30
0
 def test_act_should_move_the_pawn_one_square_to_the_right(self):
     # Given
     pawn = Pawn(2, 8, Orientation.WEST)
     fences = [[Direction.NO for i in range(FENCE_SIZE)]
               for j in range(FENCE_SIZE)]
     expected = Pawn(0, 8, Orientation.WEST)
     # When
     actual = game.act(Action.LEFT, pawn, fences)
     # Then
     self.assertEqual(actual, expected,
                      "The pawn should move one square to the left")
Beispiel #31
0
def white_test_pawn(test_board):
    assert test_board

    starting_space = test_board.get_space("e", 2)
    test_pawn = Pawn(PieceColor.WHITE)

    test_pawn.place(starting_space)

    assert test_pawn
    assert test_pawn.current_space is starting_space

    return test_pawn
Beispiel #32
0
def black_test_pawn(test_board):
    assert test_board

    starting_space = test_board.get_space("e", 7)
    test_pawn = Pawn(PieceColor.BLACK)

    test_pawn.place(starting_space)

    assert test_pawn
    assert test_pawn.current_space is starting_space

    return test_pawn
Beispiel #33
0
    def test_attack_range(self):
        pawn1 = Pawn(("a", 2), "white")
        pawn2 = Pawn(("b", 3), "black")
        pawn3 = Pawn(("d", 5), "white")

        assert pawn1.attack_range(pawn2) == True
        assert pawn2.attack_range(pawn1) == True
        with self.assertRaises(ValueError):
            pawn1.attack_range(pawn3)
Beispiel #34
0
 def test_init_game_should_add_the_pawn_in_the_center_of_the_base_line(
         self):
     # Given
     expected = [
         Pawn(0, 8, Orientation.WEST),
         Pawn(16, 8, Orientation.EAST)
     ]
     # When
     actual = game.init_game()
     # Then
     self.assertEqual(
         actual, expected,
         "The pawn should be placed in the center of his base line")
Beispiel #35
0
 def setBoard(self, fen):
     row, column = 0, 0
     for character in fen:
         if character == 'R':
             self.addPiece(Rook(column, row, 'w', self.board.board))
             column += 1
         elif character == 'r':
             self.addPiece(Rook(column, row, 'b', self.board.board))
             column += 1
         elif character == 'N':
             self.addPiece(Knight(column, row, 'w', self.board.board))
             column += 1
         elif character == 'n':
             self.addPiece(Knight(column, row, 'b', self.board.board))
             column += 1
         elif character == 'B':
             self.addPiece(Bishop(column, row, 'w', self.board.board))
             column += 1
         elif character == 'b':
             self.addPiece(Bishop(column, row, 'b', self.board.board))
             column += 1
         elif character == 'P':
             self.addPiece(Pawn(column, row, 'w', self.board.board))
             column += 1
         elif character == 'p':
             self.addPiece(Pawn(column, row, 'b', self.board.board))
             column += 1
         elif character == 'K':
             self.addPiece(King(column, row, 'w', self.board.board))
             column += 1
         elif character == 'k':
             self.addPiece(King(column, row, 'b', self.board.board))
             column += 1
         elif character == 'Q':
             self.addPiece(Queen(column, row, 'w', self.board.board))
             column += 1
         elif character == 'q':
             self.addPiece(Queen(column, row, 'b', self.board.board))
             column += 1
         elif character == '/':
             column = 0
             row += 1
         else:
             if character >= '1' and character <= '9':
                 column += int(character)
             elif character == ' ':
                 i = fen.index(character) + 1
                 self.whitesTurn = True if fen[i] == 'w' else False
                 return
Beispiel #36
0
 def test_pawn_init(self):
     loc = ("a", 1)
     pawn = Pawn(loc, "white")
     assert pawn.location == ("a", 1)
     assert pawn.has_moved == False
     assert pawn.board == None
     assert pawn.value == 1
Beispiel #37
0
 def putBlackPawns(self,black_pawns_list):
     for i in range(8):
         sq = self.squares[6][i]
         new_spot = self.chess_coord[sq]
         new_pawn = Pawn(new_spot, False)
         new_pawn.ficha.setpos(new_pawn.spot)
         black_pawns_list.append(new_pawn)
Beispiel #38
0
    def _init_pieces(self):
        if not self._positions:
            king = King(color=self._color)
            self._positions.update({str(king.position): king})

            queen = Queen(color=self._color)
            self._positions.update({str(queen.position): queen})

            for i in range(1, 9):
                pawn = Pawn(self._color, col=i)
                self._positions.update({str(pawn.position): pawn})

            knight = Knight(self._color, col=2)
            self._positions.update({str(knight.position): knight})

            knight = Knight(self._color, col=7)
            self._positions.update({str(knight.position): knight})

            rook = Rook(self._color, col=1)
            self._positions.update({str(rook.position): rook})

            rook = Rook(self._color, col=8)
            self._positions.update({str(rook.position): rook})

            bishop = Bishop(self._color, col=3)
            self._positions.update({str(bishop.position): bishop})

            bishop = Bishop(self._color, col=6)
            self._positions.update({str(bishop.position): bishop})
Beispiel #39
0
 def __init__(self, build_json, level):
     self.direction = 'R'
     self.weapon_damage = 25
     self._player_max_hp = 100
     self._player_hp = 100
     self._invuln_timer = 90
     self._shooting_timer = 30
     self.cycle_frame = 0
     self.climb_frame = 0
     self._falling = True
     self._shooting = False
     self._climbing = False
     self._invuln = False
     self._blink = True
     self._damage = False
     self._damage_counter = 15
     Pawn.__init__(self, build_json, level)
     self.rect = pygame.Rect((self.char_x/3, self.char_y/4),
                             ((self.char_x*2/3), self.char_y))
     self._bottom_catch = pygame.Rect(-10, level.height+10, level.width+20, 500) 
Beispiel #40
0
 def test_valid_moves_initial_position_white(self):
     pawn = Pawn(Board(), Square(6, 3), WHITE)
     expected = [Square(5, 3), Square(4, 3)]
     result = pawn.valid_moves()
     self.assertEqual(expected, result)
Beispiel #41
0
 def test_valid_moves_initial_position_black(self):
     pawn = Pawn(Board(), Square(1, 3), BLACK)
     expected = [Square(2, 3), Square(3, 3)]
     result = pawn.valid_moves()
     self.assertEqual(expected, result)