class Game: def __init__(self, game_id): self.game_id = game_id print(game_id) self.board = Board() self.last_open_pos = Position(0, 0) def open_cell(self, x, y): if not self.board.game_over: self.last_open_pos.x = x self.last_open_pos.y = y return self.board.open_cell(x, y) def mark_cell(self, x, y): if not self.board.game_over: return self.board.mark_cell(x, y) def is_game_over(self): return self.board.game_over def get_user_board(self): return self.board.user_board def is_win(self): return self.board.check_win()
def __init__(self, game_id): self.game_id = game_id print(game_id) self.board = Board() self.last_open_pos = Position(0, 0)
def setUp(self): self.board = Board(SAMPLE_GRID)
class TestBoard(unittest.TestCase): def setUp(self): self.board = Board(SAMPLE_GRID) def test_find_neighbors(self): ''' Test neighbor cells to a cell are identified correctly. ''' column = 4 row = 4 expected_locations = [ (3, 3), (3, 4), (3, 5), (4, 3), (4, 5), (5, 3), (5, 4), (5, 5) ] neigbors = self.board.find_neighbors(column, row) self.assertEquals(len(neigbors), len(expected_locations)) locations = [neigbor['location'] for neigbor in neigbors] self.assertEquals(expected_locations, locations) def test_find_neighbors_2(self): ''' Test neighbor cells to a cell are identified correctly. ''' column = 8 row = 8 expected_locations = [(7, 7), (7, 8), (8, 7)] neigbors = self.board.find_neighbors(column, row) self.assertEquals(len(neigbors), len(expected_locations)) locations = [neigbor['location'] for neigbor in neigbors] self.assertEquals(expected_locations, locations) def test_mines(self): ''' Test all mines are placed on the board and in their expected locations. ''' mines = self.board.mines self.assertEquals(NUMBER_OF_MINES, len(mines)) self.assertEquals(SAMPLE_MINES, mines) def test_flags(self): ''' Test flags are in their expected locations. ''' flags = self.board.flags self.assertEquals(5, len(flags)) self.assertEquals(SAMPLE_FLAGS, flags) def test_game_is_ongoing(self): ''' winner == None means player hasn't lost or won yet. ''' self.board.check_if_winner() self.assertIsNone(self.board.winner) def test_player_lost(self): ''' Test that opening a mined cell leads to losing the game. ''' mine = [ mine for mine in SAMPLE_MINES if not mine['is_flagged'] ][0] self.board.open_cell(mine) self.assertEquals(self.board.winner, False) def test_player_won(self): ''' Test condition for winning the game. ''' unflagged_mines = [ mine for mine in SAMPLE_MINES if not mine['is_flagged'] ] for cell in unflagged_mines: row, column = cell['location'] self.board.flag_cell(row, column) self.assertTrue(self.board.winner)