Esempio n. 1
0
def slomobug():
    print('A')
    mines = [(28, 5), (4, 8), (16, 3), (0, 1), (25, 4), (5, 13), (12, 6),
             (22, 2), (15, 6), (14, 5), (7, 9), (22, 4), (29, 4), (14, 14),
             (8, 10), (24, 0), (15, 12), (8, 15), (24, 9), (13, 3), (2, 13),
             (14, 4), (6, 1), (27, 8), (6, 6), (20, 9), (2, 8), (23, 4),
             (22, 11), (24, 1), (5, 2), (0, 3), (9, 3), (29, 7), (11, 0),
             (11, 8), (14, 10), (14, 11), (12, 7), (7, 8), (4, 0), (5, 15),
             (22, 12), (28, 10), (0, 12), (22, 10), (6, 10), (6, 3), (26, 2),
             (20, 15), (2, 12), (28, 6), (13, 11), (23, 9), (16, 5), (26, 6),
             (12, 0), (16, 15), (11, 9), (26, 12), (22, 8), (4, 1), (28, 7),
             (6, 4), (16, 11), (17, 13), (2, 7), (5, 6), (0, 11), (0, 15),
             (21, 7), (27, 10), (28, 9), (0, 2), (6, 5), (27, 9), (17, 1),
             (25, 8), (4, 14), (2, 6), (6, 15), (26, 11), (18, 15), (9, 4),
             (15, 13), (20, 4), (18, 9), (15, 7), (2, 9), (24, 2), (16, 7),
             (28, 0), (5, 14), (21, 14), (29, 5), (23, 0), (23, 10), (9, 2),
             (20, 7)]
    start = (9, 13)
    field = anonymine_fields.generic_field([30, 16], True, True)
    solver = anonymine_solver.solver()
    solver.field = field
    field.fill(mines)
    field.reveal(start)
    field.set_callback('input', output, None)
    print(field)
    print(solver.solve())
Esempio n. 2
0
    def __init__(self, cfgfile, **parameters):
        '''
        `cfgfile` is the path to the "enginecfg" configuration file.
        
        Recognised keyword arguments are:
            width=              # int >= 4
            height=             # int >= 4
            mines=              # int; Only integers are allowed here.
            gametype=           # str; 'moore', 'hex' or 'neumann'
            guessless=          # bool; Must be possible to solve without
                                #       guessing?
        
        As of version 0.0.20, no parameters are mandatory; they all
        have default values.  This may change in the future.
        '''
        # Define some constants.
        self.gametypes = ('moore', 'hex', 'neumann')

        # Handle parameters.
        default = {
            'width': 10,
            'height': 10,
            'mines': 10,
            'gametype': 'moore',
            'guessless': True,
        }
        for key in default:
            if key not in parameters:
                parameters[key] = default[key]
        assert parameters['gametype'] in ('neumann', 'hex', 'moore')

        self.cfg = eval(open(cfgfile).read())
        # Prevent DoS:
        area = parameters['width'] * parameters['height']
        if area > self.cfg['init-field']['sec-maxarea']:
            raise security_alert('Area too large, aborting')

        # Begin initialization.
        self.dimensions = (parameters['width'], parameters['height'])
        self.gametype = parameters['gametype']
        self.n_mines = parameters['mines']
        self.guessless = parameters['guessless']
        if self.gametype == 'hex':
            self.field = fields.hexagonal_field(
                parameters['width'],
                parameters['height'],
                True  # Flagcount
            )
        else:
            self.field = fields.generic_field(
                [parameters['width'], parameters['height']],
                self.gametype == 'moore',
                True  # Flagcount
            )

        self.game_status = 'pre-game'  # play-game game-won game-lost

        self.solver = solver.solver()
        self.solver.field = self.field
Esempio n. 3
0
def main():
    def mk_bug_file():
        f = open(time.strftime('BUG_%Y-%m-%dT%H:%M:%S', time.gmtime()), 'w')
        d = {
            'start': start,
            'mines': mines[:99],
            'post-solve': str(field.field),
            'exception': ''.join(traceback.format_exception(*sys.exc_info()))
        }
        f.write(pprint.pformat(d))
        f.close()
    field = anonymine_fields.generic_field([30, 16])
    solver = anonymine_solver.solver()
    solver.field = field
    mines = field.all_cells()
    fields = wins = games = bugs = 0
    while True:
        fields += 1
        random.shuffle(mines)
        field.clear()
        field.fill(mines[:99])
        for mine in mines[99:]:
            for neighbour in field.get_neighbours(mine):
                if neighbour in mines[:99]:
                    break
            else:
                field.reveal(mine)
                start = mine
                break
        else:
            assert False, "No startpoint"
        # Hopefully, the bug is above.
        try:
            win, ignore = solver.solve()
            games += 1
            if win:
                wins += 1
        except KeyboardInterrupt:
            try:
                sys.stdout.write('Press enter to continue, ^C again to quit.\n')
                sys.stdin.readline()
            except KeyboardInterrupt:
                sys.exit(0)
        except AssertionError as e:
            mk_bug_file()
            bugs += 1
            sys.stdout.write('\n{}/{}\n'.format(bugfiles, fields))
        except:
            mk_bug_file()
            raise
        sys.stdout.write(
            '{}/{} games won ({}‰);\t'
            '{}/{} fields trigger the bug ({} ppm)\n'.format(
                wins, games, int(1000.0 * float(wins)/games + 0.5),
                bugs, fields, int(1000000.0 * float(bugs)/fields + 0.5),
            )
        )
