Due Date: Monday March 15, at end of lab
Educational goals of this lab - verify that every student can
INSTRUCTIONS:
(75 points) Description of the Problem:
Imagine a bridge that is 10 "steps" wide. A robot is trying to cross the bridge. Unfortunately it has a defect which causes it not to be able to go straight forward but only steps to one side or the other.
The program's job is to ask the user how many steps they want to run the simulation, then display the outcome. The simulation ends either by finishing the number of steps the user requested, or by falling off the bridge on either side.
Sample: (does not fall off)
Random Robot Walk How many steps to take? 10 |1234567890| | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | | R| it did not fall off the bridge! Robot took 10 steps
Note that the robot starts at the middle of the bridge, that's why there are eleven R's printed. The position of the robot at the start is printed on the first line.
Another Sample: (falls off the right edge)
Random Robot Walk How many steps to take? 300 |1234567890| | R | | R | | R | | R | | R | | R | | R| | R | | R| Splash! Robot took 9 steps
The ninth step is not shown, that's what made it fall off the bridge.
Sample run: (falls off the left edge)
Random Robot Walk How many steps to take? 50 |1234567890| | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | | R | |R | | R | |R | Splash! Robot took 18 steps
(10 points) Problem #1: Complete a test plan.
Description | Input
(number of steps) | Expected behavior/output | |||
---|---|---|---|---|---|
Zero iterations | 0 | Starting position shown, did not fall off, Robot took 0 steps | |||
One iteration | 1 | _____A._____ | |||
More than one iteration, less than 5 | 4 | _____B._____ | |||
Five or more iterations | 10 | _____C._____ |
(12 points) Problem #2: Design
# Prolog including 3 P's and author's name # import _________________ library # print title # initialize the width of the bridge (set to 10 to start with, you can change it later) #and the counter for steps to zero #and the robot's location to center of bridge (calculate it!) # ask user ____________________ # print the header # while counter has not done that many steps and robot's location > 0 and ______________ # print the robot's location on bridge # get a random direction to take on next step (either -1 or 1) # add direction to robot's location # add one to _____________ # if didn't fall off bridge # print last step of robot, tell user # else # print __________ # report _______________________
(53 points) Problem #3: Complete the program.
def main(): for z in range(1, 10): print(" " * z, ":)") main()
A piece of code you can use
#if robot is in position x and the bridge is width positions wide print("|", " " * x , "R", " " * (width - x) , "|", sep="")