def push_down (grid): """merge grid values downwards""" new_grid=[] new_grid=util.create_grid(new_grid) while grid!=new_grid: new_grid=util.copy_grid(grid) for row in range(3,0,-1): for col in range(4): if grid[row][col]==0: grid[row][col]=grid[row-1][col] grid[row-1][col]=0 for row in range(3,0,-1): for col in range(4): if grid[row][col]==grid[row-1][col]: grid[row][col]= grid[row][col]+grid[row-1][col] grid[row-1][col]=0 for row in range(3,0,-1): for col in range(4): if grid[row][col]==0: grid[row][col]=grid[row-1][col] grid[row-1][col]=0 return grid
def push_right (grid): """merge grid values right""" newGrid = util.copy_grid (grid) for Row in range(4): ListRow = [] for Column in range(4): ListRow.append(grid[Row][Column]) for p in range(3): for Column in range(3,0,-1): if (Column!=0 and ListRow[Column]==0): for i in range(Column, 0,-1): ListRow[i]=ListRow[i-1] ListRow[i-1]=0 for Column in range(3,0,-1): if (Column!=0 and ListRow[Column]==ListRow[Column-1]): ListRow[Column]+=ListRow[Column-1] ListRow[Column-1]=0 for i in range(Column-1, 0,-1): ListRow[i]=ListRow[i-1] ListRow[i-1]=0 for Column in range(4): grid[Row][Column]=ListRow[Column] if(newGrid!=grid): push_right(grid)
def push_down (grid): """merge grid values downwards""" newGrid = util.copy_grid (grid) for Column in range(4): ListColumn = [] for Row in range(4): ListColumn.append(grid[Row][Column]) for p in range(3): for Row in range(3,0,-1): if (Row!=0 and ListColumn[Row]==0): for i in range(Row,0,-1): ListColumn[i]=ListColumn[i-1] ListColumn[i-1]=0 for Row in range(3,0,-1): if (Row!=0 and ListColumn[Row]==ListColumn[Row-1]): ListColumn[Row]+=ListColumn[Row-1] ListColumn[Row-1]=0 for i in range(Row-1, 0,-1): ListColumn[i]=ListColumn[i-1] ListColumn[i-1]=0 for Row in range(4): grid[Row][Column]=ListColumn[Row] if(newGrid!=grid): push_down(grid)
def push_down (grid): """merge grid values downwards""" grid_copy=[] grid_copy=util.create_grid(grid_copy) while grid!=grid_copy: grid_copy=util.copy_grid(grid) for y in range(3,0,-1): for x in range(4): if grid[y][x]==0: grid[y][x]=grid[y-1][x] grid[y-1][x]=0 for x in range(4): if grid[y][x]==grid[y-1][x]: grid[y][x]= grid[y][x]+grid[y-1][x] grid[y-1][x]=0 for y in range(3,0,-1): for x in range(4): if grid[y][x]==0: grid[y][x]=grid[y-1][x] grid[y-1][x]=0 return grid
def push_left (grid): """merge grid values left""" newGrid = util.copy_grid (grid) for Row in range(4): ListRow = [] for Column in range(4): ListRow.append(grid[Row][Column]) for p in range(3): for Column in range(4): if (Column!=3 and ListRow[Column]==0): for i in range(Column, 3): ListRow[i]=ListRow[i+1] ListRow[i+1]=0 for Column in range(4): if (Column!=3 and ListRow[Column]==ListRow[Column+1]): ListRow[Column]+=ListRow[Column+1] ListRow[Column+1]=0 for i in range(Column+1, 3): ListRow[i]=ListRow[i+1] ListRow[i+1]=0 for Column in range(4): grid[Row][Column]=ListRow[Column]
def push_down (grid): """merges grid values downwards""" #moves values downwards old_grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] while util.grid_equal(grid,old_grid) == False: old_grid = util.copy_grid(grid) for row in range(3,0,-1): for col in range(3,-1,-1): if grid[row][col] == 0 and grid[row-1][col] != 0: grid[row][col] = grid[row-1][col] grid[row-1][col] = 0 #merges values downwards for row in range (3,0,-1): for col in range(3,-1,-1): if grid[row][col] == grid[row-1][col]: grid[row][col] += grid[row-1][col] grid[row-1][col] = 0 #moves values downwards for row in range(3,0,-1): for col in range(3,-1,-1): if grid[row][col] == 0 and grid[row-1][col] != 0: grid[row][col] = grid[row-1][col] grid[row-1][col] = 0 return grid
def push_up (grid): """merge grid values upwards""" grid_copy=[]#grid_copy empty list grid_copy=util.create_grid(grid_copy) while grid!=grid_copy: grid_copy=util.copy_grid(grid)#this calls util and creates a grid copy for y in range(3):#iterate throught the list in grid for x in range(4): if grid[y][x]==0:#if the number is 0 we move the value to the right one unit grid[y][x]=grid[y+1][x] grid[y+1][x]=0 #zero value is then put in. for y in range(3): for x in range(4): if grid[y][x]==grid[y+1][x]: grid[y][x]= grid[y][x]+grid[y+1][x] grid[y+1][x]=0 for y in range(3): for x in range(4): if grid[y][x]==0: grid[y][x]=grid[y+1][x] grid[y+1][x]=0 return grid
def push_up (grid): """merges grid values upwards""" #moves values upwards old_grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] while util.grid_equal(grid,old_grid) == False: old_grid = util.copy_grid(grid) for row in range(3): for col in range(4): if grid[row][col] == 0 and grid[row+1][col] != 0: grid[row][col] = grid[row+1][col] grid[row+1][col] = 0 #merges like values upwards for row in range (3): for col in range(4): if grid[row][col] == grid[row+1][col]: grid[row][col] += grid[row+1][col] grid[row+1][col] = 0 #moves values upwards for row in range(3): for col in range(4): if grid[row][col] == 0 and grid[row+1][col] != 0: grid[row][col] = grid[row+1][col] grid[row+1][col] = 0 return grid
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): """merges grid values right""" #moves values to the right old_grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] while util.grid_equal(grid,old_grid) == False: old_grid = util.copy_grid(grid) for row in range(3,-1,-1): for col in range(3,0,-1): if grid[row][col] == 0 and grid[row][col-1] != 0: grid[row][col] = grid[row][col-1] grid[row][col-1] = 0 #merges values to the right for row in range (3,-1,-1): for col in range(3,0,-1): if grid[row][col] == grid[row][col-1]: grid[row][col] += grid[row][col-1] grid[row][col-1] = 0 #moves values to the right for row in range(3,-1,-1): for col in range(3,0,-1): if grid[row][col] == 0 and grid[row][col-1] != 0: grid[row][col] = grid[row][col-1] grid[row][col-1] = 0 return grid
def push_left(grid): test_grid=util.copy_grid(grid) # remove a single row from the grid for i in range(4): # shift the list left lis=test_grid[i] # add the values to the left lis=shift(lis) # shift the left to the left again lis=add(lis) lis=shift(lis) test_grid[i]==lis for row in range(4): # return the manipulated list into the grid for col in range(4): grid[row][col] = test_grid[row][col]
def push_left (grid): """merge grid values left""" test_grid = util.copy_grid (grid) for i in range(4): lis = test_grid[i]#pulls out a list according to the direction of the function name. A row is pulled out of the grid as a list on it's own lis=shift(lis) lis=add(lis) lis= shift(lis) for j in range(4): test_grid[i][j] = lis[j]#returns the values in the list to the same positions that they were extracted from in the grid altered_grid(grid,test_grid)#alters the grid with the edited version once all the changes have been made
def push_right (grid): """merge grid values right""" test_grid = util.copy_grid (grid) for i in range(4): lis = test_grid[i]#pulls out a list according to the direction of the function name. A row is pulled out of the grid as a list on it's own lis = lis[::-1]#because this function is to shift right, we reverse the string, apply the same shift and add methods, and once that is done, we reverse it back. This works because left and right are reverses of eachother in movement lis=shift(lis) lis=add(lis) lis= shift(lis) lis = lis[::-1] for j in range(4): test_grid[i][j] = lis[j]#returns the values in the list to the same positions that they were extracted from in the grid altered_grid(grid,test_grid)#alters the grid with the edited version once all the changes have been made
def push_right(grid): test_grid=util.copy_grid(grid) # remove a single row from a grid for i in range(4): lis=test_grid[i] lis=lis[::-1] # reverse the list lis=shift(lis) # shift the list to the left lis=add(lis) # add the values to the left lis=shift(lis) # shift the values to the left lis = lis[::-1] # reverse the list back again test_grid[i]=lis for row in range(4): for col in range(4): # add the list back into the grid grid[row][col] = test_grid[row][col]
def push_up (grid): """merge grid values upwards""" test_grid = util.copy_grid (grid)#creates a copy of the grid for us to work on for i in range(4): lis=[] for j in range(4): lis.append(test_grid[j][i])#pulls out a list according to the direction of the function name. A column is pulled out of the grid as a list on it's own lis=shift(lis) lis=add(lis) lis= shift(lis) for j in range(4): test_grid[j][i] = lis[j]#returns the values in the list to the same positions that they were extracted from in the grid altered_grid(grid,test_grid)#alters the grid with the edited version once all the changes have been made
def push_up(grid): test_grid=util.copy_grid(grid) for i in range(4): lis=[] for j in range(4): # remove a single column from a grid lis.append(test_grid[j][i]) lis=shift(lis) # shift the column to the left lis=add(lis) # add the values to the left lis=shift(lis) # shift the values to the left again for j in range(4): test_grid[j][i]=lis[j] # insert the manipulated list into the grid for row in range(4): for col in range(4): grid[row][col] = test_grid[row][col]
def push_up (grid): """merge grid values upwards""" #create temporary 4x4 grid temp_grid=util.copy_grid(grid) #populate temp_grid with the values of grid with rows and columns swapped for row in range(4): for col in range(4): temp_grid[row][col]=grid[col][row] #merge temp_grid to the left ("to the top of grid") push_left(temp_grid) #swop columns and rows again to get upwards-merged grid for row in range(4): for col in range(4): grid[row][col]=temp_grid[col][row] #return upwards-merged grid return grid
def push_up(grid): # Merge grid values upwards (i.e. copy the values into an array, manipulate the array and replace the origional array with the new array) old_grid = util.copy_grid(grid) # Create a copy of the origional grid while len(grid) > 0: del grid[0] util.create_grid(grid) # Grid with 0's for col in range(4): pos = 0 merge = 0 for row in range(4): if old_grid[row][col] == grid[merge][col]: grid[merge][col] += old_grid[row][col] merge += 1 else: grid[pos][col] = old_grid[row][col] pos += 1 if pos-merge >= 2: merge += 1
def push_right(grid): # Merge grid values left old_grid = util.copy_grid(grid) # Create a copy of the origional grid while len(grid) > 0: del grid[0] util.create_grid(grid) # Grid with 0's for row in range(4): pos = 3 merge = 3 for col in range(3,-1,-1): if old_grid[row][col] != 0: if old_grid[row][col] == grid[row][merge]: grid[row][merge] += old_grid[row][col] merge -= 1 else: grid[row][pos] = old_grid[row][col] pos -= 1 if merge - pos >= 2: merge -= 1
def push_left(grid): # Merge grid values right old_grid = util.copy_grid(grid) # Create a copy of the origional grid while len(grid) > 0: del grid[0] util.create_grid(grid) # Grid with 0's for row in range(4): pos = 0 merge = 0 for col in range(4): if old_grid[row][col] != 0: if old_grid[row][col] == grid[row][merge]: grid[row][merge] += old_grid[row][col] merge += 1 else: grid[row][pos] = old_grid[row][col] pos += 1 if pos - merge >= 2: merge += 1
def push_up(grid): """Merge numbers upward""" oldgrid = util.copy_grid(grid) while len(grid)> 0: del grid[0] util.create_grid(grid) for column in range(4): position = 0 merge = 0 for row in range(4): if oldgrid[row][column] != 0: if oldgrid[row][column] == grid[merge][column]: grid[merge][column] += oldgrid[row][column] merge += 1 else: grid[position][column] = oldgrid[row][column] position += 1 if position-merge >= 2: merge += 1
def push_right(grid): """Merge numbers to the right""" oldgrid = util.copy_grid(grid) while len(grid)> 0: del grid[0] util.create_grid(grid) for row in range(4): position = 3 merge = 3 for column in range(3,-1,-1): if oldgrid[row][column] != 0: if oldgrid[row][column] == grid[row][merge]: grid[row][merge] += oldgrid[row][column] merge -= 1 else: grid[row][position] = oldgrid[row][column] position -= 1 if merge - position >= 2: merge -= 1
def push_down (grid): """merge grid values downwards""" #create temporary 4x4 grid temp_grid=util.copy_grid(grid) #populate temp_grid with the values of grid with rows and columns swapped for row in range(4): for col in range(4): temp_grid[row][col]=grid[col][row] #merge temp_grid to the right ("to the bottom of grid") push_right(temp_grid) #swop columns and rows again to get downwards-merged grid for row in range(4): for col in range(4): grid[row][col]=temp_grid[col][row] #return downwards-merged grid return grid
def push_down(grid): test_grid=util.copy_grid(grid) for i in range(4): lis=[] for j in range(4): # remove a single column from a grid lis.append(test_grid[j][i]) lis=lis[::-1] # reverse the column lis=shift(lis) # shift the column to the left lis=add(lis) # add the values in the column to the left lis=shift(lis) # shift the column list left lis = lis[::-1] # reverse the column back for j in range(4): test_grid[j][i]=lis [j] for row in range(4): # re insert the column for col in range(4): grid[row][col] = test_grid[row][col]
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 push_right (grid): """merge grid values right""" grid_copy=[] grid_copy=util.create_grid(grid_copy) while grid!=grid_copy: grid_copy=util.copy_grid(grid) for y in range(4): for x in range(3,0,-1): if grid[y][x]==0: grid[y][x]=grid[y][x-1] grid[y][x-1]=0 for y in range(4): for x in range(3,0,-1): if grid[y][x]==grid[y][x-1]: grid[y][x]= grid[y][x]+grid[y][x-1] grid[y][x-1]=0 for y in range(4): for x in range(3,0,-1): if grid[y][x]==0: grid[y][x]=grid[y][x-1] grid[y][x-1]=0 return 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(): """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 push_left (grid): """merges grid values left""" #moves values to the left old_grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] while util.grid_equal(grid,old_grid) == False: old_grid = util.copy_grid(grid) for row in range(4): for col in range(3): if grid[row][col] == 0 and grid[row][col+1] != 0: grid[row][col] = grid[row][col+1] grid[row][col+1] = 0 #merges values to the left for row in range (4): for col in range(3): if grid[row][col] == grid[row][col+1]: grid[row][col] += grid[row][col+1] grid[row][col+1] = 0 #moves values to the left for row in range(4): for col in range(3): if grid[row][col] == 0 and grid[row][col+1] != 0: grid[row][col] = grid[row][col+1] grid[row][col+1] = 0 return grid
def push_up (grid): """merge grid values upwards""" newGrid = util.copy_grid (grid) for Column in range(4): ListColumn = [] for Row in range(4): ListColumn.append(grid[Row][Column]) for p in range(3): for Row in range(4): if (Row!=3 and ListColumn[Row]==0): for i in range(Row,3): ListColumn[i]=ListColumn[i+1] ListColumn[i+1]=0 for Row in range(4): if (Row!=3 and ListColumn[Row]==ListColumn[Row+1]): ListColumn[Row]+=ListColumn[Row+1] ListColumn[Row+1]=0 for i in range(Row+1, 3): ListColumn[i]=ListColumn[i+1] ListColumn[i+1]=0 for Row in range(4): grid[Row][Column]=ListColumn[Row]
def push_right (grid): """merge grid values right""" new_grid=[] new_grid=util.create_grid(new_grid) while grid!=new_grid: new_grid=util.copy_grid(grid) for row in range(4): for col in range(3,0,-1): if grid[row][col]==0: grid[row][col]=grid[row][col-1] grid[row][col-1]=0 for row in range(4): for col in range(3,0,-1): if grid[row][col]==grid[row][col-1]: grid[row][col]= grid[row][col]+grid[row][col-1] grid[row][col-1]=0 for row in range(4): for col in range(3,0,-1): if grid[row][col]==0: grid[row][col]=grid[row][col-1] grid[row][col-1]=0 return grid
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,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 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