def main(): """ Generates test cases data used in tests. """ for case_nro in range(0, 20): dimension_n = randint(3, 5) dimension_m = randint(3, 5) mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight} board = Board(dimension_n, dimension_m) pieces = [] number_of_pieces = randint(0, 6) for _ in range(0, number_of_pieces): pieces.append(mapping[choice([key for key in mapping.keys()])]()) inputs = { 'n': dimension_n, 'm': dimension_m, 'pieces': [piece.piece_identification for piece in pieces] } print('Generate case with {0}'.format(inputs)) input_filename = os.path.join(TEST_DATA_PATH, 'params_{0}'.format(case_nro)) with open(input_filename, 'w') as input_params_file: input_params_file.write(json.dumps(inputs)) solution_filename = os.path.join(TEST_DATA_PATH, 'solution_{0}'.format(case_nro)) with open(solution_filename, 'a') as output_file: for board in backtracking(board, pieces.copy(), pieces, set()): output_file.write(board.to_json() + '\n')
def main(): """ Main function that instanciates the problem and then calls backtracking solver """ mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight} args = parse_args() board = Board(int(args.n), int(args.m)) pieces = [] for piece_type in args.pieces.split(','): pieces.append(mapping[piece_type]()) res = set() start = time.time() for board in backtracking(board, pieces.copy(), pieces, res, args.animation): if args.output: with open(args.output, 'a') as output_file: if args.output_format == 'json': output_file.write(board.to_json() + '\n') if args.output_format == 'text': output_file.write(draw_board(board) + '\n') elif not args.animation: print(draw_board(board)) end = time.time() print('Total unique configurations found {0}'.format(len(res))) print('Total time {0} seconds'.format(end - start))
def test_to_json_two_pieces(): """test_to_json_with_a_board_with_two_pieces""" king = King() rook = Rook() board = Board(3, 4) board.put(king, 1, 1) board.put(rook, 2, 2) res = json.loads(board.to_json()) expected = json.loads( '{"m": 4, "pieces": {"(2, 2)": "R", "(1, 1)": "K"}, "n": 3}') assert res == expected
def test_very_simple(): """ A board with one position available. test_very_simple_1x1_board_with_one_piece """ expected = [{'pieces': {'(0, 0)': 'K'}, 'n': 1, 'm': 1}] board = Board(1, 1) pieces = [King()] board.put(pieces[0], 0, 0) res = [] for board in backtracking(board, pieces, pieces, set()): res.append(json.loads(board.to_json())) assert res == expected
def test_example_test_case_given(): """ This test case was given as an example. The assert were done manually before the to_json method was done. To make checks easily see: test_with_data which uses a "fuzzer" case generator to verify results. """ expected = [{ 'pieces': { '(2, 0)': 'K', '(1, 2)': 'R', '(0, 0)': 'K' }, 'm': 3, 'n': 3 }, { 'pieces': { '(0, 2)': 'K', '(2, 1)': 'R', '(0, 0)': 'K' }, 'm': 3, 'n': 3 }, { 'pieces': { '(0, 1)': 'R', '(2, 0)': 'K', '(2, 2)': 'K' }, 'm': 3, 'n': 3 }, { 'pieces': { '(0, 2)': 'K', '(1, 0)': 'R', '(2, 2)': 'K' }, 'm': 3, 'n': 3 }] pieces = [King(), King(), Rook()] board = Board(3, 3) res = [] for board in backtracking(board, pieces.copy(), pieces, set()): res.append(json.loads(board.to_json())) assert len(expected) == len(res) for expected_res in expected: assert expected_res in res
def test_example_2_test_case_given(): """ This case was given as an example in the problem """ expected = [{ 'pieces': { '(3, 3)': 'N', '(1, 1)': 'N', '(2, 2)': 'R', '(3, 1)': 'N', '(1, 3)': 'N', '(0, 0)': 'R' }, 'm': 4, 'n': 4 }, { 'pieces': { '(2, 0)': 'N', '(0, 2)': 'N', '(3, 1)': 'R', '(1, 3)': 'R', '(0, 0)': 'N', '(2, 2)': 'N' }, 'm': 4, 'n': 4 }, { 'pieces': { '(3, 3)': 'R', '(1, 1)': 'R', '(2, 0)': 'N', '(2, 2)': 'N', '(0, 0)': 'N', '(0, 2)': 'N' }, 'm': 4, 'n': 4 }, { 'pieces': { '(0, 1)': 'R', '(2, 3)': 'R', '(1, 2)': 'N', '(1, 0)': 'N', '(3, 2)': 'N', '(3, 0)': 'N' }, 'm': 4, 'n': 4 }, { 'pieces': { '(0, 1)': 'N', '(2, 1)': 'N', '(1, 2)': 'R', '(2, 3)': 'N', '(0, 3)': 'N', '(3, 0)': 'R' }, 'm': 4, 'n': 4 }, { 'pieces': { '(0, 1)': 'N', '(2, 3)': 'N', '(2, 1)': 'N', '(1, 0)': 'R', '(3, 2)': 'R', '(0, 3)': 'N' }, 'm': 4, 'n': 4 }, { 'pieces': { '(3, 3)': 'N', '(1, 1)': 'N', '(2, 0)': 'R', '(0, 2)': 'R', '(3, 1)': 'N', '(1, 3)': 'N' }, 'm': 4, 'n': 4 }, { 'pieces': { '(2, 1)': 'R', '(1, 2)': 'N', '(1, 0)': 'N', '(3, 2)': 'N', '(0, 3)': 'R', '(3, 0)': 'N' }, 'm': 4, 'n': 4 }] pieces = [Rook(), Rook(), Knight(), Knight(), Knight(), Knight()] board = Board(4, 4) res = [] for board in backtracking(board, pieces.copy(), pieces, set()): res.append(json.loads(board.to_json())) assert len(expected) == len(res) for expected_res in expected: assert expected_res in res