Ejemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(description="A sudoku resolver")
    parser.add_argument("-v", "--verbose", action="count", default=0, help="increase output verbosity (-vv or --verbose=2 for even more verbosity)")
    parser.add_argument("-g", "--grid", help="a grid directly on the command line (9x9 digits and dots, with or without \\n)")
    parser.add_argument("gridAsFile", type=argparse.FileType('r'), nargs='?', help="a file containing the grid to solve")
    args = parser.parse_args()

    if not args.gridAsFile and not args.grid:
        parser.error("A grid must be provided.")

    logLevel = [logging.WARNING, logging.INFO, logging.DEBUG][args.verbose]
    logging.basicConfig(format='%(levelname)s: %(message)s', level=logLevel)

    grid = prepareGrid(args.gridAsFile, args.grid)
    print "Loaded grid :"
    print grid.display()

    gridResolution = GridResolution(grid)
    if gridResolution.solve():
        logging.info("Grid solved completely !")
    else:
        logging.info("Solving stopped without being able to finish the grid.")

    print "Final grid :"
    print grid.display()

    logging.info("Distribution of resolution strategies used :")
    for key, value in Stats.results().iteritems():
        logging.info("  {}\t : {} cell(s)".format(key, value))
Ejemplo n.º 2
0
 def test_solve_strategies_unsuccessful(self):
     gridResolution = GridResolution(grid=Grid(), strategies=[MockStrategy(False)])
     self.assertFalse(gridResolution.solve())
Ejemplo n.º 3
0
 def test_solve_in_many_passes(self):
     grid = MockGrid(3)
     gridResolution = GridResolution(grid=grid, strategies=[MockStrategy(True)])
     self.assertTrue(gridResolution.solve())
Ejemplo n.º 4
0
 def test_solve_strategies_successful_and_grid_solved(self):
     gridResolution = GridResolution(grid=Grid(StringIO("415638972\n362479185\n789215364\n926341758\n138756429\n574982631\n257164893\n843597216\n691823547\n")), strategies=[MockStrategy(False), MockStrategy(True)])
     self.assertTrue(gridResolution.solve())