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 = GGL.gameGridLight(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 = GGL.gameGridLight(0, 0, tab) self.assertRaises(grid.add_random_tile()) 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 = GGL.gameGridLight(0, 0, tab) grid.matrix[row, column] = 0 grid.add_random_tile() self.assertTrue(grid.matrix.sum() > 16)
def test_MoveRight(self): tab = np.array( [[2,2,0,0], [2,0,2,0], [2,0,0,2], [0,2,0,2]] ) target = np.array( [[0,0,0,4], [0,0,0,4], [0,0,0,4], [0,0,0,4]] ) grid = GGL.gameGridLight(0,0,tab) grid.moveRight() self.assertTrue(GGL.array2DEquals(grid.matrix, target), str(grid.matrix) + "\n" + str(target)) tab = np.array( [[2,2,0,2], [2,0,4,2], [0,2,0,4], [4,0,2,2]] ) target = np.array( [[0,0,2,4], [0,2,4,2], [0,0,2,4], [0,0,4,4]] ) grid = GGL.gameGridLight(0,0,tab) grid.moveRight() self.assertTrue(GGL.array2DEquals(grid.matrix, target), str(grid.matrix) + "\n" + str(target))
def test_Initialisation(self): tab = np.array( [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]] ) grid = GGL.gameGridLight(0, 0, tab) self.assertEqual(4, grid.rows) self.assertEqual(4, grid.columns) grid = GGL.gameGridLight(3, 4) self.assertEqual(3, grid.rows) self.assertEqual(4, grid.columns) self.assertTrue(GGL.array2DEquals(grid.matrix, np.zeros([3,4])))
def runSimulation(params): firstDirection = params[0] gridMatrix = params[1] scores = [] deeps = [] grid = GGL.gameGridLight(matrix=gridMatrix) for i in range(NB_SIMULATIONS_MC): grid_MC = grid.clone() deep = 0 score = 0 while deep < DEEP_SIMULATIONS: if deep == 0: direction = firstDirection else: direction = random.choice( AVAILABLE_MOVES ) current_score, have_moved = grid_MC.moveTo(direction) score += current_score if have_moved: deep += 1 grid_MC.add_random_tile() else: break scores.append(score) deeps.append(deep) return getScore(scores, deeps)
def move_next(self, gameBoard, gridHistory, scoreHistory): if gameBoard.grid.isGameOver: return "" gridMatrix = gameBoard.grid.toIntMatrix() grid = GGL.gameGridLight(matrix=gridMatrix) direction, _ = evalMoveTo(grid, DEEP) return direction
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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(0, 0, loc_tab) self.assertFalse(grid.is_full(), "Error at ({0}, {1})\n{2}".format(row, col, grid.matrix))
def move_next(self, gameBoard, gridHistory, scoreHistory): if gameBoard.grid.isGameOver: return '' # print("\n\n_______________________________________________\n\n" ) gridMatrix = gameBoard.grid.toIntMatrix() # print(gridMatrix) grid = GGL.gameGridLight(matrix=gridMatrix) direction, _ = evalMoveTo(grid, DEEP) print("Moving to " + direction) return direction
def move_next(self, gameBoard, gridHistory, scoreHistory): if gameBoard.grid.isGameOver: return "" gridMatrix = gameBoard.grid.toIntMatrix() best_score = -1 best_move = "" for direction in ai_bestScoreNextMove.Available_moves: gameGrid = GGL.gameGridLight(matrix=gridMatrix) current_score, have_moved = gameGrid.moveTo(direction) if (have_moved) and (current_score > best_score): best_score = current_score best_move = direction print("{0:<5} with score {1}".format(best_move, best_score)) return best_move
def move_next(self, gameBoard, gridHistory, scoreHistory): if gameBoard.grid.isGameOver: return '' nbSimulation = 1000 deepSimulation = 6 simuationScores = SimulationNode() gridMatrix = gameBoard.grid.toIntMatrix() for i in range(nbSimulation): grid = GGL.gameGridLight(matrix=gridMatrix) deep = 0 score = 0 directions = [] while deep < deepSimulation: direction = random.choice( available_moves ) # print("{0} Moving {1}".format(' ' * deep, direction)) directions.append(direction) current_score, have_moved = grid.moveTo(direction) score += current_score if have_moved: deep += 1 grid.add_random_tile() else: deep = deepSimulation + 1 # end simuationScores.addScore(directions[0], score) # print("Action {0:<5} => {1} pts".format(directions[0], score)) best_move = simuationScores.getBestMove() # print("Best move : " + str(best_move)) # print("") return best_move
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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(matrix=tab) self.assertFalse(grid.canMoveDown()) for row in range(1, 4): for column in range(4): grid = GGL.gameGridLight(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 = GGL.gameGridLight(matrix=tab) self.assertFalse(grid.canMoveDown()) for row in range(1, 4): for column in range(4): grid = GGL.gameGridLight(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_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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(matrix=tab) self.assertFalse(grid.canMoveLeft()) for row in range(4): for column in range(0, 3): grid = GGL.gameGridLight(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 = GGL.gameGridLight(matrix=tab) self.assertFalse(grid.canMoveLeft()) for row in range(4): for column in range(1, 4): grid = GGL.gameGridLight(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_ArrayDEquals(self): a = np.array( [[0,0,0], [0,0,0]] ) b = np.array( [[0,0], [0,0]] ) self.assertFalse(GGL.array2DEquals(a, b)) b = np.array( [[0,0,0], [0,0,0], [0,0,0]] ) self.assertFalse(GGL.array2DEquals(a, b)) b = np.array( [[0,0,0], [0,0,0]] ) self.assertTrue(GGL.array2DEquals(a, b)) a = np.array( [[1,3,0], [0,4,0]] ) b = np.array( [[1,3,0], [1,4,0]] ) self.assertFalse(GGL.array2DEquals(a, b)) a = np.array( [[4,3,0], [0,4,0]] ) b = np.array( [[1,3,0], [0,4,0]] ) self.assertFalse(GGL.array2DEquals(a, b)) a = np.array( [[1,3,0], [0,4,9]] ) b = np.array( [[1,3,0], [0,4,0]] ) self.assertFalse(GGL.array2DEquals(a, b)) a = np.array( [[1,3,0], [0,4,0]] ) b = np.array( [[1,3,0], [0,4,0]] ) self.assertTrue(GGL.array2DEquals(a, b))
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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(0, 0, tab) self.assertFalse(grid.is_game_over())
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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(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 = GGL.gameGridLight(matrix=tab) self.assertFalse(grid.canMergeUpDown())