Ejemplo n.º 1
0
    def test_rightdown_win(self):
        judge = Judge()
        g = Grid(ncols=5, nrows=4)

        g.move(Color.RED, 2)
        g.move(Color.BLACK, 3)
        g.move(Color.RED, 1)
        g.move(Color.BLACK, 2)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 0)

        # 3  B - - - -
        # 2  R B - - -
        # 1  R B B - -
        # 0  R R R B -
        #    0 1 2 3 4

        self.assertFalse(judge.is_over(g.state))

        g.move(Color.BLACK, 0)
        self.assertTrue(judge.is_over(g.state))
        self.assertTrue(judge.is_over_rightdown(g.state))
        self.assertFalse(judge.is_over_vertical(g.state))
Ejemplo n.º 2
0
    def test_move(self):
        g = Grid()
        g.move(Color.RED, 0)
        state = g.state.cols_first

        self.assertEqual(state[0][0], Color.RED)
        self.assertIsNone(state[0][1])
Ejemplo n.º 3
0
    def test_from_coords(self):
        judge = Judge()
        g = Grid(ncols=5, nrows=4)

        g.move(Color.RED, 2)
        g.move(Color.BLACK, 3)
        g.move(Color.RED, 1)
        g.move(Color.BLACK, 2)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 0)

        # 3  B - - - -
        # 2  R B - - -
        # 1  R B B - -
        # 0  R R R B -
        #    0 1 2 3 4

        self.assertFalse(judge.is_over_from(g.state, 1, 2))
        self.assertFalse(judge.is_over_from(g.state, 2, 1))
        self.assertTrue(judge.is_over_from(g.state, 0, 3))
        self.assertTrue(judge.is_over_from(g.state, 3, 0))
Ejemplo n.º 4
0
    def test_rightup_win(self):
        judge = Judge()
        g = Grid(ncols=5, nrows=4)

        g.move(Color.RED, 1)
        g.move(Color.BLACK, 0)
        g.move(Color.RED, 2)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 3)
        g.move(Color.BLACK, 2)
        g.move(Color.RED, 3)
        g.move(Color.BLACK, 2)
        g.move(Color.RED, 3)

        # 3  - - - B -
        # 2  - - B R -
        # 1  - B B R -
        # 0  B R R R -
        #    0 1 2 3 4

        self.assertFalse(judge.is_over(g.state))

        g.move(Color.BLACK, 3)
        self.assertTrue(judge.is_over(g.state))
        self.assertTrue(judge.is_over_rightup(g.state))
        self.assertFalse(judge.is_over_rightdown(g.state))
Ejemplo n.º 5
0
def create_game(first_player: Player,
                second_player: Player,
                first_color: Color = Color.RED) -> Game:
    return Game(grid=Grid(),
                judge=judge,
                first_player=first_player,
                second_player=second_player,
                first_color=first_color)
Ejemplo n.º 6
0
    def test_middle_finishing(self):
        judge = Judge()
        g = Grid(ncols=4, nrows=2)
        g.move(Color.RED, 0)
        g.move(Color.RED, 1)
        g.move(Color.RED, 3)
        g.move(Color.RED, 2)

        self.assertTrue(judge.is_over(g.state))
        self.assertTrue(judge.is_over_after_move_in_col(g.state, 2))
Ejemplo n.º 7
0
    def test_horizontal_win(self):
        judge = Judge()
        g = Grid(ncols=4, nrows=4)

        g.move(Color.RED, 0)
        g.move(Color.BLACK, 0)
        g.move(Color.RED, 1)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 2)
        g.move(Color.BLACK, 2)

        self.assertFalse(judge.is_over(g.state))

        g.move(Color.RED, 3)
        self.assertTrue(judge.is_over(g.state))
        self.assertTrue(judge.is_over_horizontal(g.state))
        self.assertFalse(judge.is_over_rightdown(g.state))
Ejemplo n.º 8
0
    def test_vertical_win(self):
        judge = Judge()
        g = Grid(ncols=2, nrows=4)

        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)

        self.assertFalse(judge.is_over(g.state))

        g.move(Color.RED, 0)
        self.assertTrue(judge.is_over(g.state))
        self.assertTrue(judge.is_over_vertical(g.state))
        self.assertFalse(judge.is_over_horizontal(g.state))
