mail@pastecode.io avatar
2 years ago
1.8 kB
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)]
    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:
                print('%-5s' % " ", end = "")    
                print('%-5s' % grid[k][l], end = "")
    print("+" + "-" * 20 + "+")

def adjacentvalues(grid):
    for i in range(4):
        for j in range(4):
            if(grid[i][j]== 0):
                return True
    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
    for j in range(3):
        if(grid[3][j]== grid[3][j + 1]):
            return True
    for i in range(3):
        if(grid[i][3]== grid[i + 1][3]):
            return True

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):
        return True
        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 False