def main(): easy = True medium = False hard = False if easy: numBombs = 10 numBatteries = 10 puzzleWidth = 9 puzzleHeight = 9 if medium: numBombs = 20 numBatteries = 10 puzzleWidth = 12 puzzleHeight = 12 if hard: numBombs = 40 numBatteries = 10 puzzleWidth = 14 puzzleHeight = 14 for i in range(10): print "Test ", i worldMap = minesweeper.makeMap(puzzleHeight, puzzleWidth, numBatteries, numBombs) while(minesweeper.checkMap(worldMap)): worldMap = minesweeper.makeMap(puzzleHeight, puzzleWidth, numBatteries, numBombs) minesweeper.solve(worldMap)
def main(): f = open(filename) o = open(outfilename, 'w+') T = int(f.readline()) t = 1 while t <= T: output = solve(f) out("Case #{}: {}".format(t, output), o) t += 1
def test_end_to_end(self): grid_size_str = "3,3" mines_str = """*.. .*. ...""" grid_size = minesweeper.read_grid_size(grid_size_str) grid = minesweeper.read_mines_from_string(mines_str, grid_size) assert minesweeper.solve(grid) == [["*", "2", "1"], ["2", "*", "1"], ["1", "1", "1"]]
def autoplay(game, **kwargs): moves = 0 hopeless = False while True: #print game #print '----' result = game.outcome() if result is not None: return result, moves, hopeless state = u.generate_rules(BoardWrapper(game), game.num_mines) if game.mode == 'mineprob': state[1] = game.mine_prob solution = mnsw.solve(*state) def _cells(cells): for c in cells: if c is not None: yield c else: for e in game.cell_ids: if game.is_frontier_cell(e): yield e def get_cells(p): EPSILON = 1e-6 return _cells(k for k, v in solution.iteritems() if abs(v - p) < EPSILON) mines = get_cells(1.) safe = list(get_cells(0.)) for c in mines: game.mark(c) #print 'marking', c if safe: for c in safe: game.sweep(c) #print 'clearing', c else: # find safest min_risk = min(solution.values()) if min_risk > .5 - 1e-6: hopeless = True safest = list(get_cells(min_risk)) STRATEGY = kwargs.get('strategy') if STRATEGY: safest = locpref_strategy(STRATEGY, game, safest) move = random.choice(safest) game.sweep(move) #print 'safest', move moves += 1
def api_solve(payload): rules, mine_p = parse_api_payload(payload) result = {} start = time.time() try: result['solution'] = mnsw.solve(rules, mine_p, '_other') except mnsw.InconsistencyError: result['solution'] = None end = time.time() result['processing_time'] = end - start return result
def test_solve(self): grid = [["*", ".", "."], [".", "*", "."], [".", ".", "."]] assert minesweeper.solve(grid) == [["*", "2", "1"], ["2", "*", "1"], ["1", "1", "1"]]