示例#1
0
def algo_case3():
    """ Four flags and four exposed. case3
           0 1 2 3 4  (x)
        0 [x . 2 . 1]
        1 [1 . . x .]
        2 [. . . x .]
        3 [. . x . 1]
       (y)
    """
    algo = AlgorithmBeta(ms.GameConfig(5, 4, 4))
    algo.flags = {(0, 0), (3, 1), (3, 2), (2, 3)}
    algo.exposed_squares = {(0, 1): 1, (2, 0): 2, (4, 0): 1, (4, 3): 1}
    return algo
示例#2
0
def algo_case2():
    """ Four flags and two exposed.
           0 1 2 3 4  (x)
        0 [x . . . .]
        1 [1 2 . x .]
        2 [. . . x .]
        3 [. . x . .]
       (y)
    """
    algo = AlgorithmBeta(ms.GameConfig(5, 4, 4))
    algo.flags = {(0, 0), (3, 1), (3, 2), (2, 3)}
    algo.exposed_squares = {(0, 1): 1, (1, 1): 2}
    return algo
示例#3
0
        self.exposed_squares = set()

    def init(self, config):
        self.width = config.width
        self.height = config.height
        self.exposed_squares.clear()

    def next(self):
        while True:
            x = random.randint(0, self.width - 1)
            y = random.randint(0, self.height - 1)
            if (x, y) not in self.exposed_squares:
                break
        print('selecting point ({0},{1})'.format(x, y))
        return x, y

    def update(self, result):
        for position in result.new_squares:
            self.exposed_squares.add((position.x, position.y))


num_games = 1
config = ms.GameConfig()
ai = RandomAI()
viz = ms.GameVisualizer('key')
results = ms.run_games(config, 1, ai, viz)
if results[0].success:
    print('Success!')
else:
    print('Boom!')
print('Game lasted {0} moves'.format(results[0].num_moves))
示例#4
0
import logging
import minesweeper as ms

logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

print("This will play a single game and then quit.")
print("The minesweeper window needs focus to capture a key press.")
print()

num_games = 1
config = ms.GameConfig(16, 16, 32)
ai = ms.CSPAI()
viz = ms.PyGameVisualizer(pause=0.1, next_game_prompt=True)
result = ms.run_games(config, num_games, ai, viz).pop()
print('Game lasted {0} moves'.format(result.num_moves))
示例#5
0
    def next(self):
        # pdb.set_trace()
        while True:
            x = random.randint(0, self.width - 1)
            y = random.randint(0, self.height - 1)
            if (x, y) not in self.exposed_squares:
                break
        print('selecting point ({0},{1})'.format(x, y))
        return x, y

    def update(self, result):
        for position in result.new_squares:
            self.exposed_squares.add((position.x, position.y))


num_games = 100
config = ms.GameConfig(width=10, height=10, num_mines=12)
ai = RandomAI()
viz = ms.GameVisualizer(10)
results = ms.run_games(config, 1, ai, viz)
if results[0].success:
    print('Success!')
else:
    print('Boom!')
print('Game lasted {0} moves'.format(results[0].num_moves))
"""
Beginner       9   9   10
Intermediate   16  16  40
Expert         16  30  99
"""
    def next(self):
        while True:
            if self.x is 0:
                self.x = self.width - 1
                self.y = self.y - 1
            else:
                self.x = self.x - 1
            x = self.x
            y = self.y
            if (x, y) not in self.exposed_squares:
                break
        print('selecting point ({0},{1})'.format(x, y))
        return x, y

    def update(self, result):
        for position in result.new_squares:
            self.exposed_squares.add((position.x, position.y))


num_games = 1
config = ms.GameConfig(
)  # Sets 3 variables: height, width, and # of mines. This is then used throughout rest of program.
ai = SeqAI()
viz = ms.GameVisualizer('key')
results = ms.run_games(config, 1, ai, viz)
if results[0].success:
    print('Success!')
else:
    print('Boom!')
print('Game lasted {0} moves'.format(results[0].num_moves))
示例#7
0
def game1():
    mines = flip([[True, False, False, False, False],
                  [False, False, False, True, False],
                  [False, False, False, True, False],
                  [False, False, True, False, False]])
    return ms.Game(ms.GameConfig(5, 4, 4), mines)
示例#8
0
import logging
import minesweeper as ms
import ais as myai
import concurrent.futures

levels = [[8, 8, 10], [16, 16, 40], [30, 16, 99]]

logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
MAX_THREADS = 12
num_games = 1
config = ms.GameConfig(*levels[0])
ai = myai.BasicAI(myai.AlgorithmBeta(config))
viz = ms.PyGameVisualizer(pause='key', next_game_prompt=True)


def main():
    print("This will play a single game and then quit.")
    print("The minesweeper window needs focus to capture a key press.")
    print()

    # results = ms.run_games(config, num_games, ai)
    results = ms.run_games(config, num_games, ai, threads=MAX_THREADS)
    print('\n\nResults:')
    print('-------------------------')

    num_wins = 0
    for i, result in results:
        num_wins += result.victory == True
        print('Game {:2d}: {:3d} steps, {:s}'.format(
            i, result.num_moves, 'win' if result.victory else 'failed'))
            y = random.randint(0, self.height - 1)
            if (x, y) not in self.exposed_squares:
                break
        print(
            'selecting point ({0},{1}) | Move {2}'.format(
                x, y, self.num_moves)
        )  # The {} and .format() are how you can enter variables into the string
        return x, y

    def update(self, result):
        self.num_moves += 1
        for position in result.new_squares:
            self.exposed_squares.add(
                (position.x, position.y)
            )  # Here is where the program adds a unique x,y pair into the set. So it is keeping track of this info itself...


num_games = 1
config = ms.GameConfig(
    5, 5, 2
)  # sets 3 variables: height, width, and number of mines. These are used throughout the program to define the game.
ai = RandomAI()
viz = ms.GameVisualizer(
    1)  # 'key' if you want enter. Else, number is seconds (must be integer)
results = ms.run_games(config, 1, ai, viz)
if results[0].success:
    print('Success!')
else:
    print('Boom!')
print('Game lasted {0} moves'.format(results[0].num_moves))
示例#10
0
def test_game_init_for_total_mine_count():
    game = ms.Game(ms.GameConfig(100, 100, 800))
    assert 800 == sum(row.count(True) for row in game.mines)
示例#11
0
def game4():
    mines = flip([[False, False, True], [False, False, False],
                  [True, False, True]])
    return ms.Game(ms.GameConfig(3, 3, 3), mines)
示例#12
0
def test_run_games():
    config = ms.GameConfig()
    ai = ms.RandomAI()
    results = ms.run_games(config, 2, ai)
    assert 2 == len(results)