Skip to content

BhavyaC16/8402-Beat-the-computer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

8402-Beat-the-computer

To run your AI on the 2048 engine, run the following command, with your program's execution commands within quotes:

python3 puzzle.py "<enter your program's execution command>"

This engine was used for the event 'Toast to Code' in ESYA'19:Techfusion at IIIT-Delhi. Following is the problem statement for the contest:

The Task

Given the board configuration at a given instant, the task is to find the best way to place either the value 2 or 4 in one of the empty slots on board, in order to prevent the AI from forming a larger number by merging.

After each move by the AI, your code will be provided with the game grid, and it needs to return the coordinates and value(either 2 or 4) of the tile to be placed.

The grid is 0 indexed. The coordinates are in the order: (column, row). Example: Coordinates of 128 in the grid below are: (2,1).

Input:

The input consists of 4 lines, each of which contain 4 space separated integers, representing the board configuration.

Output:

Print the coordinates for the location of tile to be placed in same line as space separated integers

Following is the input and output format:

Input:

2 2 4 16
0 0 128 2
2 4 2 4
512 32 2 4

Output:

1 1
4

Any invalid output generated by the code will raise an “Invalid Output Error” and the program will be terminated.

The 2048 Bot

The AI is developed using the ‘expectimax’ algorithm. The expectimax search is coded as a recursive search which alternates between "expectation" steps (testing all possible tile spawn locations and values, and weighing their optimized scores by the probability of each possibility), and "maximization" steps (testing all possible moves and selecting the one with the best score). The tree search terminates when it sees a previously-seen position (using a transposition table), when it reaches a predefined depth limit, or when it reaches a board state that is highly unlikely (e.g. it was reached by getting 6 "4" tiles in a row from the starting position). The typical search depth is 4-8 moves.

Evaluation

The game is terminated when either of the following occurs:

  1. The game board is filled with no possibility of merging of tiles before the completion of 1024 moves.
  2. 1024 moves have been executed.

The bot made by the teams will be evaluated on the following basis:

For each move, the highest tile on the grid is considered as score and recorded. Upon the termination of the game, the scores are sorted by mean of sequence of 1024 moves of highest tile on the board after each move. If 2 teams get the same mean, then the mean of score of first 512 moves is considered and so on.

The scoring in case the game is terminated before 1024 moves:

The initial sequence of moves upto termination point is padded with zeros to make the sequence length 1024.

Sources

2048 AI

2048 Interface