2048 : The game

Here are the playable versions of the game 2048.

Terminal version

To play this version of the game you just need a keyboard, and for greater comfort an AZERTY binded one.

Instructions

You may type in a python shell the follwing line, after what the game will start:

Game2048.launch_2048()

The keys are the following :

  • z : up

  • q : left

  • d : right

  • s : down

To submit the direction, you must press enter after entering it.

class Game_2048

Contains the necessary functions to launch either the 2048 user game or the A.I. ones.

display()

Displays the grid and score in a terminal.

See example :

0

2

2

4

0

0

4

2

4

0

2

2

8

2

4

8

Your score is 32

down_movement()

Makes the complete move of the cells to the bottom.

See example:

2

2

0

0

0

0

2

2

0

0

0

2

4

4

0

2

becomes :

0

0

0

0

0

0

0

0

2

2

0

2

4

4

2

4

Returns

the grid and the score after completing an up movement.

inverse()

Rotate the grid according to an axial symmetry. It will be used to define movements.

All movements are based on merge_left() and left stack().

See example:

2

2

2

2

0

2

2

2

0

0

2

2

0

0

0

2

becomes :

2

2

2

2

2

2

2

0

2

2

0

0

2

0

0

0

left_movement()

Makes the complete move of the cells to the left.

See example:

2

2

0

0

0

0

2

2

0

0

0

2

4

4

0

2

becomes :

4

0

0

0

4

0

0

0

2

0

0

0

8

2

0

0

Returns

the grid and the score after completing a left movement.

main()

Launches the user game.

Classic commands are used to play (azerty keyboard) :

  • z : up

  • q : left

  • d : right

  • s : down

To do an action you must select a direction and press enter.

To quit, write “quit” and press enter.

maxcell_find()

Identifies the greater value of the grid’s cells.

Returns

the value of the identified cell.

Return type

int

merge_left()

Merges the grid cells to the left.

See example:

0

2

2

0

2

4

4

2

0

2

0

4

8

8

8

8

becomes :

0

4

0

0

0

8

0

2

0

2

0

4

16

0

16

0

newcell()

Add new cell to the grid :

  • a 2 cell with 9/10 probability

  • a 4 cell with 1/10 probability

See example:

0

2

2

0

0

0

0

0

0

0

0

0

0

0

0

0

becomes :

0

2

2

0

0

0

0

0

4

0

0

0

0

0

0

0

Returns

the new grid.

newcell_start()

Initializes the grid with :

  • a 2 cell with 9/10 probability

  • a 4 cell with 1/10 probability

Returns

the initialized grid

possible_action()

Checks if a movement is possible on the grid.

See example:

In this grid, cells [0][0] and [0][1] can be merged in up and down movements.

2

4

8

4

2

8

4

8

4

2

8

4

2

4

2

8

Whereas in this one, no cells can be merged.

8

4

8

4

2

8

4

8

4

2

8

4

2

4

2

8

Returns

True if the movement is possible, false otherwise.

Return type

bool

right_movement()

Makes the complete move of the cells to the right.

See example:

2

2

0

0

0

0

2

2

0

0

0

2

4

4

0

2

becomes :

0

0

0

4

0

0

0

4

0

0

0

2

0

0

8

2

Returns

the grid and the score after completing an up movement.

stack()

Stacks the grid’s cells to the left.

See example:

0

2

0

2

0

0

0

8

4

0

0

2

0

2

8

0

becomes :

2

2

0

0

8

0

0

0

4

2

0

0

2

8

0

0

stop_game()

Checks if the game should be stop (when there is no more possible movement).

Returns

True if the game can continued, false otherwise.

Return type

bool

sup_2048()

Counts the amount of cells with values greater or equal than to 2048.

Returns

the count of these cells.

Return type

int

transpose()

Transposes (mathematically) the grid. It will be used to define movements. All movements are based on merge_left() and left stack().

See example:

2

2

2

2

0

2

2

2

0

0

2

2

0

0

0

2

becomes :

2

0

0

0

2

2

0

0

2

2

2

0

2

2

2

2

Returns

the transposed grid.

up_movement()

Makes the complete move of the cells to the top.

See example:

2

2

0

0

0

0

2

2

0

0

0

2

4

4

0

2

becomes :

2

2

2

4

4

4

0

2

0

0

0

0

0

0

0

0

Returns

the grid and the score after completing an up movement.

Graphic interface version

A graphical version looking like the classic game is also playable.

Instructions

The graphical game follows the same instructions as the terminal version. The only difference stands in the python command:

Game2048.launch_2048_visual()
../_images/Human_playing.gif

6561 : An alternative game

Unlike the 2048 game, this one consists of a 3x3 grid.

Instructions

Type the following code lines :

Game2048.launch_6561()

And play as you do with 2048.

class Game_6561

Alternative game to 2048 with a 3x3 grid and a goal score of 6561.

Contains the same functions as class Game_2048

(excepting A.I. plays ones).