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
6. Step 4 – adding random robot moves
Final game

Instruction

Good job! Now we'll write a code which will pick a random move for a robot.

In each round, a robot can do one of the following:

  1. Turn right
  2. Move forward
  3. Shoot a laser beam

We'll implement those moves later on, but we can write code to simulate them now.

Exercise

  1. Create a function named act_robot(robot). The function should first pick a random number from 0 to 2 (see the Hint if you don't know how to generate a random number in Python). Based on the number picked, the function will make the robot turn right, shoot a laser beam, or move forward. We don't have these methods implemented yet, so for now we'll just print simple statements to the output:
    1. For the random number 0, print the entire robot argument and add the word "right" after a space, e.g.
      {'name': 'B', 'direction': '<'} right
    2. If the number is 1, print robot and "laser shoot" after a space.
    3. If the number is 2, print robot and "move forward" after a space.
  2. Create the play_round(robot_a, robot_b) function. Inside the function, invoke the act_robot(robot_a) function and the act_robot(robot_b) function (in that order).

Stuck? Here's a hint!

To pick a random number from 0 to 2, use the following invocation:

action = random.randint(0, 2)