def test_is_full(self): tab = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_full()) tab = np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) grid = GameGrid(0, 0, tab) self.assertTrue(grid.is_full()) tab = np.array([[2, 4, 2, 4], [2, 4, 2, 4], [2, 4, 2, 4], [2, 4, 2, 4]]) grid = GameGrid(0, 0, tab) self.assertTrue(grid.is_full()) tab = np.array([[1, 1, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertTrue(grid.is_full()) for row in range(4): for col in range(4): loc_tab = np.array(tab) loc_tab[row, col] = 0 grid = GameGrid(0, 0, loc_tab) self.assertFalse( grid.is_full(), "Error at ({0}, {1})\n{2}".format(row, col, grid.matrix))
def evalAddTile(par_grid: GGL.GameGridLight, deep: int): if deep == 0: return par_grid.getScore() # print("evalAddTile Deep " + str(deep)) scores = np.zeros( 2 * par_grid.rows * par_grid.columns) + 1000000000 # + inf for x in range(par_grid.columns): for y in range(par_grid.rows): if not par_grid.canAddTile(x, y): continue for tileToAdd in [2, 4]: # print("Add tile {0} at ({1}, {2})".format(tileToAdd, x, y)) loc_grid = par_grid.clone() loc_grid.add_tile(x, y, tileToAdd) # print("Matrix") _, score = evalMoveTo(loc_grid, deep) # Yes, keep same deep here. # print("Add tile {0} at ({1}, {2}) => {3} pts".format(tileToAdd, x, y, score)) index = coord_to_index(loc_grid, x, y, tileToAdd) scores[index] = score # min score : suppose the added tile is at the worst position # print( scores ) return scores.min()
def evalMoveTo(par_grid: GGL.GameGridLight, deep: int): if deep == 0: return '', par_grid.getScore() # print("evalMoveTo Deep " + str(deep)) scores = [] for direction in AVAILABLE_MOVES: loc_grid = par_grid.clone() score, have_moved = loc_grid.moveTo(direction) # print("{0} => {1} pts".format(direction, score)) # print(loc_grid.matrix) if have_moved: score = evalAddTile(loc_grid, deep - 1) scores.append((direction, score)) else: scores.append((direction, -1)) # return argmax( scores ) best_direction = '' best_score = -1 for direction, score in scores: if score > best_score: best_direction = direction best_score = score # print("Best direction : " + str(best_direction)) return best_direction, best_score
def evalAddTile(par_grid: GGL.GameGridLight, deep: int): if deep == 0: return par_grid.getScore() scores = [] for x in range(par_grid.columns): for y in range(par_grid.rows): if not par_grid.canAddTile(x, y): continue for tileToAdd in [2, 4]: loc_grid = par_grid.clone() loc_grid.add_tile(x, y, tileToAdd) _, score = evalMoveTo(loc_grid, deep) # Yes, keep same deep here. scores.append(score) arrayScores = np.array(scores) avgScore = arrayScores.mean() return avgScore
def test_MoveUp(self): tab = np.array([[2, 2, 2, 0, 0, 0], [2, 0, 0, 2, 2, 0], [0, 2, 0, 2, 0, 2], [0, 0, 2, 0, 2, 2]]) target = np.array([[4, 4, 4, 4, 4, 4], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) grid = GameGrid(0, 0, tab) score, has_moved = grid.moveUp() self.assertTrue(array2DEquals(grid.matrix, target), "\n" + str(grid.matrix) + "\n" + str(target)) self.assertTrue(has_moved) self.assertEqual(24, score) tab = np.array([[2, 2, 2, 4, 0, 0], [2, 4, 0, 2, 2, 4], [4, 2, 4, 2, 4, 2], [0, 4, 2, 4, 2, 2]]) target = np.array([[4, 2, 2, 4, 2, 4], [4, 4, 4, 4, 4, 4], [0, 2, 2, 4, 2, 0], [0, 4, 0, 0, 0, 0]]) grid = GameGrid(0, 0, tab) score, has_moved = grid.moveUp() self.assertTrue(array2DEquals(grid.matrix, target), "\n" + str(grid.matrix) + "\n" + str(target)) self.assertTrue(has_moved) self.assertEqual(12, score) tab = np.array([[2, 2, 0, 4], [2, 0, 2, 0], [0, 4, 0, 2], [2, 2, 4, 2]]) target = np.array([[4, 2, 2, 4], [2, 4, 4, 4], [0, 2, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(0, 0, tab) score, has_moved = grid.moveUp() self.assertTrue(array2DEquals(grid.matrix, target), "\n" + str(grid.matrix) + "\n" + str(target)) self.assertTrue(has_moved) self.assertEqual(8, score)
def test_add_random_tile(self): tab = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(0, 0, tab) grid.add_random_tile() self.assertTrue(grid.matrix.sum() > 0) tab = np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) grid = GameGrid(0, 0, tab) grid.add_random_tile() self.assertTrue(array2DEquals(tab, grid.matrix)) tab = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) for row in range(4): for column in range(4): grid = GameGrid(0, 0, tab) grid.matrix[row, column] = 0 grid.add_random_tile() self.assertTrue(grid.matrix.sum() > 16)
def evalMoveTo(par_grid: GGL.GameGridLight, deep: int): if deep == 0: return '', par_grid.getScore() scores = [] for direction in AVAILABLE_MOVES: loc_grid = par_grid.clone() score, have_moved = loc_grid.moveTo(direction) if have_moved: score = evalAddTile(loc_grid, deep - 1) scores.append((direction, score)) else: scores.append((direction, -1)) best_direction = '' best_score = -1 for direction, score in scores: if score > best_score: best_direction = direction best_score = score return best_direction, best_score
def test_is_game_over_column(self): tab = np.array([[1, 5, 1, 5], [1, 6, 2, 6], [3, 7, 3, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [2, 7, 3, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 7, 3, 7], [3, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 5, 2, 6], [3, 7, 3, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 6, 3, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 7, 3, 7], [4, 7, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 1, 6], [3, 7, 3, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 7, 2, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 7, 3, 7], [4, 8, 3, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 5], [3, 7, 3, 7], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 7, 3, 6], [4, 8, 4, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 5, 1, 5], [2, 6, 2, 6], [3, 7, 3, 7], [4, 8, 4, 7]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over())
def test_is_game_over_lines(self): tab = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 1, 2], [3, 4, 3, 4], [1, 2, 1, 2], [3, 4, 3, 4]]) grid = GameGrid(0, 0, tab) self.assertTrue(grid.is_game_over()) tab = np.array([[1, 1, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 2, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 3], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 5, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 6, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 7], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 1, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 2, 4], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 3], [5, 6, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 5, 7, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 6, 8]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 7]]) grid = GameGrid(0, 0, tab) self.assertFalse(grid.is_game_over())
def test_canMoveLeft(self): # Set a number at the left of each column => can't move left tab = np.zeros([4, 4]) for row in range(4): grid = GameGrid(matrix=tab) grid.matrix[row, 0] = 2 self.assertFalse(grid.canMoveLeft(), "Can move left ! \n" + str(grid.matrix)) # Set a number everywhere but at the left of each column => can move left tab = np.zeros([4, 4]) for row in range(4): for column in range(1, 4): grid = GameGrid(matrix=tab) grid.matrix[row, column] = 2 self.assertTrue(grid.canMoveLeft()) # Set an empty tile everywhere but at the right of each column => can move left tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(matrix=tab) self.assertFalse(grid.canMoveLeft()) for row in range(4): for column in range(0, 3): grid = GameGrid(matrix=tab) grid.matrix[row, column] = 0 self.assertTrue(grid.canMoveLeft()) tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(matrix=tab) self.assertFalse(grid.canMoveLeft()) for row in range(4): for column in range(1, 4): grid = GameGrid(matrix=tab) grid.matrix[row, column] = grid.matrix[ row, column - 1] # copy cell from the one above self.assertTrue(grid.canMoveLeft(), "should move left\n" + str(grid.matrix)) # merge cells
def test_canMoveDown(self): # Set a number at the bottom of each column => can't move down tab = np.zeros([4, 4]) for column in range(4): grid = GameGrid(matrix=tab) grid.matrix[3, column] = 2 self.assertFalse(grid.canMoveDown(), "Can move down \n" + str(grid.matrix)) # Set a number everywhere but at the bottom of each column => can move down tab = np.zeros([4, 4]) for row in range(0, 3): for column in range(4): grid = GameGrid(matrix=tab) grid.matrix[row, column] = 2 self.assertTrue(grid.canMoveDown()) # Set an empty tile everywhere but at the top of each column => can move down tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(matrix=tab) self.assertFalse(grid.canMoveDown()) for row in range(1, 4): for column in range(4): grid = GameGrid(matrix=tab) grid.matrix[row, column] = 0 self.assertTrue(grid.canMoveDown()) # Set 2 identical consecutive tile in a column => merge cells tab = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]) grid = GameGrid(matrix=tab) self.assertFalse(grid.canMoveDown()) for row in range(1, 4): for column in range(4): grid = GameGrid(matrix=tab) grid.matrix[row, column] = grid.matrix[ row - 1, column] # copy cell from the one above self.assertTrue(grid.canMoveDown()) # merge cells
def test_canMergeUpDown(self): # Only one value, assert can't merge tab = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) for row in range(4): for column in range(4): grid = GameGrid(matrix=tab) grid.matrix[row, column] = 2 self.assertFalse(grid.canMergeUpDown(), "Can merge : \n" + str(grid.matrix)) tab = np.array([[2, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(matrix=tab) self.assertTrue(grid.canMergeUpDown()) tab = np.array([[0, 2, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(matrix=tab) self.assertTrue(grid.canMergeUpDown()) tab = np.array([[0, 0, 0, 2], [0, 0, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(matrix=tab) self.assertTrue(grid.canMergeUpDown()) tab = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 2], [0, 0, 0, 2]]) grid = GameGrid(matrix=tab) self.assertTrue(grid.canMergeUpDown()) tab = np.array([[3, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) grid = GameGrid(matrix=tab) self.assertFalse(grid.canMergeUpDown()) tab = np.array([[0, 0, 0, 3], [2, 3, 0, 0], [0, 0, 0, 3], [0, 0, 0, 4]]) grid = GameGrid(matrix=tab) self.assertFalse(grid.canMergeUpDown())