Example #1
0
class TestBoard(unittest.TestCase):
    def setUp(self):
        self.basic_game = Game(board=None, rnd=random.Random(1))

    def test_constructor(self):
        self.assertEqual(self.basic_game.score(), 0)
        game = Game(rnd=random.Random(1))
        self.assertEqual(game.board(), self.basic_game.board())

    def test_smash(self):
        # This doesn't comprehensively test smashing because
        # the tests for Board mostly did that already.
        for direction in DIRECTIONS:
            board = Board()
            board = board.update((1, 1), 2)
            game = Game(rnd=random.Random(1), board=board)
            self.assertTrue(game.smash(direction))
            self.assertEqual(game.board()[1, 1], 0)
        for direction in DIRECTIONS:
            board = Board()
            board = board.update((0, 0), 2)
            game = Game(rnd=random.Random(1), board=board)
            if direction in {UP, LEFT}:
                self.assertFalse(game.smash(direction))
            else:
                self.assertTrue(game.smash(direction))
                self.assertEqual(game.board()[0, 0], 0)
Example #2
0
 def test_smash(self):
     # This doesn't comprehensively test smashing because
     # the tests for Board mostly did that already.
     for direction in DIRECTIONS:
         board = Board()
         board = board.update((1, 1), 2)
         game = Game(rnd=random.Random(1), board=board)
         self.assertTrue(game.smash(direction))
         self.assertEqual(game.board()[1, 1], 0)
     for direction in DIRECTIONS:
         board = Board()
         board = board.update((0, 0), 2)
         game = Game(rnd=random.Random(1), board=board)
         if direction in {UP, LEFT}:
             self.assertFalse(game.smash(direction))
         else:
             self.assertTrue(game.smash(direction))
             self.assertEqual(game.board()[0, 0], 0)
Example #3
0
    def add_n_examples(self, strategy, rnd, n,
                       starting_positions_dataset=None):
        """Runs games and adds them to the dataset until at least @p n
        examples have been added.  Returns the number of examples added.

        If @p starting_positions_dataset is set, games will be started from
        a randomly selected position from that dataset rather than from a
        blank board."""
        print("Adding", n, "examples to dataset.")
        added = 0
        while added < n:
            starting_game = None
            if starting_positions_dataset:
                random_position = starting_positions_dataset.nth_example(
                    rnd.randint(0,
                                starting_positions_dataset.num_examples() - 1))
                starting_game = Game(Board.from_vector(random_position))
                if not starting_game.board().can_move():
                    continue
            num_added = self.add_game(strategy, rnd, starting_game)
            if (added // 10000) != ((num_added + added) // 10000):
                print("Added %d so far..." % (num_added + added))
            added += num_added
        return added
Example #4
0
 def test_constructor(self):
     self.assertEqual(self.basic_game.score(), 0)
     game = Game(rnd=random.Random(1))
     self.assertEqual(game.board(), self.basic_game.board())
Example #5
0
    strategy = None
    if args.strategy == "spinny":
        strategy = SpinnyStrategy()
    elif args.strategy == "random":
        strategy = RandomStrategy()
    else:
        from strategy.nn.nn_strategy import ModelStrategy
        strategy = ModelStrategy(args.strategy, verbose_period=5000)

    total = 0
    for i in range(args.number_of_games):
        game = Game()
        running = True
        while running:
            turn_outcome = game.do_turn(
                strategy.get_move(game.board(), game.score()))
            if args.verbose:
                game.pretty_print()
            running = (turn_outcome != GAMEOVER)
        strategy.notify_outcome(game.board(), game.score())
        if not args.summary:
            print(game.score())
        total += game.score()
        if not (i % 25):
            print("...", i, "/", args.number_of_games)
    if args.summary:
        print("Strategy %s had average score %f after %d games" %
              (args.strategy,
               (total / args.number_of_games), args.number_of_games))
Example #6
0
from game.game import Game
from ai.move_eval import count_obstructing
import numpy as np

game = Game()

game.board_size = [3, 3]
game.start_pos = [1, 1]
game.reset()

game.board = np.array([[2, 1, 0], [1, 0, 0], [0, 0, 0]])

assert count_obstructing(game, [1, 1], [1, 1], 3) == 1
assert count_obstructing(game, [1, 1], [1, 1], 2) == 1
assert count_obstructing(game, [1, 1], [1, 1], 1) == 0

game.board = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 0]])

assert count_obstructing(game, [1, 1], [1, 1], 3) == 0