Problem: 2048 Puzzle
Have you ever played 2048 puzzle? If not, give it a try!
The 2048 puzzle is played on a simple gray 44 grid, with numbered tiles that slide smoothly when a player
moves them using the four arrow keys.Every turn, a new tile will randomly appear in an empty spot on the board
with a value of either 2 or 4. Tiles slide as far as possible in the chosen direction until they are stopped by either
another tile or the edge of the grid. If two tiles of the same number collide while moving, they will merge into a
tile with the total value of the two tiles that collided. The resulting tile cannot merge with another tile again in
the same move. Higher-scoring tiles emit a soft glow.
In this assignment, you are to implement the AIs programs for this game. Unlike some chess games, two
players in this game (computer and human) take dierent actions in their turn. But adversarial search can be
applied to this game as well. Have fun!
1 Implementation Detail
To simplify your work, a skeleton code is provided to help you test you program on your own machine. This code
1. GameManager.py - Read-only. This module will load your AIs and have it compete automatically.
2. Grid.py - Read-only. This module provides Grid object with some useful operations - move, getAvaliable-
Cells, insertTile and clone (some of them may not be ecient enough). You may use these operations in
your AI code directly. However, to get a better performance, you are encouraged to write your own code
which can ONLY be used in your AI internally. (This means your AI's getMove function still takes a Grid
object as parameter, but it does not matter whether you use those provided operations or not.)
3. BaseAI.py - Read-only. All your AIs should inherit from this module and implement getMove function
which takes Grid as parameter, return a move (dierent move for dierent player).
4. ComputerAI.py - Read-only. Inherited from BaseAI, returns a computer action which is a tuple (x, y)
indicating the place you want to place a tile.
5. PlayerAI.py - Writable! Inherited from BaseAI, returns a number which indicates the player's action (0 -
> UP, 1 - > DOWN, 2 - > LEFT, 3 - > RIGHT). You NEED to modify this file to make it as intelligent
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
from random import randint
from BaseAI import BaseAI
# Algorithm Heuristic
def heuristic(self, grid):
score = 0
adj = 0
paths = 
# Compute Cell Value scoring
for x in range(4):
for y in range(4):
cell = grid.map[y][x]
if cell != 0:
score += (cell/2 - 1)
# Give a bonus adjacency score for being
# next to equivalent cells
if x > 0 and cell...