Ejemplo n.º 9
0
    def test_state(self):
        g = Grid(ncols=5, nrows=4)
        g.move(Color.RED, 0)
        g.move(Color.BLACK, 1)
        g.move(Color.RED, 1)
        g.move(Color.BLACK, 2)
        g.move(Color.RED, 4)
        g.move(Color.BLACK, 1)
        state = g.state.rows_first

        # 3  - - - - -
        # 2  - B - - -
        # 1  - R - - -
        # 0  R B B - R
        #
        #    0 1 2 3 4

        self.assertSequenceEqual([None, None, None, None, None], state[3])
        self.assertSequenceEqual([None, Color.BLACK, None, None, None],
                                 state[2])
        self.assertSequenceEqual([None, Color.RED, None, None, None], state[1])
        self.assertSequenceEqual(
            [Color.RED, Color.BLACK, Color.BLACK, None, Color.RED], state[0])
Ejemplo n.º 10
0
 def test_overflow(self):
     g = Grid()
     for _ in range(6):
         g.move(Color.RED, 2)
     with self.assertRaises(AssertionError):
         g.move(Color.RED, 2)
Ejemplo n.º 11
0
 def test_move_outside(self):
     g = Grid()
     with self.assertRaises(AssertionError):
         g.move(Color.RED, 7)
Ejemplo n.º 12
0
    def test_initial_state_rows_first(self):
        state = Grid(ncols=7, nrows=6).state.rows_first

        self.assertEqual(len(state), 6)
        self.assertEqual(len(state[0]), 7)
Ejemplo n.º 13
0
    def test_size(self):
        g = Grid(ncols=12, nrows=2)

        self.assertEqual(g.ncols, 12)
        self.assertEqual(g.nrows, 2)
Ejemplo n.º 14
0
            move = random.choice(grid.available_moves)
            return self._traverse_from(
                grid.grid_after_move(current_color, move),
                Color(1 - current_color))

    def _rollout_from(self,
                      grid: Grid,
                      color: Color,
                      last_col: Union[int, None] = None) -> bool:
        if len(grid.available_moves) == 0 or (last_col is None and self._judge.is_over(grid.state)) or \
                (last_col is not None and self._judge.is_over_after_move_in_col(grid.state, last_col)):
            return color != self._color

        move = random.choice(grid.available_moves)
        has_won = self._rollout_from(grid.grid_after_move(color, move),
                                     Color(1 - color), move)
        return has_won

    def _finishing_move_in(self, grid: Grid) -> Union[int, None]:
        for move in grid.available_moves:
            after_move = grid.grid_after_move(self._color, move)
            if self._judge.is_over_after_move_in_col(after_move.state, move):
                return move


if __name__ == '__main__':
    game = Game(Grid(), Judge(), MCTSPlayer(Color.RED, Judge(), 2, 1000),
                MCTSPlayer(Color.BLACK, Judge(), 5, 1000))

    print(game.play())
Ejemplo n.º 15
0
                (last_move is not None and self._judge.is_over_after_move_in_col(grid.state, last_move)):
            return self._evaluate(grid.state, self._judge), 0

        value = -INF if color == MAX_COLOR else INF
        best_move = None
        for move in grid.available_moves:
            child_value, _ = self._minmax(grid.grid_after_move(color, move),
                                          depth - 1, alpha, beta,
                                          Color(1 - color), move)

            if color == MAX_COLOR and child_value > value:
                best_move = move
                value = child_value
                alpha = max(alpha, value)
            elif color == MIN_COLOR and child_value < value:
                best_move = move
                value = child_value
                beta = min(beta, value)

            if alpha >= beta: break

        self._transposition_table[grid.state] = (depth, value, best_move)
        return value, best_move


if __name__ == '__main__':
    game = Game(Grid(), Judge(),
                MinmaxPlayer(Color.RED, Judge(), Evaluator(), 4, 30),
                MinmaxPlayer(Color.BLACK, Judge(), Evaluator(), 6, 18))
    print(game.play())
Ejemplo n.º 16
0
 def test_empty_grid(self):
     Grid(ncols=7, nrows=6)
Ejemplo n.º 17
0
    def test_from_last_in_empty(self):
        judge = Judge()
        g = Grid(ncols=2, nrows=2)

        with self.assertRaises(AssertionError):
            judge.is_over_after_move_in_col(g.state, 1)