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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

import random 

 

 

def create_grid(n=4): 

""" 

Create a new game grid 

:param n: (int) the size of the grid nxn 

:return: (list) the grid as a list of lists 

""" 

game_grid = [] 

# Grid of size n x n 

for i in range(0,n): 

game_grid.append([' ' for j in range(n)]) 

return game_grid 

 

 

 

def init_game(n=4): 

""" 

Create a new game grid with two tiles 

:param n: (int) the size of the grid nxn 

:return: (list) the game grid as a list of lists 

""" 

game_grid=create_grid(n) 

game_grid=grid_add_new_tile(game_grid) 

game_grid=grid_add_new_tile(game_grid) 

return game_grid 

 

 

 

 

 

def grid_add_new_tile(grid): 

""" 

Add a new tile in the grid game at a free position 

:param grid: (list) the game grid 

:return: (list) the game grid 

""" 

x,y = get_new_position(grid) 

grid[x][y]= get_value_new_tile() 

return grid 

 

 

 

def get_all_tiles(grid): 

""" 

Return the set of all tiles of a grid in a single list 

:param grid: (list) the game grid 

:return: (list) a list with the values of the tiles of the game_grid. If the position is free, the 0 value is returned 

""" 

tiles = [] 

for row in grid: 

for tile in row: 

if tile==' ': 

tile = 0 

tiles.append(tile) 

return tiles 

 

 

def get_value_new_tile(): 

""" 

Get the value 2 or 4 randomly with 90% of chance for 2 

:return: (int ) tile value between 2 or 4 

""" 

tile_value= 4 

a = random.random() 

if a < 0.9: 

tile_value=2 

return tile_value 

 

 

def grid_get_value(grid,x,y): 

""" 

Returns the value of the grid in coordinate (x,y) 

:param grid: (list)the grid game 

:param x: (int) the first coordinate 

:param y: (int) the second coordiate 

:return: (int) the value of the tile in (x,y). If there is no tile, return 0 

""" 

value = grid[x][y] 

if value == ' ': 

value = 0 

return value 

 

 

def get_empty_tiles_positions(grid): 

""" 

Return a list of tuples of coordinates of the empty tiles 

:param grid: (list) the game grid 

:return: (list of tuples) the list of tuple coordinates 

""" 

empty_tiles = [] 

for x in range(len(grid)): 

for y in range(len(grid)): 

if grid_get_value(grid, x, y) == 0: 

empty_tiles.append((x, y)) 

return empty_tiles 

 

 

def get_new_position(grid): 

""" 

Return the coordinate of a free position for a new tile in the grid 

:param grid: (list) the game grid 

:return: (tuple) the coordinate for the new tile 

""" 

return random.choice(get_empty_tiles_positions(grid)) 

 

 

 

 

 

print(create_grid(4))