Write a module of utility functions called util.py for manipulating 2-dimensiona
ID: 3695426 • Letter: W
Question
Write a module of utility functions called util.py for manipulating 2-dimensional arrays of size 4x4. (These functions will be used in Question 3.) The functions you need to write are as follows: def create_grid(grid):
"""create a 4x4 array of zeroes within grid"""
def print_grid (grid):
"""print out a 4x4 grid in 5-width columns within a box"""
def check_lost (grid):
"""return True if there are no 0 values and there are no adjacent values that are equal; otherwise False"""
def check_won (grid):
"""return True if a value>=32 is found in the grid; otherwise False"""
def copy_grid (grid):
"""return a copy of the given grid"""
def grid_equal (grid1, grid2):
"""check if 2 grids are equal - return boolean value"""
2048 is a puzzle game where the goal is to repeatedly merge adjacent numbers in a grid until the number 2048 is found. Your task in this question is to complete the code for a 2048 programme, using the utility module (util.py) from Question 2 and a supplied main programme (2048.py). The heart of the game is the set of merging functions that merge adjacent equal values and eliminate gaps - you are required ONLY to write these functions in a module named push.py:
def push_up (grid):
"""merge grid values upwards"""
def push_down (grid):
"""merge grid values downwards"""
def push_left (grid):
"""merge grid values left"""
def push_right (grid):
"""merge grid values right"""
Note: The check_won() function from util.py assumes you have won when you reach 32 - this is simply to make testing easier. The random number generator has been set to generate the same values each time for testing purposes.
testutil.py:
# grid utility routines
import util
# run test
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_one_test ():
test = int (input (""))
run_test (test)
def run_all_tests ():
for test in range (23):
print ("Test",test)
run_test (test)
run_one_test ()
2048.py:
# random number generator
import random
# grid utility routines
import util
# grid value merging routines
import push
def add_block (grid):
"""add a random number to a random location on the grid"""
# set distributon of number possibilities
options = [2,2,2,2,2,4]
# 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
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: ")
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)
# initialize the random number generator to a fixed sequence
random.seed (12)
# play the game
play ()
I've been trying to solve this problem all week and its driving me insane. Please if someone could show me a step by step solution I would be eternally gratefull.
Explanation / Answer
util.py
2048.py
testutil.py
push.py
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.