-1

ecently, my friend challenged me to make an AI that plays the famous 2048 game in Java. I made it and it worked fine for a few minutes then got stuck. After a little debugging, I realized that it was not reading the board correctly and it thought that a 32 was a 1024. In order to explain why it did this, I must first explain how it gets the value of the tiles on the screen.

It first makes the user draw a square around the game board. This was done to tell the program where the game was on the screen. It then generates 16 points, each located in an individual tile. Using the Robot class, it stores the colors of those 16 points in an array.

Here is a picture of this (the small squares represent the points): enter image description here

It then checks if it has found a new color. If it has found a new color then it assigns the next power of 2 to that color. For example, the first color(the background) is assigned to 0. the next color(white) is assigned to 2 then the next is assigned to 4 and so on. The problem is that when the tiles move, they are animated that is, they don't just teleport to the correct location instead, they move to the correct location. this causes a moment where the wrong color passes under one of the 16 points and the program incorrectly reads that tile.

which brings me to my question: Is there any way to read the tile value in the game 2048 without having to recreate the game in Java?

EDIT:

More specifically, my question is How to get the actual game board Data from 2048 in java?

What I am asking is a way to get the values of the tiles in an external java program.

For Example, take this board:

enter image description here

From this board i would like to get the values of the numbers and store them in an array which would look something like this: {0,0,2,4,0,0,32,2,2,2,16,8,4,32,64,128}

Thanks in advance :)

Paul
  • 670
  • 7
  • 19
  • Your question should be: "Is there any way I can read the tile and make sure that I'm not erroneously reading the wrong temporary color?" – Kayaman Mar 01 '16 at 07:41
  • Very interesting question. Unfortunately too broad. – Dropout Mar 01 '16 at 07:41
  • @Dropout Could you tell me how to make the question less broad? – Paul Mar 01 '16 at 07:53
  • @Kayaman That is just how i went about solving the problem. I am also asking is there is another/ better way to go about it. – Paul Mar 01 '16 at 07:55
  • Sample repeatedly with a small time delay until 3(?) samples return the same value in every cell? I dunno, reaching. – Erick G. Hagstrom Mar 01 '16 at 07:56
  • This is neither interesting nor complicated. If you can't trust the first result you get, then don't trust the first result you get. It may introduce some slowness, but also correctness. – Kayaman Mar 01 '16 at 07:59
  • @Paul by breaking it up into smaller parts and asking specific questions on the way, while you code. – Dropout Mar 01 '16 at 08:02

1 Answers1

4

One way this can be done is by figuring out which color each tile represents. This assumes you are using the original version.

  • No tile: #cdc1b4
  • 2: #eee4da
  • 4: #ede0c8
  • 8: #f2b179
  • 16: #f59563
  • 32: #f67c5f
  • 64: #f65e3b
  • 128: #edcf72
  • 256: #edcc61
  • 512: #edc850
  • 1024: #edc53f
  • 2048+: #edc22e

As you can probably see, this will only work up to 2048. After that, they share the same colors.

So if you intend to go above 2048, you may need to use an Optical Character Recognition library. This will be slower, but would allow potentially any maximum, so long as it can be parsed. There are quite a few posted here.

The font appears to be Clear Sans.

All colors and fonts were taken from the spreadsheets of the game.

Community
  • 1
  • 1
Obicere
  • 2,999
  • 3
  • 20
  • 31
  • This is similar to what I have been doing so far, the problem is that the tiles are moving which causes the program to think that a tile is in a place where it shouldn't be. – Paul Mar 01 '16 at 07:52
  • If you're recalculating each tile's position after every move, that shouldn't be a problem. And unless you have access to the actual board data, you'd need to calculate the new value anyway. – Obicere Mar 01 '16 at 07:54
  • @obicerce The thing is that it moves too fast and when the next move is evaluated, the tile has partially moved causing the program to place the tile in the wrong place. This only affects one move as the board is recalculated every move but, it can cause the program to perform a move which leads to it's loss. – Paul Mar 01 '16 at 07:58
  • Then maybe multiple samples or a delay? Sure it might slow down each move a little. And if it becomes that big of an issue, the game is fairly simple to implement, [even being the subject to code golf!](http://codegolf.stackexchange.com/questions/24134/create-a-simple-2048-game-clone) – Obicere Mar 01 '16 at 08:01
  • @obicerce Just tried and delay of 1 second and it made it to 512 before it messed-up again. any longer and it will take too long to play. Also, if i used multiple samples then how will i choose the right sample to use? – Paul Mar 01 '16 at 08:14
  • Just Hard coded the colors and it worked! Thanks! Also, is there a way to go beyond 2048? Thanks soo much! – Paul Mar 01 '16 at 09:17
  • @Paul that would require the OCR and the font I suggested at the bottom. Either that or writing the game yourself, which shouldn't be too hard and save a lot of time. – Obicere Mar 01 '16 at 16:23
  • Just 1 last request, Could you post the value of the background color please? Thanks a lot for your time :) – Paul Mar 02 '16 at 03:02
  • @Paul updated. Its actually just a transparent layer on top of rounded-corner game area. And it can also be derived by simply not matching any of the other tile colors. – Obicere Mar 02 '16 at 03:06