I've written a short Selenium application in Java, to run the 2048 game:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver = new FirefoxDriver();
Actions actions = new Actions(driver);
actions = actions.sendKeys(Keys.ARROW_UP);
actions = actions.sendKeys(Keys.ARROW_RIGHT);
actions = actions.sendKeys(Keys.ARROW_DOWN);
actions = actions.sendKeys(Keys.ARROW_LEFT);
Action action = actions.build();
By byButtonXpath = By.xpath("//a[@class='restart-button']");
By byMessageXpath = By.xpath("//div[@class='game-message game-over']");
driver.get("http://gabrielecirulli.github.io/2048/");
while (true)
{
driver.findElement(byButtonXpath).click();
do action.perform();
while (driver.findElements(byMessageXpath).size() == 0);
}
I've assumed that the best heuristic approach would be to keep the board as balanced as possible. In mathematical terms, I guess it could be asserted by aiming for a minimal standard-deviation of the numbers on the board.
Based on this assumption, I have used is a simple clockwise arrow-clicking (up, right, left, right) algorithm. Now, obviously, this is not a very sophisticated algorithm, yet it still broke my own record (perhaps I'm not very sophisticated either).
In any case, my questions are:
- Is my heuristic approach of keeping the board as balanced as possible at all times correct?
- Are there any known algorithms that consider the current numbers before making the next move?
Thanks