Esempio n. 4
0
def pypybug():
    size = [5, 5]
    start = (1, 1)
    mines = [(3, 4), (4, 4), (4, 0)]
    field = anonymine_fields.generic_field(size, True, True)
    solver = anonymine_solver.solver()
    solver.field = field
    field.fill(mines)
    field.reveal(start)
    field.set_callback('input', output, None)
    print(field)
    print(solver.solve())
Esempio n. 5
0
def pypybug():
    size = [5, 5]
    start = (1, 1)
    mines = [(3, 4), (4, 4), (4, 0)]
    field = anonymine_fields.generic_field(size, True, True)
    solver = anonymine_solver.solver()
    solver.field = field
    field.fill(mines)
    field.reveal(start)
    field.set_callback('input', output, None)
    print(field)
    print(solver.solve())
Esempio n. 6
0
def profile_solver(x, y, m):
    field = anonymine_fields.generic_field([x, y])
    mines = field.all_cells()
    random.shuffle(mines)
    field.fill(mines[:m])
    for mine in mines[m:]:
        for neighbour in field.get_neighbours(mine):
            if neighbour in mines[:m]:
                break
        else:
            field.reveal(mine)
            break
    solver = anonymine_solver.solver()
    solver.field = field
    solver.solve()
Esempio n. 7
0
def run623():
    field = anonymine_fields.generic_field([6, 6, 6])

    mines = field.all_cells()
    random.shuffle(mines)
    field.fill(mines[:22])

    field.reveal(mines[22])

    solver = anonymine_solver.solver()
    solver.field = field
    ret = solver.solve()
    if ret[0]:
        print(field.field)
    print(ret)
Esempio n. 8
0
def run623():
    field = anonymine_fields.generic_field([6, 6, 6])
    
    mines = field.all_cells()
    random.shuffle(mines)
    field.fill(mines[:22])
    
    field.reveal(mines[22])
    
    solver = anonymine_solver.solver()
    solver.field = field
    ret = solver.solve()
    if ret[0]:
        print(field.field)
    print(ret)
Esempio n. 9
0
def runneumann(x=78, y=18, m=225):
    field = anonymine_fields.generic_field([x, y], False)
    field.set_callback('input', output, None)

    mines = field.all_cells()
    random.shuffle(mines)
    field.fill(mines[:m])

    for mine in mines[m:]:
        for neighbour in field.get_neighbours(mine):
            if neighbour in mines[:m]:
                break
        else:
            field.reveal(mine)
            break

    solver = anonymine_solver.solver()
    solver.field = field
    print(solver.solve())
Esempio n. 10
0
def runneumann(x=78, y=18, m=225):
    field = anonymine_fields.generic_field([x, y], False)
    field.set_callback('input', output, None)
    
    mines = field.all_cells()
    random.shuffle(mines)
    field.fill(mines[:m])
    
    for mine in mines[m:]:
        for neighbour in field.get_neighbours(mine):
            if neighbour in mines[:m]:
                break
        else:
            field.reveal(mine)
            break
    
    solver = anonymine_solver.solver()
    solver.field = field
    print(solver.solve())
Esempio n. 11
0
def slomobug():
    print('A')
    mines = [(28,5),(4,8),(16,3),(0,1),(25,4),(5,13),(12,6),(22,2),(15,6),
(14,5),(7,9),(22,4),(29,4),(14,14),(8,10),(24,0),(15,12),(8,15),(24,9),(13,3),
(2,13),(14,4),(6,1),(27,8),(6,6),(20,9),(2,8),(23,4),(22,11),(24,1),(5,2),
(0,3),(9,3),(29,7),(11,0),(11,8),(14,10),(14,11),(12,7),(7,8),(4,0),(5,15),
(22,12),(28,10),(0,12),(22,10),(6,10),(6,3),(26,2),(20,15),(2,12),(28,6),
(13,11),(23,9),(16,5),(26,6),(12,0),(16,15),(11,9),(26,12),(22,8),(4,1),(28,7),
(6,4),(16,11),(17,13),(2,7),(5,6),(0,11),(0,15),(21,7),(27,10),(28,9),(0,2),
(6,5),(27,9),(17,1),(25,8),(4,14),(2,6),(6,15),(26,11),(18,15),(9,4),(15,13),
(20,4),(18,9),(15,7),(2,9),(24,2),(16,7),(28,0),(5,14),(21,14),(29,5),(23,0),
(23,10),(9,2),(20,7)]
    start = (9, 13)
    field = anonymine_fields.generic_field([30, 16], True, True)
    solver = anonymine_solver.solver()
    solver.field = field
    field.fill(mines)
    field.reveal(start)
    field.set_callback('input', output, None)
    print(field)
    print(solver.solve())
