Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/quasarbright/2048
An AI that plays 2048
https://github.com/quasarbright/2048
Last synced: 26 days ago
JSON representation
An AI that plays 2048
- Host: GitHub
- URL: https://github.com/quasarbright/2048
- Owner: quasarbright
- License: gpl-3.0
- Created: 2020-06-19T20:19:24.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-26T06:18:35.000Z (over 4 years ago)
- Last Synced: 2024-11-06T07:42:20.632Z (3 months ago)
- Language: Python
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 2048 AI
An AI for the game 2048The AI with search depth 5 always gets the 16k tile, usually gets the 30k tile, and sometimes gets the 60k tile.
The algorithm is simple minimax with a heuristic score. This algorithm isn't expectimax. Although the spawning is random, the randomness does not affect minimax from planning. I made it so the randomness is consistent across searches so the AI knows where tiles will spawn after a move. This obviously gives the AI a significant advantage over expectimax agents with less knowledge.
For the heuristic, I encourage the tiles to be arranged in a snaking pattern in decreasing order. This seems to be the optimal way to play. To be exact, I generated sequences of positions for each possible snaking hamiltonian path on the grid, and for each path, I used this algorithm to calculate a path score:
```python
def pathScore(path, grid):
score = 0
for v in path:
score *= 0.5
score += grid[v.y][v.x]
return score
```I take the maximum path score out of all the snake paths, divide by 10 if the game is over, and return that as the score. This heuristic leads to the AI organizing the tiles in whichever snake path is optimal at the time.
# dependencies
* python >=3.6
* numpy# running
run `python main.py 5` to run the AI with a search depth of 5