module td_1a.cp2048
¶
Short summary¶
module ensae_teaching_cs.td_1a.cp2048
Simple strategy for 2048.
Classes¶
class |
truncated documentation |
---|---|
Implements the logic of the game 2048. |
|
To store additional information while guessing the best move. |
|
Raised when the game is over. |
Functions¶
function |
truncated documentation |
---|---|
Applies method best_move until gameover starting from the current position. Repeats ntries times and the maximum … |
Static Methods¶
staticmethod |
truncated documentation |
---|---|
Moves numbers inside a vector whether this vector represents a row or a column. |
Methods¶
method |
truncated documentation |
---|---|
Displays the game as a string. |
|
Selects the best move knowing the current game. By default, selects a random direction. This function must … |
|
Makes a copy of the game. |
|
Checks the game is over or not. Returns True in that case. |
|
Adds a number in the game. |
|
Updates the game after a direction was chosen. |
|
Returns the maximum values. |
Documentation¶
Simple strategy for 2048.
- class ensae_teaching_cs.td_1a.cp2048.Game2048(game=None)¶
Bases :
object
Implements the logic of the game 2048.
- Paramètres
game – None or matrix 4x4
- __init__(game=None)¶
- Paramètres
game – None or matrix 4x4
- __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
- 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]
- 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.
- __init__(game)¶
- exception ensae_teaching_cs.td_1a.cp2048.GameOverException¶
Bases :
RuntimeError
Raised when the game is over.
- 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)
>>>
[32, 32, 64, 128, 16, 64, 128, 32, 32, 32]