Hide keyboard shortcuts

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 random strategy for the game 2048. 

3""" 

4import random 

5 

6 

7def random_strategy(game, state=None, moves=None): 

8 """ 

9 Returns a random direction. 

10 

11 :param game: matrix usually 4x4 but could anything else 

12 :param state: keeps here anything you need, it will be 

13 here at the next call to the function 

14 :param moves: list of previous moves (unused) 

15 :return: a direction in `{0, 1, 2, 3}` 

16 """ 

17 return random.randint(0, 3) 

18 

19 

20def random_strategy_all_but_one(game, state=None, moves=None): 

21 """ 

22 Returns a random direction among three. 

23 

24 :param game: matrix usually 4x4 but could anything else 

25 :param state: keeps here anything you need, it will be 

26 here at the next call to the function 

27 :param moves: list of previous moves (unused) 

28 :return: a direction in `{0, 1, 2}` 

29 """ 

30 return random.randint(0, 2)