Playing Threes and 2048 with R

May 2, 2014
By

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

I'll say it again: the 2048 game is horribly addictive. Luckily for my productivity, though, I've been playing a lot less of it since I finally managed to complete the game's main goal: combine and double-up like-numbered tiles until you create the ultimate 2048 tile:

2048-winner

My strategy? Play the game like Tetris. Only move tiles down, left, and right and attempt to collect the largest tiles at the bottom. It is possible get into situations where you can only move up, which is likely to place an inaccessible low value tile at the bottom of the grid, but if you can avoid that situation you can get consistently high scores. This StackOverflow thread suggests some other strategies.

If you want to test out your own strategy, Mark Patterson has developed a neat framework in the R language to do so. You just need to write your own algorithm to make a move from the current state of the board, and Mark's script will go to the 2048 website, play a game according to your algorithm, and report back your score. It does this using the RSelenium package, which allows R to remotely control web pages. Its main use is for automated testing of Web applications, but here Mark uses it to play a game. Clever! 

For those of you who have mastered 2048 and are looking for another fix, I recommend the Threes iOS game.  It's very similar to 2048, but more difficult because the first pair you make in the sequence is a 1 and a 2, to make a 3. The fact that you have to match different tiles to begin adds another layer of complexity to the game. 

Threes-iphone-screenshot

Similar strategies apply to Threes as do for 2048, though. To get a sense of a "baseline" score for Threes, data scientist and co-founder of Datascope Mike Stringer simulated the "control" strategy of randomly swiping left, right, up and down and found the following distribution of scores:

Threes-random-swiping

Mike used a similar technique to Mark's to simulate the games, using Python to drive a web-based clone of Threes. (There's also a web-based clone.) Mike was also kind enough to compare a Tetris strategy (randomly move left, right and down, but never up) to this control, but a Kolmogorov-Smirnov test in R found no significant difference between the score distributions. Still, my high score in Threes (if I may brag) is 32,976 using a variant of this method, so I still think there's something to it. (Mostly I move left/right/down, but occasionally up if absolutely necessary or to match a stuck low-value tile near the bottom.) 

Anyone else got good strategies for Threes or 2048?

To leave a comment for the author, please follow the link and comment on his blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.