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):
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:
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 :)
One way this can be done is by figuring out which color each tile represents. This assumes you are using the original version.
#cdc1b4
#eee4da
#ede0c8
#f2b179
#f59563
#f67c5f
#f65e3b
#edcf72
#edcc61
#edc850
#edc53f
#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.