def test_result_true(self): puzzle_list = [[['C', 'A', 'T'], ['X', 'Z', 'T'], ['Y', 'O', 'T']], [ ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X', 'D'], ], [['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X', 'D'], ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X', 'D'], ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'X', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'Y', 'X', 'D']]] puzzle_list.append(zip(*puzzle_list[1][::-1])) puzzle_list.append(list(reversed(zip(*puzzle_list[1])))) for puzzle in puzzle_list: for index, puzzle in enumerate(puzzle_list): ps = PuzzleSolver(puzzle) if index == 0: self.assertEqual(ps.find_words(), 8) elif index in [1, 3, 4]: self.assertEqual(ps.find_words(), 22) else: self.assertEqual(ps.find_words(), 69)
def test_get_string_list(self): puzzle = [ ['C', 'A', 'T'], ['X', 'Z', 'T'], ['Y', 'O', 'T']] puzzle_2 = [ ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X']] ps = PuzzleSolver(puzzle) ps2 = PuzzleSolver(puzzle_2) result = ['CAT', 'XZT', 'YOT', 'CXY', 'AZO', 'TTT', 'CZT', 'AT', 'XO', 'TO', 'TZY', 'AX'] result2 = [ 'CATAPULT', 'XZTTOYOO', 'YOTOXTXX', 'CXY', 'AZO', 'TTT', 'ATO', 'POX', 'UYT', 'LOX', 'TOX', 'PYX', 'TTX', 'XO', 'ATO', 'UOX', 'CZT', 'AOT', 'LO', 'TZY', 'ATO', 'LYX', 'TOT', 'OX', 'AX', 'PTT', 'UOO'] self.assertEqual(set(ps.get_string_list()), set(result)) self.assertEqual(set(ps2.get_string_list()), set(result2))
def test_result_type(self): puzzle = [ ['C', 'A', 'T'], ['X', 'Z', 'T'], ['Y', 'O', 'T']] ps = PuzzleSolver(puzzle) result = ps.find_words() self.assertIsInstance(result, int)
def test_input_type(self): puzzle = 'String' with self.assertRaises(WrongInputException) as contextManager: PuzzleSolver(puzzle) exception = contextManager.exception self.assertEqual(exception.code, 0)
def test_input_char_length(self): puzzle = [['X', 'AS', 'T'], ['C', 'A', 'O'], ['Y', 'O', 'T']] with self.assertRaises(WrongInputException) as contextManager: PuzzleSolver(puzzle) exception = contextManager.exception self.assertEqual(exception.code, 5)
def test_input_row_type(self): puzzle = [['C', 'A', 'O', 'C', 'A', 'T'], 'OK', ['Y', 'O', 'T', 'H', 'A', 'Y']] with self.assertRaises(WrongInputException) as contextManager: PuzzleSolver(puzzle) exception = contextManager.exception self.assertEqual(exception.code, 1)
def test_result_true(self): puzzle_list = [ [ ['C', 'A', 'T'], ['X', 'Z', 'T'], ['Y', 'O', 'T'] ], [ ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X', 'D'], ], [ ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X', 'D'], ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X', 'D'], ['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T', 'M'], ['X', 'X', 'T', 'T', 'O', 'Y', 'O', 'O', 'S'], ['Y', 'O', 'T', 'O', 'X', 'T', 'Y', 'X', 'D'] ] ] puzzle_list.append(zip(*puzzle_list[1][::-1])) puzzle_list.append(list(reversed(zip(*puzzle_list[1])))) for puzzle in puzzle_list: for index, puzzle in enumerate(puzzle_list): ps = PuzzleSolver(puzzle) if index == 0: self.assertEqual(ps.find_words(), 8) elif index in [1, 3, 4]: self.assertEqual(ps.find_words(), 22) else: self.assertEqual(ps.find_words(), 69)
def test_get_string_list(self): puzzle = [['C', 'A', 'T'], ['X', 'Z', 'T'], ['Y', 'O', 'T']] puzzle_2 = [['C', 'A', 'T', 'A', 'P', 'U', 'L', 'T'], ['X', 'Z', 'T', 'T', 'O', 'Y', 'O', 'O'], ['Y', 'O', 'T', 'O', 'X', 'T', 'X', 'X']] ps = PuzzleSolver(puzzle) ps2 = PuzzleSolver(puzzle_2) result = [ 'CAT', 'XZT', 'YOT', 'CXY', 'AZO', 'TTT', 'CZT', 'AT', 'XO', 'TO', 'TZY', 'AX' ] result2 = [ 'CATAPULT', 'XZTTOYOO', 'YOTOXTXX', 'CXY', 'AZO', 'TTT', 'ATO', 'POX', 'UYT', 'LOX', 'TOX', 'PYX', 'TTX', 'XO', 'ATO', 'UOX', 'CZT', 'AOT', 'LO', 'TZY', 'ATO', 'LYX', 'TOT', 'OX', 'AX', 'PTT', 'UOO' ] self.assertEqual(set(ps.get_string_list()), set(result)) self.assertEqual(set(ps2.get_string_list()), set(result2))
def test_input_ok(self): puzzle = [['X', 'Z', 'T', 'A', 'T', 'Z'], ['C', 'A', 'O', 'C', 'A', 'T'], ['Y', 'O', 'T', 'H', 'A', 'Y']] self.assertIsInstance(PuzzleSolver(puzzle), PuzzleSolver)
def test_result_type(self): puzzle = [['C', 'A', 'T'], ['X', 'Z', 'T'], ['Y', 'O', 'T']] ps = PuzzleSolver(puzzle) result = ps.find_words() self.assertIsInstance(result, int)
def test_find_word_in_string(self): string = 'CATAPULT' result = PuzzleSolver.find_word_in_string('AT', string) self.assertEqual(result, 2)
from Puzzle import Puzzle from PuzzleSolver import PuzzleSolver import utils file = "data/board2.txt" p = Puzzle(utils.board_from_file(file)) logs = open("data/test.txt", "a") solver = PuzzleSolver(p) print(p) y = input("Solve puzzle? (y/n): ") if y == 'y' and p.is_solvable(): solution = solver.a_star() for s in reversed(solution): logs.write(str(s)) logs.write("\n\n") logs.write("Puzzle solved with " + str(len(solution)) + " steps.") logs.close() print("Puzzle solved.") elif y != 'y': print("Solver quit.") else: print("Puzzle is not solvable.")
#!/usr/bin/env python import sys from PuzzleSolver import PuzzleSolver if len(sys.argv) < 2: print "Must supply file to read or an input board" sys.exit(1) first = True for arg in sys.argv[1:]: if first: s = PuzzleSolver(arg,debug=True) first = False else: s.changeBoard(arg) s.loadBoard() s.solveBoard() #s.writeBoard() #s.printBoard(debug=True) s.printBoard(debug=True) #s.solver.entryElim(4,4) #s.printBoard(debug=True)