def successors(state): next_states = [] # przy pierwszym stanie wybor 3 nastepnych zamiast 9 (dla optymalizacji) if state.grid.is_empty(): for i in range(3): new_grid = Grid() new_grid[i**2].mark = 'O' next_states.append(State(new_grid, state, is_max=False)) return next_states # wybor nastepnych stanow for square in state.grid: if square.is_empty(): new_grid = state.grid.copy() if state.is_max: new_grid.get_square(square.x, square.y).mark = 'O' next_states.append(State(new_grid, state, is_max=False)) else: new_grid.get_square(square.x, square.y).mark = 'X' next_states.append(State(new_grid, state, is_max=True)) return next_states
def test_grid_is_occupied(self): """Tests Grid.is_occupied method.""" grid = Grid(3) grid.data[0][2] = 'X' # Test occupied position assert grid.is_occupied(0, 2) # Test free position assert not grid.is_occupied(0, 1)
def test_grid_display(self, capsys): """Test output of Grid.show method.""" grid = Grid(3) grid.data = [['X', 'Y', 'V'], ['V', 'X', 'Y'], ['X', 'Y', 'X']] grid.show() captured = capsys.readouterr() # Test for expected output of the core part of the grid assert '0\t 1\t 2\t' in captured.out assert '\r\n\t ------------------------\n' in captured.out assert '------------------------\n\r\n' in captured.out
def test_grid_update(self, capsys): """Test Grid.update method.""" grid = Grid(3) grid.update(1, 1, 'x') assert 'x' in grid[1][1] grid.update(2, 0, 'y') assert 'y' in grid[2][0]
def test_ai_move(self): """Test Player.ai_move method.""" config = {'char': '@', 'ai': True} player = Player(config) grid = Grid(3) i = player.ai_move(grid) # Assert AI input is in correct format. assert re.match(r'^\d\,\d$', i) # Assert AI input is within the grid. i = i.split(',') x = int(i[0]) y = int(i[1]) assert 0 <= x <= grid.size assert 0 <= y <= grid.size
def test_full_grid(self): """Tests Grid.is_full method.""" grid = Grid(3) # Test full grid grid.data = [['X', 'Y', 'V'], ['V', 'X', 'Y'], ['X', 'Y', 'V']] assert grid.is_full() # Test not full grid grid.data = [['(0,1)', 'Y', 'V'], ['V', 'X', 'Y'], ['X', 'Y', 'V']] assert not grid.is_full()
def __init__(self, grid=Grid(), previous_state=None, is_max=True): self.grid = grid self.is_max = is_max self.previous_state = previous_state self.successors = [] self.value = 0
def test_grid_init(self): """Test Grid initialization.""" grid = Grid(10) assert isinstance(grid, Grid) assert isinstance(grid.data, list) assert grid.size == 10
def test_grid_flat(self, capsys): """Test output of Grid.flat method.""" grid = Grid(3) # Assert that flattened grid is a list with length of grid size squared assert isinstance(grid.flat(), list) and len( grid.flat()) == grid.size**2