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.addTile(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 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 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.addTile(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()