Esempio n. 12
0
def chance(width, height, n_mines, runs):
    spinner = '\\|/-'
    solver = anonymine_solver.solver()
    field = anonymine_fields.generic_field([width, height])
    solver.field = field
    
    starttime = time.time()
    
    i = 0
    success = 0.0
    while i < runs:
        i += 1
        mines = field.all_cells()
        random.shuffle(mines)
        field.clear()
        field.fill(mines[:n_mines])
        for mine in mines[n_mines:]:
            for neighbour in field.get_neighbours(mine):
                if neighbour in mines[:n_mines]:
                    break
            else:
                field.reveal(mine)
                break
        progress = int(40.0 * i/runs)
        success += solver.solve()[0]
        
        ETA = (runs - i) * (time.time() - starttime)/i
        
        sys.stderr.write('\x1b[2K\r{}@{}x{}:\t{}%\t[{}{}]{}\tETA: {}+{}'.format(
            n_mines, width, height,
            int(100.0 * success/i + 0.5),
            '=' * progress, ' ' * (40 - progress),
            spinner[i % len(spinner)],
            int(ETA/86400), time.strftime('%H:%M:%S', time.gmtime(ETA))
        ))
    sys.stderr.write('\n')
    return success/runs
Esempio n. 13
0
def chance(width, height, n_mines, runs):
    spinner = '\\|/-'
    solver = anonymine_solver.solver()
    field = anonymine_fields.generic_field([width, height])
    solver.field = field

    starttime = time.time()

    i = 0
    success = 0.0
    while i < runs:
        i += 1
        mines = field.all_cells()
        random.shuffle(mines)
        field.clear()
        field.fill(mines[:n_mines])
        for mine in mines[n_mines:]:
            for neighbour in field.get_neighbours(mine):
                if neighbour in mines[:n_mines]:
                    break
            else:
                field.reveal(mine)
                break
        progress = int(40.0 * i / runs)
        success += solver.solve()[0]

        ETA = (runs - i) * (time.time() - starttime) / i

        sys.stderr.write(
            '\x1b[2K\r{}@{}x{}:\t{}%\t[{}{}]{}\tETA: {}+{}'.format(
                n_mines, width, height, int(100.0 * success / i + 0.5),
                '=' * progress, ' ' * (40 - progress),
                spinner[i % len(spinner)], int(ETA / 86400),
                time.strftime('%H:%M:%S', time.gmtime(ETA))))
    sys.stderr.write('\n')
    return success / runs
Esempio n. 14
0
 def __init__(self, cfgfile, **parameters):
     '''
     `cfgfile` is the path to the "enginecfg" configuration file.
     
     Recognised keyword arguments are:
         width=              # int >= 4
         height=             # int >= 4
         mines=              # int; Only integers are allowed here.
         gametype=           # str; 'moore', 'hex' or 'neumann'
         flagcount=          # bool; Count how many flags are left?
         guessless=          # bool; Must be possible to solve without
                             #       guessing?
     
     As of version 0.0.20, no parameters are mandatory; they all
     have default values.  This may change in the future.
     '''
     # Define some constants.
     self.gametypes = ('moore', 'hex', 'neumann')
     
     # Handle parameters.
     default = {
         'width':     10,
         'height':    10,
         'mines':     10,
         'gametype':  'moore',
         'flagcount': True,
         'guessless': True,
     }
     for key in default:
         if key not in parameters:
             parameters[key] = default[key]
     assert parameters['gametype'] in ('neumann', 'hex', 'moore')
     
     self.cfg = eval(open(cfgfile).read())
     # Prevent DoS:
     area = parameters['width'] * parameters['height']
     if area > self.cfg['init-field']['sec-maxarea']:
         raise security_alert('Area too large, aborting')
     
     # Begin initialization.
     self.dimensions = (parameters['width'], parameters['height'])
     self.gametype = parameters['gametype']
     self.n_mines = parameters['mines']
     self.flagcount = parameters['flagcount']
     self.guessless = parameters['guessless']
     if self.gametype == 'hex':
         self.field = fields.hexagonal_field(
             parameters['width'],
             parameters['height'],
             parameters['flagcount']
         )
     else:
         self.field = fields.generic_field(
             [parameters['width'], parameters['height']],
             self.gametype == 'moore',
             parameters['flagcount']
         )
     
     self.game_status = 'pre-game' # play-game game-won game-lost
     
     self.solver = solver.solver()
     self.solver.field = self.field