def test_get_coordinates(self): arr = numpy.array([6, 2, 3, 4, 1, 5, 1, 5, 3, 1, 6, 2, 5, 0, 4]) board = GameBoard(verbose=False) board.create_game_from_array(arr) x, y = board.get_coordinates() self.assertEqual(x, 3) self.assertEqual(y, 2)
self.board.move_up() return False if __name__ == '__main__': pattern_dictionary = File.load_binary('pattern_dictionary.bin') i = 0 for key in pattern_dictionary: percent = i / len(pattern_dictionary) * 100 max_iter = 35 i += 1 if pattern_dictionary[key]['steps'] == -1: while True: stdout.write("\rProgress: {} Iteration: {}".format( percent, max_iter)) stdout.flush() board = GameBoard(verbose=False) board.create_game_from_array(pattern_dictionary[key]['array']) dfs = DepthFirstSearch(board, max_iter) result = dfs.search() if result: pattern_dictionary[key]['steps'] = dfs.steps dfs.move_sequence.reverse() pattern_dictionary[key]['moves'] = dfs.move_sequence File.save_binary('pattern_dictionary.bin', pattern_dictionary) break max_iter += 1 print() File.save_binary('pattern_dictionary.bin', pattern_dictionary)
def test_top_row_solved(self): arr1 = numpy.array([0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 6, 5, 5, 4]) board = GameBoard(verbose=False) board.create_game_from_array(arr1) self.assertFalse(board.top_row_solved()[0]) arr2 = numpy.array([0, 1, 3, 3, 4, 2, 2, 1, 1, 1, 6, 6, 5, 5, 4]) board = GameBoard(verbose=False) board.create_game_from_array(arr2) self.assertFalse(board.top_row_solved()[0]) self.assertEqual(board.top_row_solved()[1], [0, 3]) arr3 = numpy.array([0, 2, 3, 4, 1, 6, 1, 5, 3, 1, 6, 2, 5, 5, 4]) board = GameBoard(verbose=False) board.create_game_from_array(arr3) self.assertFalse(board.top_row_solved()[0]) self.assertEqual(board.top_row_solved()[1], [0]) arr4 = numpy.array([6, 2, 3, 4, 1, 0, 1, 5, 3, 1, 6, 2, 5, 5, 4]) board = GameBoard(verbose=False) board.create_game_from_array(arr4) self.assertTrue(board.top_row_solved()[0]) self.assertEqual(board.top_row_solved()[1], [])
def test_pattern_solved(self): arr = numpy.array( [-1, -1, -1, -1, -1, 1, 1, 0, -1, -1, -1, -1, -1, -1, -1]) board = GameBoard(verbose=False) board.create_game_from_array(arr) self.assertFalse(board.pattern_solved()) arr = numpy.array( [-1, -1, -1, 1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 1, -1]) board = GameBoard(verbose=False) board.create_game_from_array(arr) self.assertTrue(board.pattern_solved()) arr = [-1, -1, -1, -1, -1, 1, 1, 0, -1, -1, -1, -1, -1, -1, -1] board = GameBoard(verbose=False) board.create_game_from_array(arr) self.assertFalse(board.pattern_solved())
def test_search(self): board = GameBoard(verbose=False) board.create_random_game(GameDifficultyEnum.MASTER) astar = BestFirstSearch(board) astar.search()
if array[i] == array[i + 10]: transformed_array[i] = 2 transformed_array[i + 10] = 2 transformed_array[pair[0]] = 1 transformed_array[pair[1]] = 1 transformed_array[zero_index] = 0 return transformed_array if __name__ == "__main__": c = 0 for i in range(100000): percent = i / 100000 * 100 stdout.write("\rNovice: {}".format(percent)) stdout.flush() board = GameBoard(verbose=False) board.create_random_game(GameDifficultyEnum.NOVICE) astar = BestFirstSearch(board) astar.search() if not board.game_cleared(): c += 1 print() print("Errors: {}".format(c)) print() c = 0 for i in range(100000): percent = i / 100000 * 100 stdout.write("\rApprentice: {}".format(percent)) stdout.flush() board = GameBoard(verbose=False) board.create_random_game(GameDifficultyEnum.APPRENTICE)
def create_mapping(self): pattern_dictionary = File.load_binary('pattern_dictionary.bin') for key in pattern_dictionary: initial_mapping = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ] array = pattern_dictionary[key]['array'] current_position = [i for i, j in enumerate(array) if j == 0] movements = pattern_dictionary[key]['moves'] board = GameBoard(verbose=False) board.create_game_with_mapping(initial_mapping, current_position[0]) for move in movements: if move == 'r': board.move_right() elif move == 'l': board.move_left() elif move == 'd': board.move_down() elif move == 'u': board.move_up() pattern_dictionary[key]['final_mapping'] = board.get_board_state() File.save_binary('pattern_dictionary.bin', pattern_dictionary)
def build(): """ Builder pattern 1) Ask user for game mode 2) Ask user for game difficulty 3) If mode set to manual then create a random board 4) if mode set to auto then use ML model to solve puzzle :return: None """ mode = GameMode.run() root_directory = os.path.abspath(__file__ + "r/../../") rel_path = r'data/game/' directory_path = os.path.join(root_directory, rel_path) input1 = GameBoard.obtain_game_from_file(directory_path + 'input1.txt') input2 = GameBoard.obtain_game_from_file(directory_path + 'input2.txt') input3 = GameBoard.obtain_game_from_file(directory_path + 'input3.txt') input4 = GameBoard.obtain_game_from_file(directory_path + 'input4.txt') if mode == PlayModeEnum.MANUAL: for game in input1: board = GameBoard() board.create_game_from_array(game) print('------------------------------------------------') game = ManualGame(board) game.run() else: for game in input1: board = GameBoard(verbose=False) board.create_game_from_array(game) game = AutoGame(board) start = time.time() game.run() end = time.time() GameBuilder.save('output1.txt', board.move_sequence, end - start) for game in input2: board = GameBoard(verbose=False) board.create_game_from_array(game) game = AutoGame(board) start = time.time() game.run() end = time.time() GameBuilder.save('output2.txt', board.move_sequence, end - start) for game in input3: board = GameBoard(verbose=False) board.create_game_from_array(game) game = AutoGame(board) start = time.time() game.run() end = time.time() GameBuilder.save('output3.txt', board.move_sequence, end - start) for game in input4: board = GameBoard(verbose=False) board.create_game_from_array(game) game = AutoGame(board) start = time.time() game.run() end = time.time() GameBuilder.save('output4.txt', board.move_sequence, end - start) print('Solved all games')