コード例 #1
0
 def test_queen_attacks(self):
     queen = create_piece('q', 0, 0)
     for i in range(1, 8):
         self.assertTrue(queen.is_attacking_position(i, i))
         self.assertTrue(queen.is_attacking_position(i, 0))
         self.assertTrue(queen.is_attacking_position(0, i))
         self.assertFalse(queen.is_attacking_position(i, i + 1))
コード例 #2
0
def place_pieces(board_list, board, pieces, index=0, offset=0):
    """
    Backtracking algorithm to place a list of pieces in the board.

    :param board_list: list of found boards
    :param board: current board
    :param pieces: list of pieces
    :param index: current piece index
    :param offset: current starting offset
    :return:
    """
    if index == len(pieces):
        board_list.append(board)
    else:
        current_offset = offset
        piece = create_piece(pieces[index])
        while current_offset < (board.rows * board.columns):
            try:
                used_offset = board.place_piece(piece, current_offset)
            except ValueError:
                break
            place_pieces(board_list, copy.deepcopy(board), pieces, index + 1,
                         used_offset + 1)
            board.remove_piece(piece)
            current_offset = used_offset + 1
コード例 #3
0
 def place_piece(self, piece, offset):
     """
     Place a piece in the next available position starting from the offset.
     """
     if offset >= self.rows * self.columns:
         raise ValueError("Out of Bounds")
     for next_offset in range(offset, self.rows * self.columns):
         row, column = self.get_row_column(next_offset)
         if self.is_safe(create_piece(piece.name, row, column)):
             piece.row, piece.column = row, column
             self.pieces.append(piece)
             return next_offset
     raise ValueError("Out of Bounds")
コード例 #4
0
 def test_create_pieces(self):
     self.assertEqual(create_piece('k'), King())
     self.assertEqual(create_piece('q'), Queen())
     self.assertEqual(create_piece('b'), Bishop())
     self.assertEqual(create_piece('r'), Rook())
     self.assertEqual(create_piece('n'), Knight())
コード例 #5
0
 def test_knight_attacks(self):
     knight = create_piece('n', 0, 0)
     self.assertTrue(knight.is_attacking_position(1, 2))
     self.assertTrue(knight.is_attacking_position(2, 1))
     self.assertFalse(knight.is_attacking_position(1, 1))
コード例 #6
0
 def test_bishop_attacks(self):
     bishop = create_piece('b', 0, 0)
     for i in range(1, 8):
         self.assertTrue(bishop.is_attacking_position(i, i))
         self.assertFalse(bishop.is_attacking_position(0, i))
コード例 #7
0
 def test_rook_attacks(self):
     rook = create_piece('r', 0, 0)
     for i in range(1, 8):
         self.assertTrue(rook.is_attacking_position(0, i))
         self.assertTrue(rook.is_attacking_position(i, 0))
         self.assertFalse(rook.is_attacking_position(i, i))
コード例 #8
0
 def test_king_attacks(self):
     king = create_piece('k', 0, 0)
     self.assertTrue(king.is_attacking_position(1, 1))
     self.assertFalse(king.is_attacking_position(2, 2))
コード例 #9
0
 def test_is_safe(self):
     k1 = create_piece('k')
     q1 = create_piece('q', 1, 2)
     board = Board(3, 3)
     board.place_piece(k1, 0)
     self.assertTrue(board.is_safe(q1))
コード例 #10
0
 def test_place_pieces(self):
     k1 = create_piece('k')
     k2 = create_piece('k')
     board = Board(3, 3)
     self.assertEqual(board.place_piece(k1, 0), 0)
     self.assertEqual(board.place_piece(k2, 0), 2)