Question
I need help with the 2048 game on python 3. This is what I have on my code so far : import random as rnd
I need help with the 2048 game on python 3. This is what I have on my code so far :
import random as rnd import os import sys
class Grid(): def __init__(self, row=4, col=4, initial=2): self.row = row # number of rows in grid self.col = col # number of columns in grid self.initial = initial # number of initial cells filled self.score = 0
self._grid = self.createGrid(row, col) # creates the grid specified above
self.emptiesSet = list(range(row * col)) # list of empty cells
for _ in range(self.initial): # assignation to two random cells self.assignRandCell(init=True)
def createGrid(self, row, col): grid = [] for rows in range(row): col = [] for columns in range(col): col.append(0) grid.append(col) return(grid)
def setCell(self, cell, val):
""" This function should take two arguments cell and val and assign the cell of the grid numbered 'cell' the value in val.
This function does not need to return anything.
You should use this function to change values of the grid. """
pass
def getCell(self, cell):
"""" This function should return the value in cell number 'cell' of the grid.
You should use this function to access values of the grid """
pass
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I am trying to figure out how to implement the methods setCell and getCell. This is the hint I received from the assignment description :
Task 2
Once you are finished with Task 1, you can begin this task. Be sure to erase the test code from the previous task. Now, you will implement the setCell() and getCell() functions to set and access values in the grid. We can imagine the elements of the grid to be associated with a cell number. That is each element of the grid is a cell. So, in a (4 x 4) grid, the cells are numbered 0 through 15. As a result, instead of accessing an element using, say,grid._grid[1][2], we access that element with grid.getCell(6). Implement setCell() such that it takes two arguments, cell and val, and assigns the value of val to cell number cell in the grid. Implement getCell() such that it takes an argument, cell, and returns the value in the grid at cell number cell. Once you are done implementing the functions, append the following lines to the bottom of your program:
grid = Grid()
grid.setCell(6, 8)
print(grid._grid[1][2])
print(grid.getCell(6))
If your functions are implemented correctly, with the default values, you should see the following output:
8 8
The variable_grid is actually considered to be a private attribute and should not be manipulated directly. All forms of manipulating the grid in your program should be done using setCell() and getCell() functions. Erase the test codes before continuing. Now, uncomment lines 16 and 17 in the program. The two lines should look like this:
for _ in range(self.initial): self.assignRandCell(init=True)
Once you have uncommented those two lines. Add the following lines of code to the bottom of your program and run it:
grid = Grid()
grid.drawGrid()
Running your program should print output that looks like
| | | | 2 | | | | | | | | | | | | | 2 | | |
Don't worry if the2's are in different positions. The function assignRand() places2's in random empty tiles. The function drawGrid()is also already done for you so it need not be changed or re-written.
** EDIT**
Please ignore the indentation problems,I am having difficulties formating my question
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started