Skip to main content
Engineering LibreTexts

4.41: Telling if the Player Has Won

  • Page ID
  • def hasWon(revealedBoxes):
        # Returns True if all the boxes have been revealed, otherwise False
        for i in revealedBoxes:
            if False in i:
                return False # return False if any boxes are covered.
        return True

    The player has won the game when all of the icon pairs have been matched. Since the "revealed" data structure gets values in it set to True as icons have been matched, we can simply loop through every space in revealedBoxes looking for a False value. If even one False value is in revealedBoxes, then we know there are still unmatched icons on the board.

    Note that because revealedBoxes is a list of lists, the for loop on line 3 [285] will set the inner list as the values of i. But we can use the in operator to search for a False value in the entire inner list. This way we don’t need to write an additional line of code and have two nested for loops like this:

    for x in revealedBoxes:
        for y in revealedBoxes[x]:
            if False == revealedBoxes[x][y]:
                return False