Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Cyber Battle Game
Game elements
Main Game Loop
Robot moves
Final game

Instruction

Great! Implementing the right turn was a piece of cake, so now let's try something slightly more complicated: moving forward.

Remember that a robot can only move forward in the direction it is facing. If the new field is occupied by the opponent or is outside the game board, nothing should happen.

To make things easier, we'll create four helper functions before creating the actual move_forward() function. These functions will do the following:

  1. Get the robot's current position.
  2. Get the robot's new position.
  3. Check if the new position is empty.
  4. Check if the new position is within the board's borders.

Exercise

We'll start with the first function. Write a get_position(board, robot) function which accepts a board and a robot and returns a tuple with two values: the row and the column of the robot's current position. The rows and columns are numbered from 0 to 3.

Stuck? Here's a hint!

You will need a nested for loop iterating over the indexes:

for i, row in enumerate(board):
  for j, cell in enumerate(row):

If the given cell contains the robot's name (cell == robot['name']), return a tuple with (i, j).