A Virtual Robot Example

From Knowledge Kitchen
Jump to navigation Jump to search


A virtual robot that wanders around the city. Written in Python.

A few things to note:

  • this example is essentially an extension of the virtual cat example
  • internal state: the robot has an internal sense of where it is in New York City by keeping track of the current street and avenue
  • randomness: the robot can wander randomly around the city by generating random numbers and using them to determine direction
  • boolean logic: the robot performs bounds-checking on its street and avenue positions to make sure it doesn't wander off the map
  • boolean logic: the robot can identify when it has at New York University (NYU) by check its street and avenue positions against the known NYU coordinates


The Robot class

Save this code into a file named robot.py. You will not execute (i.e. "run") this code. Rather, this code will be used by other test scripts which will be executed.

"""This file contains the plan for a virtual robot that wanders around New York City"""

#load the random-related functions
import random

#load the time-related functions
import time

class Robot:
    """The template from which we construct robots"""

    def __init__(self, name="John", year=2016):
        """Initial setup of a robot object"""
        self.name = name
        self.year_created = year
        self.street = 4
        self.ave = 4
        self.beep()

    def beep(self):
        """Tell the robot to beep"""
        print("Beep!")

    def go_up(self):
        """Tell the robot to go one block North in its virtual world of New York City"""
        if self.street < 130:
            self.street = self.street + 1
            self.report_location()
        else:
            print("Sorry, {}, it is not okay for you to go any further north than 130th St.".format(self.name))


    def go_down(self):
        """Tell the robot to go one block South in its virtual world of New York City"""
        if self.street > 1:                
            self.street = self.street - 1
            self.report_location()
        else:
            print("Sorry... {} is not allowed to go any further downtown than 1st St.".format(self.name))


    def go_left(self):
        """Tell the robot to go one block West in its virtual world of New York City"""
        if self.ave < 11:
            self.ave = self.ave + 1
            self.report_location()
        else:
            print("{} reached the Hudson River and stopped to admire the view.".format(self.name))


    def go_right(self):
        """Tell the robot to go one block East in its virtual world of New York City"""
        if self.ave > 1:
            self.ave = self.ave - 1
            self.report_location()
        else:
            print("Sorry... {} reached East River and tried to jump in... we stopped him.".format(self.name))


    def report_location(self):
        """Ask the robot to tell us where it is currently"""
        msg = """
              {} is currently at
              {}th St. and {}th Ave.
              """.format(self.name,
                         self.street,
                         self.ave)
        print(msg)

    def pick_a_direction(self):
        """Allow the robot to randomly pick a direction and move in it"""
        #generate a random number from 1-4
        num = random.randint(1,4)
        if num == 1:
            self.go_up()
        elif num == 2:
            self.go_right()
        elif num == 3:
            self.go_down()
        elif num == 4:
            self.go_left()

    def check_if_at_nyu(self):
        """Check whether the robot is currently at the address of New York University"""
        if self.street == 4 and self.ave == 4:
            print("Hey, {} is back at NYU!".format(self.name))

Test programs

This scripts you can use to "test" the Robot class by creating some robot objects and instructing them to do various tasks. These are the scripts you will actually want to execute.

Make a mortal robot

Save this code into a file named make_one_short_lived_robot.py:

"""This file makes a robot object and manually moves it a few blocks around the city"""

#import the robot class
from robot import *

#instantiating a robot objects
robot1 = Robot("Charlie", 1992)
robot1.beep() #tell the robot to beep

#manually drive the robot in a loop, around and back to its origin
robot1.go_up() 
robot1.go_up() 
robot1.check_if_at_nyu() #the robot is not at NYU, so this does nothing
robot1.go_right()
robot1.go_right()
robot1.check_if_at_nyu() #the robot is not at NYU, so this does nothing
robot1.go_down() 
robot1.go_down() 
robot1.check_if_at_nyu() #the robot is not at NYU, so this does nothing
robot1.go_left() 
robot1.go_left() 
robot1.check_if_at_nyu() #the robot is now back at NYU, so it will say so!

#done

Make an immortal robot

Save this code into a file named make_one_immortal_robot.py:

"""This file makes a robot object and forces it to wander the city for eternity"""

#import the robot class
from robot import *

#instantiating a robot objects
robot1 = Robot() #generic robot
robot1.beep() #tell the robot to beep

#make the robot move around until the end of time
while 4 == 4:
    #have the robot automatically pick a direction and go in it
    robot1.pick_a_direction()

    #check whether the robot is at NYU or not
    robot1.check_if_at_nyu()

    #pause for 1 second
    time.sleep(1)

What links here