Tuesday, March 25, 2014

Artificial Intelligence for 2048

I've always wanted to write an artificial intelligence (AI) that plays a video game by actually "watching" the screen and actually sending the right keyboard commands.  Today, I've done that for 2048, a sliding-tile game implemented in Javascript by Gabriele Cirulli.

In brief, you are given a 4x4 grid and must input UP, DOWN, LEFT or RIGHT to slide tiles.  Tiles move as far as they can go and when two tiles with the same number collide, they merge into a new tile with double the value of the original tiles.  When a key is pressed every tile moves, so every move impacts the entire board.  After every move, a new tile appears on the grid at a random location (a "2" tile with 90% probability or a "4" tile with 10% probability).  (If that was unclear, just play the game for a minute).  The objective is to merge tiles until the 2048 tile appears, which is a formidable challenge.  When the board is full and no moves can be made, the game is over.

I implemented a 2048 AI agent in C# that uses the Minimax algorithm with Alpha-Beta pruning to perform a local search of the game space.

When the software is launched, it estimates the location of the 2048 grid on the screen by looking for the leftmost pixel with the color of the exterior grid (R=187, G=173, B=160) and assumes the grid is 500x500 pixels.  After it finds the grid, it pauses to allow you to set your browser to the active window and then enters a loop.  Within the loop, it:

  1. Estimates the current state of the game
  2. Performs the minimax search to a depth of 6 player moves
  3. Sends the appropriate keypress to 2048 in the browser
  4. Wait a second for the 2048 animation to resolve (otherwise state estimation will be wrong)
State estimation uses the distinctive colors of the tiles (parsed out of the CSS file at http://gabrielecirulli.github.io/2048/style/main.css) to determine which tile occupies each space.  Colors are sampled in the upper-left corner of each tile, to avoid conflicting with the number.

By choosing the minimax algorithm, the AI treats 2048 as if it were a two-player game.  The first player is simply "the player" (the agent) and the second "player" is the random tile placement.  The AI assumes that the random tile placement will be chosen in the worst possible place (from the perspective of the AI).  Obviously, sometimes the (random!) placement won't always be worst-case, but by making this assumption, riskier lines of gameplay can be avoided.

Since minimax can only feasibly look-ahead several moves, the quality of the farthest states it can look at must be estimated.  I've used a very simple heuristic.  The quality of a state is equal to:

Quality = - [ (Number of occupied tiles) + Entropy/ln(16) ]

In general, it tries to minimize the number of tiles on the board.  The Entropy term is just the Shannon entropy of the distribution of non-zero tiles on the grid.  In simple terms, the entropy term just says "Prefer to have tiles of a few types, rather than of many types".  The idea is that it's easier to combine tiles if you have a grid of three "4"s than if you have a grid with one "4", one "8" and one "16".  The entropy term is scaled to be in the range of 0.0-1.0 so that minimizing the number of occupied tiles in the grid is the dominant guiding force.

Keypresses are sent using the Windows.System.Forms.SendKey API, which is incredibly convenient.  However, you'll need to use an implementation of C# that allows this.  This works on Windows, but I'm not sure if it will work on Linux under Mono (possibly, but I haven't tried it).

Overall, the AI tends to do pretty well when playing the game.  It does not always win, but if often does.

Software is implemented in C# and is available on GibHub at: https://github.com/djparente/AI2048

Please feel free to leave feedback in the comments!

5 comments:

  1. Great work! I really like the logic to grab the state based on the colors on the screen, although sometimes I had issues with it and it didn't get the right state. Have you experienced that?

    I hope you don't mind, I took some of your code to make a Windows Forms based 2048 game. I made sure to mention your blog in mine: http://programmingwithalex.blogspot.com/2014/07/2048-ai-implimentation-in-c.html

    For some more feedback about your code, I'm not sure I agree with the entropy approach, instead maybe each turn should count less and less towards the score by some specific amount?

    In my experience the expectimax algorithm turned out to be the best approach as we know something about the probability of the "opponent".

    ReplyDelete
  2. Just when many were convinced of the futility of AI in the immediate future, an interesting article in Wired shows just how embedded it is in our everyday life, best virtual assistant program

    ReplyDelete
  3. You will you be able to confidently turn in your final dissertation, knowing that it is a high-quality academic document, which was edited by someone well versed in academic writing. best dissertation writing service uk Dissertation editing services have the skill to eliminate plagiarism and uncited works.

    ReplyDelete
  4. i love reading this article so beautiful!!great job! grammar tool

    ReplyDelete
  5. My Name is Abhay Kumar. I’m a Blogger living in India. I am a fan of technology, design, and writing. I’m also interested in programming and blog. I would like to share articles related to SEO, blogging tutorials, WordPress Tutorial, Make money online. You can read my blog.Python training course in gurgaon

    ReplyDelete