Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Implements a function to test a strategy for the 2048.
3"""
4from .cp2048 import Game2048, GameOverException
7def evaluate_strategy(fct_strategy, ntries=10):
8 """
9 Applies method *best_move* until gameover
10 starting from the current position. Repeats *ntries* times
11 and the maximum number in every try.
13 :param fct_strategy: a function which returns the best move (see below)
14 :return: enumerator on scores
16 One example to show how to test a strategy:
18 ::
20 import random
21 from pystrat2048 import evaluate_strategy
23 def random_strategy(game, state, moves):
24 return random.randint(0, 3)
26 scores = list(evaluate_strategy(random_strategy))
27 print(scores)
28 """
29 for i in range(0, ntries):
30 g = Game2048()
31 while True:
32 try:
33 g.next_turn()
34 except (GameOverException, RuntimeError):
35 break
36 d = fct_strategy(g.game, g.state, g.moves)
37 g.play(d)
38 yield g.score()