Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2048 game help on python 3 ? Current code : import random as rnd import os import sys class Grid(): def __init__(self, row=4, col=4, initial=2):

2048 game help on python 3 ?

Current code :

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 retu```````rn 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 """

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I am trying to implement the setCell and GetCell methods. Setcell should take two arguments cell and val and assign the cell of the grid numbered 'cell' the value in val while getCell should return the value in cell number 'cell' of the grid.

This is what the assignment description says :

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* disregard the wrong indent,the format won't let me change it further

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions

Question

what are general impressions of the talk from TED procrastinator

Answered: 1 week ago

Question

Explain the Neolithic age compared to the paleolithic age ?

Answered: 1 week ago

Question

What is loss of bone density and strength as ?

Answered: 1 week ago