Site icon R-bloggers

Playing Threes and 2048 with R

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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. 

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:

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 their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.