# File: Spamsweeper.py # Author: # Time Spent: # Comments: from random import * ### Add your code here ### Provided code below this line. No need to modify. class Boardcell: '''An object that represents a cell in the spamsweeper board.''' def __init__(self): ''' Initialize a board cell. Initially cells are not mined, exposed and they have 0 neighbors. ''' self.mined = False self.exposed = False self.neighbors = 0 def __repr__(self): ''' returns the string representation for a board cell''' return "Boardcell: Mined: "+ str(self.mined) + \ " Exposed: " + str(self.exposed) + " Neighbors: " + \ str(self.neighbors) class Gameboard: def __init__(self, numrows, numcols, nummines): ''' initialize a new spamsweeper gameboard ''' self.rows = numrows self.cols = numcols self._grid = [] # grid starts with an underscore to # signal that it's not to be accessed directly # initialize the cells for r in range(numrows): self._grid.append([]) for c in range(numcols): self._grid[r].append(Boardcell()) # now set the mines while nummines > 0: rand_r = choice(range(numrows)) rand_c = choice(range(numcols)) if not self.getMined(rand_r, rand_c): self._grid[rand_r][rand_c].mined = True nummines = nummines - 1 self.computeNeighborCounts() def computeNeighborCounts(self): ''' compute and store the number of mined neighbors each node has. ''' for r in range(self.rows): for c in range(self.cols): count = 0 for rN in range(r-1, r+2): for cN in range(c-1, c+2): if rN >= 0 and rN < self.rows and cN >= 0 and \ cN < self.cols: if self.getMined(rN, cN): count += 1 self._grid[r][c].neighbors = count # Since we do not want the user of Gameboard to access the grid # we provide a few accessor functions. Note: this differ from # getters and setters in Java because the board is not # technically private. def setExposed(self, r, c): ''' expose the cell at row r, col c ''' self._grid[r][c].exposed = True def getExposed(self, r, c): ''' determine whether the cell at row r, col c is exposed ''' return self._grid[r][c].exposed def getNeighborCount( self, r, c ): ''' Get the number of mined neighbors at row r, col c ''' return self._grid[r][c].neighbors def getMined( self, r, c ): ''' Determine whether cell r, c is mined ''' cell = self._grid[r][c] return cell.mined def cheatPrintBoard(self): '''For debugging purposes, print the board and show where the cans of spam (mines) are located!''' for c in range(self.cols): print str(chr(ord('a')+c)), print "\n" for r in range(self.rows): for c in range(self.cols): if self.getMined(r,c): print "S", else: print str(self.getNeighborCount(r, c)), print "" def printBoard(self): ''' Prints the string representation of the board for the player to see.''' print self def __repr__(self): '''Returns a string representation of the board All cells that have marked as exposed are shown with their correct values (either Spam or the neighbor count). All cells that have not yet been exposed are drawn with "-" symbols.''' ret = "" for c in range(self.cols): ret += str(chr(ord('a')+c)) ret += "\n\n" for r in range(self.rows): for c in range(self.cols): if self.getExposed(r, c): if self.getMined(r, c): ret += "S" else: ret += str(self.getNeighborCount(r, c)) else: ret += "-" ret += " "+ str(r)+"\n" return ret