Untitled
unknown
plain_text
4 years ago
2.3 kB
7
Indexable
# Module that contains functions for 2048
# Name: Anson Vattakunnel
# Student Number: VTTANS001
# Date: 19 April 2022
import copy
def create_grid(grid):
"""create a 4x4 array of zeroes within grid"""
for i in range(4):
rows = []
rows = [0 for j in range(4)]
grid.append(rows)
return grid
def print_grid (grid):
"""print out a 4x4 grid in 5-width columns within a box"""
for k in range(4):
if k == 0 :
print("+" + "-" * 20 + "+")
print("|", end = "")
for l in range(4):
if int(grid[k][l]) == 0: #Checks if element in array is zero, if so then print empty space
print('%-5s' % " ", end = "")
else:
print('%-5s' % grid[k][l], end = "")
print("|")
print("+" + "-" * 20 + "+")
def adjacentvalues(grid):
#Checks if any number is 0
for i in range(4):
for j in range(4):
if(grid[i][j]== 0):
return True
#Checks if any number to right or down is the same
for i in range(3):
for j in range(3):
if(grid[i][j]== grid[i + 1][j] or grid[i][j]== grid[i][j + 1]):
return True
#checks if any number on the last row is the same
for j in range(3):
if(grid[3][j]== grid[3][j + 1]):
return True
#Checks if any number on the last column is the same
for i in range(3):
if(grid[i][3]== grid[i + 1][3]):
return True
return False
def check_lost (grid):
"""return True if there are no 0 values and there are no
adjacent values that are equal; otherwise False"""
if 0 not in grid and not adjacentvalues(grid): #Checks if there is 0 and any equal values adjecent to it
return True
else:
return False
def check_won (grid):
"""return True if a value>=32 is found in the grid; otherwise False"""
for row in grid:
for num in row:
if num >= 32:
return True
return False
def copy_grid (grid):
"""return a copy of the given grid"""
duplicate = copy.deepcopy(grid)
return duplicate
def grid_equal (grid1, grid2):
"""check if 2 grids are equal - return boolean value"""
if grid1 == grid2:
return True
return FalseEditor is loading...