Skip to main content
Engineering LibreTexts

8.27: Adding Pieces to the Board Data Structure

  • Page ID
    14603
  • def addToBoard(board, piece):
        # fill in the board based on piece's location, shape, and rotation
        for x in range(TEMPLATEWIDTH):
            for y in range(TEMPLATEHEIGHT):
                if PIECES[piece['shape']][piece['rotation']][y][x] != BLANK:
                    board[x + piece['x']][y + piece['y']] = piece['color']
    

    The board data structure is a data representation for the rectangular space where pieces that have previously landed are tracked. The currently falling piece is not marked on the board data structure. What the addToBoard() function does is takes a piece data structure and adds its boxes to the board data structure. This happens after a piece has landed.

    The nested for loops on lines 3 [376] and 4 [377] go through every space in the piece data structure, and if it finds a box in the space (line 5 [378]), it adds it to the board (line 6 [379]).