python game 2048 out of list index
<p>I was working on the coursera <a href="http://www.codeskulptor.org/#user41_yCiAGeHByt_1.py" rel="nofollow">python project 2048</a> using codesculpter.
The code works fine when I try <code>4 x 4</code> or <code>5 x 5</code>, but it shows error when <code>4 x 5</code> or any other when <code>height != width</code>. I think I must have messed up somewhere in the <code>__init__</code> or other places but I couldn't figure out. </p>

<p>Could someone give me some suggestions?</p>

<p>Here is what I have tried so far:</p>

<pre><code>import poc_2048_gui
import random

# Directions, DO NOT MODIFY
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4

# Offsets for computing tile indices in each direction.
# DO NOT MODIFY this dictionary.
OFFSETS = {UP: (1, 0),
           DOWN: (-1, 0),
           LEFT: (0, 1),
           RIGHT: (0, -1)}

def merge(line):
    """
    Helper function that merges a single row or column in 2048
    """

    # creat output list and remove 0
    after_merge=[]
    storage = []


    for num_1 in range(len(line)):
        after_merge.append(0)
        if line[num_1] != 0 :
            storage.append(line[num_1])


    # sum number        
    for num_2 in range(len(storage)):
        if num_2+2&gt; len(storage):
            break
        elif storage[num_2]==storage[num_2+1]:
            storage[num_2]*=2
            storage.pop(num_2+1)

    # replace 0 in after merge          
    for num in range(len(storage)):
        after_merge[num]=storage[num]

    return after_merge


class TwentyFortyEight:
    """
    Class to run the game logic.
    """

    def __init__(self, grid_height, grid_width):
        self.grid_height = grid_height
        self.grid_width = grid_width
        self.cell=[]
        self.indices = {}
        self.indices[UP] = [[0,n] for n in range(grid_width)] 
        self.indices[LEFT] = [[n,0] for n in range(grid_height)] 
        self.indices[RIGHT] = [[n, grid_width - 1] for n in range(grid_height)]
        self.indices[DOWN] = [[grid_height - 1, n]for n in range(grid_width)]

        self.ranges = {}
        self.ranges[UP] = grid_height
        self.ranges[DOWN] = grid_height
        self.ranges[LEFT] = grid_width
        self.ranges[RIGHT] = grid_width

        #self.reset()

    def reset(self):
        """
        Reset the game so the grid is empty except for two
        initial tiles.
        """
        self.cell = [[0*(col+row) for row in range(self.grid_height)] for col in range (self.grid_width)]
        for count in range(2):
            self.new_tile()

    def __str__(self):
        """
        Return a string representation of the grid for debugging.
        """
        a_str = ""
        for row in range(self.grid_height):
            for col in range (self.grid_width):
                a_str += ( str(self.cell[row][col]) + " " )
            a_str += '\n'
        return a_str


    def get_grid_height(self):
        """
        Get the height of the board.
        """
        # replace with your code
        return self.grid_height

    def get_grid_width(self):
        """
        Get the width of the board.
        """
        # replace with your code
        return self.grid_width

    def move(self, direction):
        """
        Move all tiles in the given direction and add
        a new tile if any tiles moved.
        """
        a_list = []
        has_moved = False
        for index in self.indices[direction]:
            for step in range(self.ranges[direction]):
                a_list.append(self.cell[index[0] + OFFSETS[direction][0] * step]
                              [index[1] + OFFSETS[direction][1] * step])
            merged_list = merge(a_list)

            if merged_list != a_list:
                for step in range(self.ranges[direction]):
                    self.cell[index[0] + OFFSETS[direction][0] * step]   \
                    [index[1] + OFFSETS[direction][1] * step] = merged_list[step]
                has_moved = True
            a_list = []

        if has_moved:
            self.new_tile()

    def new_tile(self):
        """
        Create a new tile in a randomly selected empty
        square.  The tile should be 2 90% of the time and
        4 10% of the time.
        """
        # replace with your code
        row=0
        col=0
        available_positions = []
        for row in range(self.grid_height):
            for col in range(self.grid_width):
                if self.cell[row][col] == 0:
                        available_positions.append([row, col])
        if not available_positions:
            print "There are no available positions."

        random_pos=random.choice(available_positions)
        rand_val=random.randint(1,10)
        if rand_val&gt;=9:
            new_tile=4
        else:
            new_tile=2

        self.set_tile(random_pos[0], random_pos[1], new_tile)



    def set_tile(self, row, col, value):
        """
        Set the tile at position row, col to have the given value.
        """
        # replace with your code
        self.cell[row][col] = value

    def get_tile(self, row, col):
        """
        Return the value of the tile at position row, col.
        """
        # replace with your code
        return self.cell[row][col]



poc_2048_gui.run_gui(TwentyFortyEight(4, 4))
</code></pre>