module td_1a.cp2048#

Inheritance diagram of ensae_teaching_cs.td_1a.cp2048

Short summary#

module ensae_teaching_cs.td_1a.cp2048

Simple strategy for 2048.

source on GitHub

Classes#

class

truncated documentation

Game2048

Implements the logic of the game 2048.

Game2048State

To store additional information while guessing the best move.

GameOverException

Raised when the game is over.

Functions#

function

truncated documentation

evaluate_strategy

Applies method best_move until gameover starting from the current position. Repeats ntries times and the maximum …

Static Methods#

staticmethod

truncated documentation

process_line

Moves numbers inside a vector whether this vector represents a row or a column.

Methods#

method

truncated documentation

__init__

__init__

__str__

Displays the game as a string.

best_move

Selects the best move knowing the current game. By default, selects a random direction. This function must …

copy

Makes a copy of the game.

gameover

Checks the game is over or not. Returns True in that case.

next_turn

Adds a number in the game.

play

Updates the game after a direction was chosen.

score

Returns the maximum values.

Documentation#

Simple strategy for 2048.

source on GitHub

class ensae_teaching_cs.td_1a.cp2048.Game2048(game=None)#

Bases : object

Implements the logic of the game 2048.

source on GitHub

Paramètres

game – None or matrix 4x4

source on GitHub

__init__(game=None)#
Paramètres

game – None or matrix 4x4

source on GitHub

__str__()#

Displays the game as a string.

best_move(game=None, state=None, moves=None)#

Selects the best move knowing the current game. By default, selects a random direction. This function must not modify the game.

Paramètres
  • game – 4x4 matrix or None for the current matrix

  • moves – all moves since the begining

Renvoie

one integer

source on GitHub

copy()#

Makes a copy of the game.

gameover()#

Checks the game is over or not. Returns True in that case.

next_turn()#

Adds a number in the game.

play(direction)#

Updates the game after a direction was chosen.

static process_line(line)#

Moves numbers inside a vector whether this vector represents a row or a column.

<<<

from ensae_teaching_cs.td_1a.cp2048 import Game2048
print(Game2048.process_line([0, 2, 2, 4]))

>>>

    [8, 0, 0, 0]

source on GitHub

score()#

Returns the maximum values.

class ensae_teaching_cs.td_1a.cp2048.Game2048State(game)#

Bases : object

To store additional information while guessing the best move.

source on GitHub

__init__(game)#
exception ensae_teaching_cs.td_1a.cp2048.GameOverException#

Bases : RuntimeError

Raised when the game is over.

source on GitHub

ensae_teaching_cs.td_1a.cp2048.evaluate_strategy(fct_strategy, ntries=10)#

Applies method best_move until gameover starting from the current position. Repeats ntries times and the maximum number in every try.

Paramètres

fct_strategy – a function which returns the best move (see below)

Renvoie

enumerator on scores

One example to show how to test a strategy:

<<<

import random
from ensae_teaching_cs.td_1a.cp2048 import evaluate_strategy


def random_strategy(game, state, moves):
    return random.randint(0, 3)


scores = list(evaluate_strategy(random_strategy))
print(scores)

>>>

    [128, 32, 64, 128, 32, 64, 64, 16, 32, 64]

source on GitHub