Question

Create a version of Minesweeper Game in Python.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

class Minesweeper(object):
    ''' Minesweeper class - Plays Minesweeper.

    It provides methods that carry out most of the needed functionality. It has
    the following data structures:
       size: Integer. The board is a square Size x Size grid. Default value 5.
       mines: Integer. The board contains this many bombs, randomly placed at
       start of game. Default value 3.
       actualBoard: A list of size lists, each size items long. This represents
       the current game state. Possible list contents:
            'M': This cell contains a mine (bomb).
            0-8: An integer, giving the number of mines in this cell's
            neighborhood.
       playerBoard: A list of lists, the same size as ActualBoard, containing
       the board as it will be displayed to the player. Possible contents:
            'H': This cell is hidden and can be turned over.
            0-8: This cell has this many mines in its neighborhood.
            'X': Used if the player loses, this shows the location of a mine.
       gameState: Has 3 possible states: "win", "loss", "playing" (These are
       strings)
    '''

    def __init__(self, sideSize = 5, mines = 3):
       ''' Initializer. Sets up boards, places mines (via call to method),
            counts bombs in neighborhod for each bomb (via call to another method).
            In: sideSize of one side of grid, number of mines to be placed.
            Returns: Nothing
            Interaction with user: None.
            Modifications to data structures: ActualBoard is set up, mines
            placed, counts updated. PlayerBoard set to all H. GameState set to
            "playing"
            Errors handled: If SideSize <= 2, a value of 2 is used.
            If SideSize > 15, a value of 15 is used.
            If Mines is greater than the number of cells in the grid, every cell
            is given a bomb and any excess is ignored.
            If Mines < 0, a value of 0 is used.
       '''
       if sideSize <= 2:
            sideSize = 2
       elif sideSize > 15:
            sideSize = 15

       if mines < 0:
            mines = 0
       elif mines > sideSize**2:
            mines = sideSize**2

       self.size = sideSize
       self.mines = mines
       self.gameState = 'playing'

       self.actualBoard = self.createBoard(0)
       self.playerBoard = self.createBoard('H')
       self.placeMines()

    def createBoard(self, value):
       ''' Creates empty board
            In: value - default value for filling board
            Returns: new board represented by list of lists
            Interaction with user: None
            Modifications to data structures: Reads self.size to set apropriate
            size for board
            Errors handled: None
       '''
       board = []
       for i in range(self.size):
            board.append([value] * self.size)
       return board
   
    def placeMines(self):
       ''' Puts mines onto ActualBoard.
            In: None.
            Returns: None.
            Interaction with user: None.
            Modifications to data structures: Reads self.size and self.mines to
            handle setup. Randomly selects location for self.mines mines to be
            placed. Calls self.getCounts() to finish setting up ActualBoard.
            Errors handled: None (input was screened in __init__()). Note that
            this method must make sure that each mine is placed in a separate
            cell; that is, if a mine is about to be placed in a cell that
            already has a mine in it, another cell should be picked instead.
       '''
       mineLocations = [''] * (self.size**2 - self.mines)
       mineLocations += ['M'] * self.mines
       random.shuffle(mineLocations)
       for i, m in enumerate(mineLocations):
            if m == 'M':
                r = i // self.size
                c = i % self.size
                self.actualBoard[r][c] = 'M'
       self.getCounts()...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Get help from a qualified tutor
Live Chats