EDIT: This is a naive algorithm, modelling human conscious thought process, and gets very weak results compared to AI that search all possibilities since it only looks one tile ahead. It was submitted early in the response timeline.
I have refined the algorithm and beaten the game! It may fail due to simple bad luck close to the end (you are forced to move down, which you should never do, and a tile appears where your highest should be. Just try to keep the top row filled, so moving left does not break the pattern), but basically you end up having a fixed part and a mobile part to play with. This is your objective:
This is the model I chose by default.
1024 512 256 128
8 16 32 64
4 2 x x
x x x x
The chosen corner is arbitrary, you basically never press one key (the forbidden move), and if you do, you press the contrary again and try to fix it. For future tiles the model always expects the next random tile to be a 2 and appear on the opposite side to the current model (while the first row is incomplete, on the bottom right corner, once the first row is completed, on the bottom left corner).
Here goes the algorithm. Around 80% wins (it seems it is always possible to win with more "professional" AI techniques, I am not sure about this, though.)
initiateModel();
while(!game_over)
{
checkCornerChosen(); // Unimplemented, but it might be an improvement to change the reference point
for each 3 possible move:
evaluateResult()
execute move with best score
if no move is available, execute forbidden move and undo, recalculateModel()
}
evaluateResult() {
calculatesBestCurrentModel()
calculates distance to chosen model
stores result
}
calculateBestCurrentModel() {
(according to the current highest tile acheived and their distribution)
}
A few pointers on the missing steps. Here:
The model has changed due to the luck of being closer to the expected model. The model the AI is trying to achieve is
512 256 128 x
X X x x
X X x x
x x x x
And the chain to get there has become:
512 256 64 O
8 16 32 O
4 x x x
x x x x
The O
represent forbidden spaces...
So it will press right, then right again, then (right or top depending on where the 4 has created) then will proceed to complete the chain until it gets:
So now the model and chain are back to:
512 256 128 64
4 8 16 32
X X x x
x x x x
Second pointer, it has had bad luck and its main spot has been taken. It is likely that it will fail, but it can still achieve it:
Here the model and chain is:
O 1024 512 256
O O O 128
8 16 32 64
4 x x x
When it manages to reach the 128 it gains a whole row is gained again:
O 1024 512 256
x x 128 128
x x x x
x x x x