Kickstart 2020 with new opportunities! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction to nested lists
Iterating over nested lists
Modifying nested lists
11. Nested lists as game board representations – Part 1
Working with multiple lists
Summary

Instruction

Good job! Nested lists are also sometimes used to represent game boards. In the example below, we'll have a 4 x 5 board with either empty fields or fields with obstacles. The following board...

game_board

... can be represented in Python in the following way:

game_board = [
  ['-', '-', 'x', '-', '-'],
  ['-', '-', '-', '-', '-'],
  ['x', '-', '-', '-', '-'],
  ['-', '-', '-', 'x', '-']
]

A hyphen '-' represents an empty field and an 'x' represents a field with an obstacle.

Now, let's say we want to remove all the obstacles from the board. We can use the following code:

for i in range(len(game_board)):
  for j in range(len(game_board[i])):
    if game_board[i][j] == 'x':
      game_board[i][j] = '-'

The code above checks every field in the board. If the field contains an obstacle (an 'x'), it turns it into an empty field ('-').

Exercise

Create a function named is_board_correct(input_board) that accepts a game board in the format shown in the template code. The function should count the number of obstacles on the board (represented by 'x') and return True if there are exactly three obstacles. If there is any other number of obstacles, the function should return False.
You can use the example game boards in the template to test your code.

Stuck? Here's a hint!

Create a temporary obstacle_count variable before your nested for loops.