HNHacker News
TopNewBestAskShowJobs

ovolve

3 karma · joined March 22, 2012

submissionscomments
ovolve··on Show HN: 2048 turned 10 this year, I built an updated version to celebrate
Someone forked 2048-AI and added evil mode :) https://sztupy.hu/2048-Hard/ Originating in the StackOverflow comments here https://stackoverflow.com/questions/22342854/what-is-the-opt...

BTW, congrats on the whole thing; what a ride. I remember staying up all night implementing the AI after seeing it here, and the rush of seeing it win the first time, plus the added rush of seeing the AI post right next to the original post on here. Thanks for the fun!

ovolve··on 2048 AI
Ahh good catch. That got messed up during refactoring the original code. Thanks to the person who submitted the fix on github too.
ovolve··on 2048 AI
I've been messing around trying to find a balance between both of those heuristics. They're both implemented, but there doesn't seem to be any magic bullet.
ovolve··on 2048 AI
=)

Check out the eval function, and specifically the function smoothness() in grid.js. It implements the edge weighting you describe!

ovolve··on 2048 AI
Heh. If you look in the code you'll see a big commented out chunk where I tried randomly sampling computer moves to get sort of an 'expected value' for the opposition's move. Empirically, it performed worse. I think this is for the same reason that all minimax algos assume optimal play by the opponent: if you assume optimal and they play less than so, it can only work in your favor. However I think there's some truth in the fact that sometimes an unpredicted random computer move can mess things up. Unfortunately exhaustively enumerating all possible computer moves was way too slow (for single-threaded javascript in the browser).
ovolve··on 2048 AI
Trying every possibility was way too slow (branching factor of ~15 to 20), so it only searches the most "annoying" moves, where annoying is defined by lining up with the highest value tiles and not being adjacent to other 2's (or 4's). The game is random though, so it can and does make moves that haven't been searched.
ovolve··on 2048 AI
Random location (uniform). 90% chance of a 2, 10% chance of a 4. This actually made it hard to model the computer move in the search.
ovolve··on 2048 AI
Sorry, the code is a little unkempt.

The basic idea is minimax search. Googling that will get you started, but basically the algorithm plays out the game and keeps a score of the position after every move. Then it just makes the move that leads to the best score.

The "score" here is basically a count of how many free squares there are (with a little extra to keep things aligned if possible).

One major thing with these search algorithms is that the game tree grows exponentially as you move forward. To combat that, implemented alpha-beta pruning and a heuristic to only search the nastier computer moves rather than all of the possibilities.

ovolve··on 2048 AI
Source code here https://github.com/ov3y/2048-AI