Search code examples
python2048

2048 game on 1d list - why it works for some tests and not others?


Trying to implement the 2048 game in python, starting with a function that merges a 1d list . My code works for all but one test see below. I believe this is due to the code not processing consecutive duplicates but not sure of the best way to implement this, here is my code so far:

def merge(line):
    """
    Function that merges a single row or column in 2048.
    """
    
    tile_merged = False
    first_element = 0
    result = [0 for element in line]
    
    for number in list(line): 
        if tile_merged == False:
            if number != 0 and result[first_element] == 0:
                result[first_element] = number
                tile_merged = False
               
            elif number != 0 and result[first_element] > 0:
                result[first_element] += number
                tile_merged = True
                first_element += 1
            tile_merged = False       
    return result

print merge([2,0,2,4]) # [4,4,0,0]
print merge([0,0,2,2]) # [4,0,0,0]
print merge([2,2,0,0]) # [4,0,0,0]
print merge([2,2,2,2,2]) # [4,4,2,0,0]
print merge([8,16,16,8]) # [24,24,0,0] should be [8,32,8,0]

any help appreciated.


Solution

  • I believe this function should do what you want:

    def merge(l):
        # keeping track of the previous element
        prev = None
        res = []
    
        for item in l:
            # if it is zero, we ignore it to remove the empty space
            if item == 0:
                continue
    
            if item == prev:
                # they are the same, so let's merge.
                # we replace the last item with the merged version
                res[-1] += item
    
                # set prev to None again to prevent this one from being merged with the next
                prev = None
    
            else:
                # they are not the same, so we just append it unchanged and store this item in prev.
                res.append(item)
                prev = item
    
        # pad with zeros
        res.extend([0] * (len(l) - len(res)))
    
        return res
    

    I tested it with your cases and it works.

    This simply keeps track of the previous item, ignores the zeros, and if the current one is the same as the previous one, then they are merged.