def play (): grid = [] util.create_grid (grid) add_block (grid) add_block (grid) won_message = False while (True): util.print_grid (grid) key = input ("Enter a direction:\n") if (key in ['x', 'u', 'd', 'l', 'r']): saved_grid = util.copy_grid (grid) if (key == 'x'): return elif (key == 'u'): push.push_up (grid) elif (key == 'd'): push.push_down (grid) elif (key == 'r'): push.push_right (grid) elif (key == 'l'): push.push_left (grid) if util.check_lost (grid): print ("Game Over!") return elif util.check_won (grid) and not won_message: print ("Won!") won_message = True if not util.grid_equal (saved_grid, grid): add_block (grid)
def push_right (grid): #merging them for col in range(4): for row in range(3,-1,-1): if grid[col][row] == grid[col][row-1]: #merges them grid[col][row] = grid[col][row] + grid[col][row-1] grid[col][row-1] = 0 #shifting util.print_grid(grid) count = 1 while count < 4: for col in range(4): for row in range(3,0,-1): if grid[col][row] == 0: if grid[col][row-1] != 0: grid[col][row] = grid[col][row-1] grid[col][row-1] = 0 count = count + 1 #merging again for col in range(4): for row in range(3,0,-1): if grid[col][row] == grid[col][row-1]: #merges them grid[col][row] = grid[col][row] + grid[col][row-1] grid[col][row-1] = 0 util.print_grid(grid) return grid
def push_down(grid): mat = transpose(grid) mat = reverse(mat) merge(mat) mat = reverse(mat) mat = transpose(mat) util.print_grid(mat) return mat
def push_left(grid): """merge grid values left""" count=0 for row in range(4): for col in range(3): block= grid[row][col] if block!= 0: #if adjacent block equals current block if (grid[row][col+1]==grid[row][col]): #newblock is equal to the combination of the blocks newblock= (grid[row][col+1]+grid[row][col]) position=grid[row][col] #make old blocks equal to 0 grid[row][col]=0 grid[row][col+1]=0 for column in range(4): if grid[row][column]==0: zeropos=column break grid[row][column]=newblock else: for col in range(4): #counts the number of zero's if grid[row][col]==0: count=count+1 break if grid[row][col+1]==0: count=count+1 if grid[row][col+2]==0: count=count+1 #current block equals adjacent values grid[row][col]=grid[row][col+count] grid[row][col+count]=0 count=0 #same as top, which merges adjacent values after they have been shifted for col in range(3): block= grid[row][col] if block!= 0: if (grid[row][col+1]==grid[row][col]): newblock= (grid[row][col+1]+grid[row][col]) position=grid[row][col] grid[row][col]=0 grid[row][col+1]=0 for column in range(4): if grid[row][column]==0: zeropos=column break grid[row][column]=newblock util.print_grid(grid)
def push_Right(grid): for y in range(3,0,-1): for x in range(3,0,-1): if grid[x][y]==0: continue else: for i in range(x,0,-1): #make sure not out of range if x-1 >= 0: if gird[x+1][y]==0: grid[x+1][y]=grid[x][y] grid[x][y]=0 for y in range(3,0,-1): for x in range(3,0,-1): if grid[x][y]== grid[x-1][y]: grid[x-1][y]= grid[x][y]+grid[x-1][y] util.print_grid(grid)
def push_down(grid): for y in range(3): for x in range(3): if grid[x][y]==0: continue else: for i in range(x,0,-1): #make sure not out of range if x-1 >= 0: if gird[x][y+1]==0: grid[x][y+1]=grid[x][y] grid[x][y]=0 for y2 in range(3): for x2 in range(3): if grid[x][y]== grid[x][y+1]: grid[x][y+1]= grid[x][y]+grid[x][y+1] util.print_grid(grid)
def play (): """generate grid and play game interactively""" # create grid grid = [] util.create_grid (grid) # add 2 starting random numbers add_block (grid) add_block (grid) won_message = False while (True): util.print_grid (grid) key = input ("Enter a direction:\n") if (key in ['x', 'u', 'd', 'l', 'r']): # make a copy of the grid saved_grid = util.copy_grid (grid) if (key == 'x'): # quit the game return # manipulate the grid depending on input elif (key == 'u'): push.push_up (grid) elif (key == 'd'): push.push_down (grid) elif (key == 'r'): push.push_right (grid) elif (key == 'l'): #print(grid) push.push_left (grid) #print(grid) # check for a grid with no more gaps or legal moves if util.check_lost (grid): print ("Game Over!") return # check for a grid with the final number elif util.check_won (grid) and not won_message: print ("Won!") won_message = True # finally add a random block if the grid has changed if not util.grid_equal (saved_grid, grid): add_block (grid)
def play(): """generate grid and play game interactively""" # create grid grid = [] util.create_grid(grid) # add 2 starting random numbers add_block(grid) add_block(grid) won_message = False while (True): util.print_grid(grid) key = input("Enter a direction:\n") if (key in ['x', 'u', 'd', 'l', 'r']): # make a copy of the grid saved_grid = util.copy_grid(grid) if (key == 'x'): # quit the game return # manipulate the grid depending on input elif (key == 'u'): push.push_up(grid) elif (key == 'd'): push.push_down(grid) elif (key == 'r'): push.push_right(grid) elif (key == 'l'): push.push_left(grid) # check for a grid with no more gaps or legal moves if util.check_lost(grid): print("Game Over!") return # check for a grid with the final number elif util.check_won(grid) and not won_message: print("Won!") won_message = True # finally add a random block if the grid has changed if not util.grid_equal(saved_grid, grid): add_block(grid)
def play (): # makes the game and plays # makes grid grid = [] util.create_grid (grid) # adds 2 random numbers to start with add_block (grid) add_block (grid) won_message = False while (True): util.print_grid (grid) key = input ("Enter a direction:\n") if (key in ['x', 'u', 'd', 'l', 'r']): # copies the grid saved_grid = util.copy_grid (grid) if (key == 'x'): # end the game return # edit the grid depending on the input elif (key == 'u'): push.push_up (grid) elif (key == 'd'): push.push_down (grid) elif (key == 'r'): push.push_right (grid) elif (key == 'l'): push.push_left (grid) # check for a grid with no more gaps or legal moves if util.check_lost (grid): print ("Game Over!") return # check for a grid with the last number elif util.check_won (grid) and not won_message: print ("Won!") won_message = True # lastly add a random block if the grid has changed if not util.grid_equal (saved_grid, grid): add_block (grid)
def play(): grid = [] # CREATE THE GRID util.create_grid(grid) # PLACE THE TWO NUMBERS ON THE BOARD util.generate_next_position(grid) while (True): # Clear the terminal so that the game seems more interactive os.system('cls' if os.name == 'nt' else 'clear') # PRINT THE CURRENT GRID util.print_grid(grid) # set 'key' to a user input which represents the move they would like to do key = input("Enter a direction:\n") # check if 'key' is one of the available options if (key in ['x', 'u', 'd', 'l', 'r']): if (key == 'x'): # quit the game return # manipulate the grid depending on input elif (key == 'u'): grid = push.push_up(grid) elif (key == 'd'): grid = push.push_down(grid) elif (key == 'r'): grid = push.push_right(grid) elif (key == 'l'): grid = push.push_left(grid) print(grid) # check for a grid with no more gaps or legal moves util.check_for_loss(grid) util.check_for_win(grid)
def push_up(grid): mat = transpose(grid) merge(mat) mat = transpose(mat) util.print_grid(mat) return mat
def play(): """generate grid and play game interactively""" # create grid grid = [] util.create_grid(grid) # add 2 starting random numbers add_block(grid) add_block(grid) # initially set 'won_message' to False. 'won_message' is used to determine if the user has been alerted that they won as to not keep printing the winning message if they continue playing. won_message = False # start while (True): # Clear the terminal so that the game seems more interactive os.system('cls' if os.name == 'nt' else 'clear') # print the current state of the grid util.print_grid(grid) # set 'key' to a user input which represents the move they would like to do key = input("Enter a direction:\n") # check if 'key' is one of the available options if (key in ['x', 'u', 'd', 'l', 'r']): # make a copy of the grid saved_grid = util.copy_grid(grid) if (key == 'x'): # quit the game return # manipulate the grid depending on input elif (key == 'u'): push.push_up(grid) elif (key == 'd'): push.push_down(grid) elif (key == 'r'): push.push_right(grid) elif (key == 'l'): push.push_left(grid) # check for a grid with no more gaps or legal moves if util.check_lost(grid): print("Game Over!") return # check for a grid with the final number elif not won_message and util.check_won(grid): print("Won!") won_message = True # Clear the terminal so that the game seems more interactive os.system('cls' if os.name == 'nt' else 'clear') # print the current state of the grid util.print_grid(grid) if input('Would you like to keep playing? (Y)es or (N)o: ' ).lower() == 'n': print('Game Over!') return # finally add a random block if the grid has changed if not util.grid_equal(saved_grid, grid): add_block(grid) # check for a grid with no more gaps or legal moves if util.check_lost(grid): # Clear the terminal so that the game seems more interactive os.system('cls' if os.name == 'nt' else 'clear') # print last state of grid util.print_grid(grid) print("Game Over!") return
##TODO I haven't incorporated a prior so this really is more of a maximum likelihood rather than bayesian irl algorithm reward = [[0, 0, 0, -1, 0, 0, 0], [0, -1, 0, -1, 0, -1, 0], [0, -1, 0, -1, 0, -1, 0], [1, -1, 0, 0, 0, -1, 0]] #true expert reward terminals = [21] #no terminals, you can change this if you want gamma = 0.95 #discount factor for mdp grid = gridworld.GridWorld(reward, terminals, gamma) #create grid world print "expert reward" util.print_reward(grid) pi_star, V_star = mdp_solver.policy_iteration(grid) #solve for expert policy print pi_star print "expert policy" util.print_policy(grid, pi_star) print "expert value function" util.print_grid(grid, np.reshape(V_star, (grid.rows, grid.cols))) Q_star = mdp_solver.calc_qvals(grid, pi_star, V_star, gamma) print "expert Q-values" print Q_star #give optimal action in each (non-terminal) state as demonstration #we can test giving demonstrations in some but not all states, or even noisy demonstrations to see what happens if we want demo = [(state, np.argmax(Q_star[state, :])) for state in range(grid.num_states) if state not in terminals] print "demonstration", demo ####### gradient descent starting from random guess at expert's reward reward_guess = np.reshape( [np.random.randint(-1, 2) for _ in range(grid.num_states)], (grid.rows, grid.cols)) #reward_guess = np.zeros((grid.rows,grid.cols))
def play(agent='human', record=True, filename='human_games.dat', delay=1): """generate grid and play game interactively""" # create grid grid = [] util.create_grid(grid) # add 2 starting random numbers add_block(grid) add_block(grid) won_message = False score = 0 trajectory = [] saved_grid = [[]] try: while (True): util.print_grid(grid) if agent == HUMAN: key = input("Enter a direction:\n") if (key == 'x'): # quit the game break try: key = util.action_num_to_letter(int(key)) except: continue elif agent == SVM or agent == NN: if util.grid_equal(saved_grid, grid): #previous action not allowed #use pior agent since model is deterministic given the same input key = agents.random_agent_with_prior() print("Prior") else: print("Model") model_agent = agents.Model() model_agent.load_model(filename="models/" + agent + ".joblib") key = model_agent.predict([np.array(grid).flatten()]) key = util.action_num_to_letter(int(key[0])) print(key) elif agent == RANDOM: key = agents.random_agent() elif agent == RANDOM_WITH_PRIOR: key = agents.random_agent_with_prior() if (key in ['u', 'd', 'l', 'r']): # make a copy of the grid saved_grid = util.copy_grid(grid) # manipulate the grid depending on input if (key == 'u'): _, add_score = push.push_up(grid) elif (key == 'd'): _, add_score = push.push_down(grid) elif (key == 'r'): _, add_score = push.push_right(grid) elif (key == 'l'): _, add_score = push.push_left(grid) score += add_score print("Score: ", score) if record and key in 'ludr': data_instance = np.array(saved_grid).flatten() data_instance = np.append( data_instance, [util.action_letter_to_num(key), add_score, score]) data_instance = np.append(data_instance, np.array(grid).flatten()) trajectory.append(data_instance) # check for a grid with no more gaps or legal moves if util.check_lost(grid): print("Game Over!") break # check for a grid with the final number elif util.check_won(grid) and not won_message: print("Won!") won_message = True # finally add a random block if the grid has changed if not util.grid_equal(saved_grid, grid): add_block(grid) if agent != HUMAN: time.sleep(delay) #in seconds finally: if record: util.save_game(trajectory, filename="games/" + agent.lower() + "/" + agent.lower())
def run_test (test): if test == 0: grid = [] util.create_grid (grid) print (len (grid)) print (len (grid[0])) print (len (grid[1])) print (len (grid[2])) print (len (grid[3])) print (grid[0][0]) print (grid[1][2]) print (grid[2][1]) print (grid[3][3]) elif test == 1: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] util.print_grid (grid) elif test == 2: grid = [[2 File "/home/rngkum001/python/util.py", line 21 print("|") ^ IndentationError: unindent does not match any outer indentation level rngkum001@sl-dual-78:~/python$ python3 question2.py > output.txt Traceback (most recent call last): ,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] util.print_grid (grid) elif test == 3: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.check_lost (grid)) elif test == 4: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] print (util.check_lost (grid)) elif test == 5: grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.check_lost (grid)) elif test == 6: grid = [[4,16,2,4],[2,4,16,2],[2,4,8,4],[4,8,4,2]] print (util.check_lost (grid)) elif test == 7: grid = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] print (util.check_lost (grid)) elif test == 8: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.check_won (grid)) elif test == 9: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] print (util.check_won (grid)) elif test == 10: grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.check_won (grid)) elif test == 11: grid = [[4,16,2,4],[2,4,16,2],[2,4,8,4],[4,8,4,2]] print (util.check_won (grid)) elif test == 12: grid = [[2,32,2,4],[4,2,16,2],[8,0,8,4],[2,0,4,2]] print (util.check_won (grid)) elif test == 13: grid = [[2,2,8,0],[0,8,16,0],[16,32,8,8],[2,8,4,4]] print (util.check_won (grid)) elif test == 14: grid = [[64,32,32,2],[8,4,2,0],[4,2,0,0],[2,0,0,0]] print (util.check_won (grid)) elif test == 15: grid = [[64,32,32,2],[8,4,2,0],[4,2,0,0],[2,0,0,0]] print (util.check_won (grid)) elif test == 16: grid = [[128,4,0,0],[8,4,2,0],[4,2,0,2],[2,0,0,0]] print (util.check_won (grid)) elif test == 17: grid = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] test_grid = util.copy_grid (grid) print (grid[0][0],test_grid[0][0]) print (grid[1][2],test_grid[1][2]) print (grid[3][3],test_grid[3][3]) grid[0][0] = 64 grid[1][2] = 64 grid[3][3] = 64 print (grid[0][0],test_grid[0][0]) print (grid[1][2],test_grid[1][2]) print (grid[3][3],test_grid[3][3]) elif test == 18: grid1 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] grid2 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] print (util.grid_equal (grid1, grid2)) elif test == 19: grid1 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] grid2 = [[4,2,8,2],[2,8,16,4],[16,32,8,4],[4,8,4,2]] print (util.grid_equal (grid1, grid2)) elif test == 20: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.grid_equal (grid1, grid2)) elif test == 21: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[2,4,8,16],[32,64,128,256],[512,1024,2048,4096],[8192,16384,32768,65536]] print (util.grid_equal (grid1, grid2)) elif test == 22: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.grid_equal (grid1, grid2))
def push_left(grid): merge(grid) util.print_grid(grid) return grid
def push_right(grid): mat = reverse(grid) merge(mat) mat = reverse(mat) util.print_grid(mat) return mat
def run_test (test): if test == 0: grid = [] util.create_grid (grid) print (len (grid)) print (len (grid[0])) print (len (grid[1])) print (len (grid[2])) print (len (grid[3])) print (grid[0][0]) print (grid[1][2]) print (grid[2][1]) print (grid[3][3]) elif test == 1: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] util.print_grid (grid) elif test == 2: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] util.print_grid (grid) elif test == 3: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.check_lost (grid)) elif test == 4: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] print (util.check_lost (grid)) elif test == 5: grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.check_lost (grid)) elif test == 6: grid = [[4,16,2,4],[2,4,16,2],[2,4,8,4],[4,8,4,2]] print (util.check_lost (grid)) elif test == 7: grid = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] print (util.check_lost (grid)) elif test == 8: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.check_won (grid)) elif test == 9: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] print (util.check_won (grid)) elif test == 10: grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.check_won (grid)) elif test == 11: grid = [[4,16,2,4],[2,4,16,2],[2,4,8,4],[4,8,4,2]] print (util.check_won (grid)) elif test == 12: grid = [[2,32,2,4],[4,2,16,2],[8,0,8,4],[2,0,4,2]] print (util.check_won (grid)) elif test == 13: grid = [[2,2,8,0],[0,8,16,0],[16,32,8,8],[2,8,4,4]] print (util.check_won (grid)) elif test == 14: grid = [[64,32,32,2],[8,4,2,0],[4,2,0,0],[2,0,0,0]] print (util.check_won (grid)) elif test == 15: grid = [[64,32,32,2],[8,4,2,0],[4,2,0,0],[2,0,0,0]] print (util.check_won (grid)) elif test == 16: grid = [[128,4,0,0],[8,4,2,0],[4,2,0,2],[2,0,0,0]] print (util.check_won (grid)) elif test == 17: grid = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] test_grid = util.copy_grid (grid) print (grid[0][0],test_grid[0][0]) print (grid[1][2],test_grid[1][2]) print (grid[3][3],test_grid[3][3]) grid[0][0] = 64 grid[1][2] = 64 grid[3][3] = 64 print (grid[0][0],test_grid[0][0]) print (grid[1][2],test_grid[1][2]) print (grid[3][3],test_grid[3][3]) elif test == 18: grid1 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] grid2 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] print (util.grid_equal (grid1, grid2)) elif test == 19: grid1 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] grid2 = [[4,2,8,2],[2,8,16,4],[16,32,8,4],[4,8,4,2]] print (util.grid_equal (grid1, grid2)) elif test == 20: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.grid_equal (grid1, grid2)) elif test == 21: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[2,4,8,16],[32,64,128,256],[512,1024,2048,4096],[8192,16384,32768,65536]] print (util.grid_equal (grid1, grid2)) elif test == 22: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.grid_equal (grid1, grid2))
def run_test (test): if test == 0: grid = [] util.create_grid (grid) print(grid) print (len (grid)) print (len (grid[0])) print (len (grid[1])) print (len (grid[2])) print (len (grid[3])) print (grid[0][0]) print (grid[1][2]) print (grid[2][1]) print (grid[3][3]) elif test == 1: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] util.print_grid (grid) elif test == 2: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] util.print_grid (grid) elif test == 3: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.check_lost (grid)) elif test == 4: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] print (util.check_lost (grid)) elif test == 5: grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.check_lost (grid)) elif test == 6: grid = [[4,16,2,4],[2,4,16,2],[2,4,8,4],[4,8,4,2]] print (util.check_lost (grid)) elif test == 7: grid = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] print (util.check_lost (grid)) elif test == 8: grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.check_won (grid)) elif test == 9: grid = [[2,0,2,0],[0,4,0,8],[0,16,0,128],[2,2,2,2]] print (util.check_won (grid)) elif test == 10: grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.check_won (grid)) elif test == 11: grid = [[4,16,2,4],[2,4,16,2],[2,4,8,4],[4,8,4,2]] print (util.check_won (grid)) elif test == 12: grid = [[2,32,2,4],[4,2,16,2],[8,0,8,4],[2,0,4,2]] print (util.check_won (grid)) elif test == 13: grid = [[2,2,8,0],[0,8,16,0],[16,32,8,8],[2,8,4,4]] print (util.check_won (grid)) elif test == 14: grid = [[64,32,32,2],[8,4,2,0],[4,2,0,0],[2,0,0,0]] print (util.check_won (grid)) elif test == 15: grid = [[64,32,32,2],[8,4,2,0],[4,2,0,0],[2,0,0,0]] print (util.check_won (grid)) elif test == 16: grid = [[128,4,0,0],[8,4,2,0],[4,2,0,2],[2,0,0,0]] print (util.check_won (grid)) elif test == 17: grid = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] test_grid = util.copy_grid (grid) print (grid[0][0],test_grid[0][0]) print (grid[1][2],test_grid[1][2]) print (grid[3][3],test_grid[3][3]) grid[0][0] = 64 grid[1][2] = 64 grid[3][3] = 64 print (grid[0][0],test_grid[0][0]) print (grid[1][2],test_grid[1][2]) print (grid[3][3],test_grid[3][3]) elif test == 18: grid1 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] grid2 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] print (util.grid_equal (grid1, grid2)) elif test == 19: grid1 = [[4,2,8,2],[2,8,16,8],[16,32,8,4],[4,8,4,2]] grid2 = [[4,2,8,2],[2,8,16,4],[16,32,8,4],[4,8,4,2]] print (util.grid_equal (grid1, grid2)) elif test == 20: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print (util.grid_equal (grid1, grid2)) elif test == 21: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[2,4,8,16],[32,64,128,256],[512,1024,2048,4096],[8192,16384,32768,65536]] print (util.grid_equal (grid1, grid2)) elif test == 22: grid1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] grid2 = [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]] print (util.grid_equal (grid1, grid2))
# get random number chosen = options[random.randint(0,len(options)-1)] found = False while (not found): # get random location x = random.randint (0, 3) y = random.randint (0, 3) # check and insert number if (grid[x][y] == 0): grid[x][y] = chosen found = True #grid = [[0,0,0,0],[0,0,0,0],[0,0,2,0],[2,0,0,0]] grid = [[3,3,4,5],[3,5,5,2],[0,5,2,2],[2,0,2,0]] util.print_grid(grid) #merge grid values upwards def push_right (grid): #merging them for col in range(4): for row in range(3,-1,-1): if grid[col][row] == grid[col][row-1]: #merges them grid[col][row] = grid[col][row] + grid[col][row-1] grid[col][row-1] = 0 #shifting util.print_grid(grid) count = 1 while count < 4: