Skip to main content
Engineering LibreTexts

8.28: Creating a New Board Data Structure

  • Page ID
  • def getBlankBoard():
        # create and return a new blank board data structure
        board = []
        for i in range(BOARDWIDTH):
            board.append([BLANK] * BOARDHEIGHT)
        return board

    The data structure used for the board is fairly simple: it’s a list of lists of values. If the value is the same as the value in BLANK, then it is an empty space. If the value is an integer, then it represents a box that is the color that the integer indexes in the COLORS constant list. That is, 0 is blue, 1 is green, 2 is red, and 3 is yellow.

    In order to create a blank board, list replication is used to create the lists of BLANK values which represents a column. This is done on line 5 [386]. One of these lists is created for each of the columns in the board (this is what the for loop on line 4 [385] does).