Ejemplo n.º 1
0
    def test_open(self):
        my_grid = Grid(3, 3)

        # Don't open if the game isn't playing
        my_grid._state = GameState.WIN
        my_grid.open(0, 0)
        expected = ['   ', '   ', '   ']
        self.assertEqual(expected, self.grid_to_string(my_grid))
        my_grid._state = GameState.PLAYING

        # Empty grid will expose everything on click.
        my_grid.open(1, 1)
        expected = ['___', '___', '___']
        self.assertEqual(expected, self.grid_to_string(my_grid))

        # Empty grid with a bomb in the middle.
        my_grid.clear()
        expected = ['   ', ' X ', '   ']
        my_grid.get(1, 1).is_bomb = True
        my_grid.open(1, 1)
        self.assertEqual(expected, self.grid_to_string(my_grid))

        # New game with two bombs nothing opened
        my_grid.clear()
        expected = ['  x', '   ', 'x  ']
        my_grid.get(2, 0).is_bomb = True
        my_grid.get(0, 2).is_bomb = True
        self.assertEqual(expected, self.grid_to_string(my_grid))

        # Same grid open at 1, 0
        my_grid.open(1, 0)
        expected = [' 1x', '   ', 'x  ']
        self.assertEqual(expected, self.grid_to_string(my_grid))

        # Same grid open at 1, 0
        my_grid.open(0, 0)
        expected = ['_1x', '1  ', 'x  ']
        self.assertEqual(expected, self.grid_to_string(my_grid))

        my_grid = Grid(4, 4)
        my_grid.get(0, 1).is_bomb = True
        my_grid.get(1, 3).is_bomb = True
        my_grid.open(2, 1)
        expected = [' 1__', 'x1__', '  1_', ' x1_']
        self.assertEqual(expected, self.grid_to_string(my_grid))

        my_grid = Grid(3, 4)
        my_grid.get(1, 1).is_bomb = True
        my_grid.get(2, 2).is_bomb = True
        my_grid.open(0, 3)
        expected = ['   ', ' x ', '1 x', '_1 ']
        self.assertEqual(expected, self.grid_to_string(my_grid))
Ejemplo n.º 2
0
    def test_grid_equal(self):
        my_grid = Grid(3, 3)
        # width
        self.assertNotEqual(my_grid, Grid(2, 3))
        # height
        self.assertNotEqual(my_grid, Grid(3, 2))

        # check game state.
        my_grid._state = GameState.WIN
        self.assertNotEqual(my_grid, Grid(3, 3))
        my_grid._state = GameState.PLAYING  # back to normal

        # same size both default
        self.assertEqual(my_grid, Grid(3, 3))
        my_grid.get(0, 0).is_bomb = True
        # Not default state anymore
        self.assertNotEqual(my_grid, Grid(3, 3))
Ejemplo n.º 3
0
    def test_toggle_flagged(self):
        my_grid = Grid(3, 3)

        # should not change if state != playing
        my_grid._state = GameState.LOSE
        my_grid.toggle_flagged(0, 0)
        self.assertEqual(my_grid.get(0, 0).state, State.DEFAULT)

        # should change if playing
        my_grid._state = GameState.PLAYING
        my_grid.toggle_flagged(0, 0)
        self.assertEqual(my_grid.get(0, 0).state, State.FLAGGED)
        my_grid.toggle_flagged(0, 0)
        self.assertEqual(my_grid.get(0, 0).state, State.DEFAULT)

        # Didn't mix up row and column did we?
        my_grid._state = GameState.PLAYING
        my_grid.toggle_flagged(1, 0)
        self.assertEqual(my_grid.get(1, 0).state, State.FLAGGED)
        my_grid.toggle_flagged(1, 0)
        self.assertEqual(my_grid.get(1, 0).state, State.DEFAULT)

        # should not change if open.
        my_grid.get(0, 0).state = State.OPEN
        my_grid.toggle_flagged(0, 0)
        self.assertEqual(my_grid.get(0, 0).state, State.OPEN)

        # should throw an error if out of bounds.
        with self.assertRaises(
                ValueError,
                msg='Should not be able to access grid outside of bounds.'):
            my_grid.toggle_flagged(-1, 0)
            my_grid.toggle_flagged(0, -1)
            my_grid.toggle_flagged(3, 0)
            my_grid.toggle_flagged(0, 3)
            my_grid.toggle_flagged(3, 3)