Question: Using Python 3. I have the following code for the game 2048. the code is pretty much done. The only thing I cannot get to

Using Python 3. I have the following code for the game 2048. the code is pretty much done. The only thing I cannot get to work is how to update the Score after each move. It should work just like when we play 2048, the score updates after each sucessful move. But i cannot do it. Below you will find my code.

import random as rnd import os import sys

class Grid():

def __init__(self, row=4, col=4, initial=2): self.row = row self.col = col self.initial = initial self.score = 0 self.size = row * col

self._grid = self.createGrid(row, col)

self.emptiesSet = list(range(row * col))

for _ in range(self.initial): self.assignRandCell(init=True)

def createGrid(self, row, col): grid = []

for i in range(col): grid.append([0] * row)

return grid def setCell(self, cell, val): row = cell // self.col col = cell % self.row self._grid[row][col] = val

def getCell(self, cell): row = cell // self.col col = cell % self.row return self._grid[row][col]

def assignRandCell(self, init=False):

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): 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): self.emptiesSet = [i for i in range(self.size) if self.getCell(i) == 0]

def collapsible(self): if self.emptiesSet != []: return True

for row in range(0, self.row * self.col, self.row):

for col in range(self.col - 1): current = self.getCell(row + col) if current == self.getCell(row + col + 1) and current != 0: return True

for i in range((self.row - 1) * self.col): if self.getCell(i) == self.getCell(i + self.col) and self.getCell(i) != 0: return True return False

def collapseRow(self, lst): length = len(lst) newList = [i for i in lst if i != 0] collapsedList = [] mem = None

for i in newList: if i == mem: collapsedList[-1] *= 2 mem = None else: collapsedList.append(i) mem = i

while len(collapsedList) != length: collapsedList.append(0)

return collapsedList, collapsedList != lst def collapseLeft(self): isCollapsed = False for i in range(self.size): if i % self.col == 0: row = [self.getCell(a) for a in range(i, i + self.col)] newRow, collapsed = self.collapseRow(row) if collapsed: isCollapsed = True self.setCell(i, newRow[i % self.col]) return isCollapsed

def collapseRight(self): isCollapsed = False for i in range(self.size): if i % self.col == 0: row = [self.getCell(a) for a in range(i, i + self.col)] row.reverse() newRow, collapsed = self.collapseRow(row) newRow.reverse()

if collapsed: isCollapsed = True self.setCell(i, newRow[i % self.col]) return isCollapsed

def collapseUp(self): isCollapsed = False for i in range(self.col): col = [self.getCell(a) for a in range(i, self.size, self.col)] newCol, collapsed = self.collapseRow(col) if collapsed: isCollapsed = True for ii in range(self.row): self.setCell(ii * self.col + i, newCol[ii]) return isCollapsed

def collapseDown(self): isCollapsed = False for i in range(self.col): col = [self.getCell(a) for a in range(i, self.size, self.col)] col.reverse() newCol, collapsed = self.collapseRow(col) newCol.reverse() if collapsed: isCollapsed = True for ii in range(self.row): self.setCell(ii * self.col + i, newCol[ii]) return isCollapsed

class Game(): def __init__(self, row=4, col=4, initial=2): self.game = Grid(row, col, initial) self.play() def printPrompt(self): 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))

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.updateEmptiesSet() 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!