Question: Python Coding Problem : The 2048 Game To get used to the rules of the game you can play the game at https://gabrielecirulli.github.io/2048/. skeleton code:

Python Coding

Problem : The 2048 Game

To get used to the rules of the game you can play the game at https://gabrielecirulli.github.io/2048/.

skeleton 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): """  Create the grid here using the arguments row and col  as the number of rows and columns of the grid to be made.   The function should return the grid to be used in __init__()  """   pass     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    def assignRandCell(self, init=False): """  This function assigns a random empty cell of the grid  a value of 2 or 4.   In __init__() it only assigns cells the value of 2.   The distribution is set so that 75% of the time the random cell is  assigned a value of 2 and 25% of the time a random cell is assigned  a value of 4  """   if len(self.emptiesSet): cell = rnd.sample(self.emptiesSet, 1)[0] if init: self.setCell(cell, 2) else: cdf = rnd.random() if cdf > 0.75: self.setCell(cell, 4) else: self.setCell(cell, 2) self.emptiesSet.remove(cell) def drawGrid(self): """  This function draws the grid representing the state of the game  grid  """   for i in range(self.row): line = '\t|'  for j in range(self.col): if not self.getCell((i * self.row) + j): line += ' '.center(5) + '|'  else: line += str(self.getCell((i * self.row) + j)).center(5) + '|'  print(line) print() def updateEmptiesSet(self): """  This function should update the list of empty cells of the grid.  """   pass    def collapsible(self): """  This function should test if the grid of the game is collapsible  in any direction (left, right, up or down.)   It should return True if the grid is collapsible.  It should return False otherwise.  """   pass    def collapseRow(self, lst): """  This function takes a list lst and collapses it to the LEFT.   This function should return two values:  1. the collapsed list and  2. True if the list is collapsed and False otherwise.  """   pass    def collapseLeft(self): """  This function should use collapseRow() to collapse all the rows  in the grid to the LEFT.   This function should return True if any row of the grid is collapsed  and False otherwise.  """   pass   def collapseRight(self): """  This function should use collapseRow() to collapse all the rows  in the grid to the RIGHT.   This function should return True if any row of the grid is collapsed  and False otherwise.  """   pass    def collapseUp(self): """  This function should use collapseRow() to collapse all the columns  in the grid to UPWARD.   This function should return True if any column of the grid is  collapsed and False otherwise.  """   pass    def collapseDown(self): """  This function should use collapseRow() to collapse all the columns  in the grid to DOWNWARD.   This function should return True if any column of the grid is  collapsed and False otherwise.  """   pass  class Game(): def __init__(self, row=4, col=4, initial=2): """  Creates a game grid and begins the game  """   self.game = Grid(row, col, initial) self.play() def printPrompt(self): """  Prints the instructions and the game grid with a move prompt  """   if sys.platform == 'win32': os.system("cls") else: os.system("clear") print('Press "w", "a", "s", or "d" to move Up, Left, Down or Right respectively.') print('Enter "p" to quit. ') self.game.drawGrid() print(' Score: ' + str(self.game.score)) def play(self): moves = {'w' : 'Up', 'a' : 'Left', 's' : 'Down', 'd' : 'Right'} stop = False collapsible = True while not stop and collapsible: self.printPrompt() key = input(' Enter a move: ') while not key in list(moves.keys()) + ['p']: self.printPrompt() key = input(' Enter a move: ') if key == 'p': stop = True else: move = getattr(self.game, 'collapse' + moves[key]) collapsed = move() if collapsed: self.game.assignRandCell() collapsible = self.game.collapsible() if not collapsible: if sys.platform == 'win32': os.system("cls") else: os.system("clear") print() self.game.drawGrid() print(' Score: ' + str(self.game.score)) print('No more legal moves.') def main(): game = Game() #main() 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!