예제 #1
0
    def test_calculate_group(self):
        queen = ChessPiece(ChessPiece.QUEEN)
        rook = ChessPiece(ChessPiece.ROOK)

        starting_pieces = [[queen, 2]]
        table = ChessTable(starting_pieces)
        table.x_size = self.X_SIZE
        table.y_size = self.Y_SIZE
        table.prepare()

        table_for_start_of_round = ChessTable()
        table_for_start_of_round.x_size = self.X_SIZE
        table_for_start_of_round.y_size = self.Y_SIZE
        table_for_start_of_round.prepare()

        #lets start with queens
        combination = [0, 0, 0, 0, queen, 0, 0, 0, 0, 0, queen, 0, 0, 0, 0, 0]
        true_result = [1, 1, 1, 0, queen, 1, 1, 1, 1, 1, queen, 1, 1, 1, 1, 1]
        line = 0

        self.assertEqual(
            table.calculate_group(table, table_for_start_of_round,
                combination,
                line).start_table ,
            true_result,
            "Didn't returned correct table")

        #impossible combination should result false
        combination = [0, 0, 0, 0, queen, 0, 0, 0, 0, queen, 0, 0, 0, 0, 0, 0]
        true_result = False
        self.assertEqual(
            table.calculate_group(table, table_for_start_of_round,
                combination, line),
            true_result,
            "Didn't return False for impossible table")

        #is going findout to next piece kind
        starting_pieces = [
            [queen, 2],
            [rook, 1]]

        table = ChessTable(starting_pieces)
        table.x_size = self.X_SIZE
        table.y_size = self.Y_SIZE
        table.prepare()

        combination = [0, 0, 0, 0, queen, 0, 0, 0, 0, 0, queen, 0, 0, 0, 0, 0]
        true_result = True

        self.assertEqual(
            table.calculate_group(table, table_for_start_of_round,
                combination, line),
            true_result,
            "Didn't start recursive function")
예제 #2
0
    def test_bishop_danger_zones(self):
        test_table = ChessTable()
        test_table.x_size = self.X_SIZE
        test_table.y_size = self.Y_SIZE
        test_table.prepare()

        piece = ChessPiece(ChessPiece.BISHOP)
        test_table.start_table[10] = piece

        true_result = [[1, 0, 0, 0], [0, 1, 0, 1], [0, 0, piece, 0], [0, 1, 0, 1]]

        self.assertEqual(piece.bishop_danger_zones(test_table, 2, 2).get_2d_table(),
                true_result,
                "Bishops threating wrong tiles")
예제 #3
0
    def test_king_danger_zones(self):
        test_table = ChessTable()
        test_table.x_size = self.X_SIZE
        test_table.y_size = self.Y_SIZE
        test_table.prepare()

        piece = ChessPiece(ChessPiece.KING)
        test_table.start_table[10] = piece

        true_result = [[0, 0, 0, 0], [0, 1, 1, 1], [0, 1, piece, 1], [0, 1, 1, 1]]

        self.assertEqual(piece.king_danger_zones(test_table, 2, 2).get_2d_table(),
                true_result,
                "King threating wrong tiles")
예제 #4
0
    def test_bishop_danger_zones(self):
        test_table = ChessTable()
        test_table.x_size = self.X_SIZE
        test_table.y_size = self.Y_SIZE
        test_table.prepare()

        piece = ChessPiece(ChessPiece.BISHOP)
        test_table.start_table[10] = piece

        true_result = [[1, 0, 0, 0], [0, 1, 0, 1], [0, 0, piece, 0],
                       [0, 1, 0, 1]]

        self.assertEqual(
            piece.bishop_danger_zones(test_table, 2, 2).get_2d_table(),
            true_result, "Bishops threating wrong tiles")
예제 #5
0
    def test_king_danger_zones(self):
        test_table = ChessTable()
        test_table.x_size = self.X_SIZE
        test_table.y_size = self.Y_SIZE
        test_table.prepare()

        piece = ChessPiece(ChessPiece.KING)
        test_table.start_table[10] = piece

        true_result = [[0, 0, 0, 0], [0, 1, 1, 1], [0, 1, piece, 1],
                       [0, 1, 1, 1]]

        self.assertEqual(
            piece.king_danger_zones(test_table, 2, 2).get_2d_table(),
            true_result, "King threating wrong tiles")
예제 #6
0
    def test_fill_danger_zones(self):
        test_table = ChessTable()
        test_table.x_size = self.X_SIZE
        test_table.y_size = self.Y_SIZE
        test_table.prepare()

        queen = ChessPiece(ChessPiece.QUEEN)

        line = 5
        test_table.start_table[line] = queen

        true_result = [1, 1, 1, 0, 1, queen, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1]

        self.assertEqual(queen.fill_danger_zones(test_table, line).start_table,
                true_result,
                "There is a problem in fill_danger_zones")
예제 #7
0
    def test_fill_danger_zones(self):
        test_table = ChessTable()
        test_table.x_size = self.X_SIZE
        test_table.y_size = self.Y_SIZE
        test_table.prepare()

        queen = ChessPiece(ChessPiece.QUEEN)

        line = 5
        test_table.start_table[line] = queen

        true_result = [1, 1, 1, 0, 1, queen, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1]

        self.assertEqual(
            queen.fill_danger_zones(test_table, line).start_table, true_result,
            "There is a problem in fill_danger_zones")
