Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Cyber Battle Game
Game elements
5. Step 3 – board and robot visualization
Main Game Loop
Final game

Instruction

Okay! Now that we have a function which returns the game board and we have a proper robot representation, we need a function which will print (visualize) the board after each game round. We want the function to print to the console output. It should look something like this:

========================
- - - - 
- A>- - 
- - - - 
- - B<- 

First, we introduce a horizontal line that visually separates the different rounds. (Each round will print out its own board.) After the line, we print our 4 x 4 board. Note that each field is visualized with two characters: empty fields have hyphens followed by spaces, and the occupied fields contain robot names and the direction that robot faces.

Exercise

Create a function named print_board(board, robot_a, robot_b). The function accepts a board in the format we specified earlier and two robot dictionaries.

Start by printing a separating line (there are 24 equality signs in the line):

========================

Next, for each cell in the board, print two characters as outlined below.

  1. If the cell is an empty field ('-'), print a hyphen followed by a space ('- ').
  2. For a cell with either 'A' or 'B', print the letter followed by that robot's direction.

Invoke the print_board() function with the initial board and robot configuration.

Stuck? Here's a hint!

You will need a nested for loop:

for row in board:
  line = ''
  for cell in row:
    if cell == ...

To get a robot's direction, use the input argument robot_a or robot_b. It is a dictionary which contains a field named direction:

robot_a['direction']