# Help with my 2048

I want to be able to roll the matrix.

``````import numpy as np
import random

grid = np.zeros((4,4), dtype=int)

def init_grid():
rcoor = random.randint(0, 3)   #rcoor = random + coordination
rcoor1 = random.randint(0, 3)
grid[rcoor, rcoor1] = 2
rcoor = random.randint(0, 3)
rcoor1 = random.randint(0, 3)
grid[rcoor, rcoor1] = 2

rcoor = random.randint(0, 3)
rcoor1 = random.randint(0, 3)
num = random.randint(1, 10)   # < Choice 2 or 4
if num <= 8:                             # < Choice 2 or 4
num = 2                              # < Choice 2 or 4
else:                                        # < Choice 2 or 4
num = 4                              # < Choice 2 or 4
if is_empty(rcoor, rcoor1) == True:
grid[rcoor, rcoor1] = num
else:
if 0 not in grid:
print("Lose")

def is_empty(a, b):
if grid[a, b] == 0:
return True
else:
return False

init_grid()
print(grid)
<<<[[2 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 2]]
``````

It print:
[[2 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 2]]
(the result may vary)

Thank you for posting your code.

It is not clear to me what you mean by “roll the grid”.

I assume that this refers to how the pieces move in the game 2048. The user chooses a direction, and non-zero numbers in the grid shift as if by a force exerted in that direction. So, for the given input

``````[[2 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 2]]
``````

we might attain, by shifting up:

``````[[2 0 0 2]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
``````

or right:

``````[[0 0 0 2]
[0 0 0 0]
[0 0 0 0]
[0 0 0 2]]
``````
1 Like

It’s right