예제 #8
0
    def test_render_table(self):
        """
        just for fun
        """
        table = ChessTable([])
        table.x_size = self.X_SIZE
        table.y_size = self.Y_SIZE
        table.prepare()

        queen = ChessPiece(ChessPiece.QUEEN)
        rook = ChessPiece(ChessPiece.ROOK)
        knight = ChessPiece(ChessPiece.KNIGHT)
        king = ChessPiece(ChessPiece.KING)
        bishop = ChessPiece(ChessPiece.BISHOP)

        table.start_table = [1, 1, rook, knight, king, queen, bishop, 1, 1, 1, 0, 0, 0, 0, 0, 0]
        true_render = "|.|.|R|N\n|K|Q|B|.\n|.|.|.|.\n|.|.|.|.\n"

        self.assertEqual(table.render_table(),true_render)
예제 #9
0
    def test_calculate_for_piece(self):
        queen = ChessPiece(ChessPiece.QUEEN)

        starting_pieces = [[queen, 2]]
        table = ChessTable(starting_pieces)
        table.x_size = self.X_SIZE
        table.y_size = self.Y_SIZE
        table.prepare()

        table_for_start_of_round = ChessTable()
        table_for_start_of_round.x_size = self.X_SIZE
        table_for_start_of_round.y_size = self.Y_SIZE
        table_for_start_of_round.prepare()

        #trying a possible combination
        combination = [0, 0, 0, 0, queen, 0, 0, 0, 0, 0, queen, 0, 0, 0, 0, 0]
        true_result = [1, 1, 1, 0, queen, 1, 1, 1, 1, 1, queen, 1, 1, 1, 1, 1]
        line = 0

        successful, table_for_this_round = table.calculate_for_piece(
            table,
            table_for_start_of_round,
            combination,
            line
        )

        #trying an impossible combination
        true_result = (True, [1, 1, 1, 0, queen, 1, 1, 1, 1, 1, queen, 1, 1, 1, 1, 1])
        self.assertEqual((successful, table_for_this_round.start_table),true_result)

        combination = [0, 0, 0, 0, queen, 0, 0, 0, 0, queen, 0, 0, 0, 0, 0, 0]
        true_result = [1, 1, 1, 0, queen, 1, 1, 1, 1, 1, queen, 1, 1, 1, 1, 1]
        line = 0

        true_result = (False, [1, 1, 0, 0, queen, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0])

        successful, table_for_this_round = table.calculate_for_piece(
            table,
            table_for_start_of_round,
            combination,
            line
        )

        self.assertEqual((successful, table_for_this_round.start_table),true_result)
예제 #10
0
def main():
    """
    Main method for calculating unique chess configurations.
    Starts recursive method (find_place) in order to do its work,
    does time tracking and other eye candy things.
    """
    time_start = time.time()

    parser = argparse.ArgumentParser(description="""Finds all unique
    configurations of a set of normal chess pieces on a chess board
    """)

    parser.add_argument("x", help="number of columns in chess table", type=int)
    parser.add_argument("y", help="number of rows in chess table", type=int)

    parser.add_argument('--king',
                        type=int,
                        help='Number of kings in configurations')
    parser.add_argument('--queen',
                        type=int,
                        help='Number of queens in configurations')
    parser.add_argument('--rook',
                        type=int,
                        help='Number of rooks in configurations')
    parser.add_argument('--knight',
                        type=int,
                        help='Number of knights in configurations')
    parser.add_argument('--bishop',
                        type=int,
                        help='Number of bishops in configurations')
    parser.add_argument('--render',
                        type=int,
                        default=3,
                        help='Number of tables to render, default = 3')

    args = parser.parse_args()

    #pieces configuration
    total_pieces = 0
    starting_pieces = []
    if args.king:
        starting_pieces.append([ChessPiece(ChessModel.KING), args.king])
        total_pieces += args.king
    if args.queen:
        starting_pieces.append([ChessPiece(ChessModel.QUEEN), args.queen])
        total_pieces += args.queen
    if args.rook:
        starting_pieces.append([ChessPiece(ChessModel.ROOK), args.rook])
        total_pieces += args.rook
    if args.knight:
        starting_pieces.append([ChessPiece(ChessModel.KNIGHT), args.knight])
        total_pieces += args.knight
    if args.bishop:
        starting_pieces.append([ChessPiece(ChessModel.BISHOP), args.bishop])
        total_pieces += args.bishop

    table = ChessTable(starting_pieces)
    table.x_size = args.x
    table.y_size = args.y
    table.prepare()

    good_config = True
    if len(starting_pieces) == 0:
        print "You need to add at least one type of chess piece"
        good_config = False

    if args.x * args.y < total_pieces:
        print "Not enough tiles for all pieces."
        good_config = False

    if not good_config:
        #configuration error
        parser.print_usage()
        sys.exit()

    #start solving
    successful_tables = table.solve()

    if len(successful_tables) > 0:
        print "Latest 3 tables:"
        for i in successful_tables[:args.render]:
            print i.render_table()

        print "Total possibilities:"
        print len(successful_tables)

        print "Time in seconds:"
        time_end = time.time()
        print time_end - time_start
    else:
        print "Couldn't Found any combination"