Robot
Last updated
Last updated
IMPORTANT: Main Class is provided for you(See the bottom of this page). You don't have to modify Main Class.
The PR2004 is a robot that automatically gathers toys and other items scattered in a tiled hallway. A tiled hallway has a wall at each end and consists of a single row of tiles, each with some number of items to be gathered.The PR2004 robot is initialized with a starting position and an array that contains the number of items on each tile. Initially the robot is facing right, meaning that it is facing toward higher-numbered tiles.The PR2004 robot makes a sequence of moves until there are no items remaining on any tile. A move is defined as follows.
If there are any items on the current tile, then one item is removed.
If there are more items on the current tile, then the robot remains on the current tile facing the same direction.
If there are no more items on the current tile
if the robot can move forward, it advances to the next tile in the direction that it is facing;
otherwise, if the robot cannot move forward, it reverses direction and does not change position.
In the following example, the position and direction of the robot are indicated by "<" or ">" and the entries in the diagram indicate the number of items to be gathered on each tile. There are four tiles in this hallway. The starting state of the robot is illustrated in the following diagram.
After nine moves, the robot stops because the hall is clear.The PR2004 is modeled by the class Robot as shown in the following declaration. You can directly copy and paste the code in your editor.
This class can be implemented by completing 3 methods in order.
IMPORTANT: Please follow the instructions below IN ORDER. It would be much easier for you.
a)
First, write the Robot method forwardMoveBlocked()
.
Method forwardMoveBlocked()
returns true if the robot has a wall immediately in front of it, so that it cannot move forward. Otherwise, forwardMoveBlocked returns false.Complete method forwardMoveBlocked() first in Robot Class.
b)
Second, write the Robot method move()
.
Method move has the robot carry out one move as specified at the beginning of the question. The specification for a move is repeated here for your convenience.
If there are any items on the current tile, then one item is removed.
If there are more items on the current tile, then the robot remains on the current tile facing the same direction.
If there are no more items on the current tile
if the robot can move forward, it advances to the next tile in the direction that it is facing
otherwise, if the robot cannot move forward, it reverses direction and does not change position.
You have to call forwardMoveBlocked() to receive full credit.
c)
Last, write the Robot method clearHall()
. Method clearHall()
clears the hallway, repeatedly having this robot make a move until the hallway has no items, and returns the number of moves made.
In the example at the beginning of this problem, clearHall()
would take the robot through the moves shown and return 9, leaving the robot in the state shown in the final diagram.
You have to call hallIsClear() and move() to receive full credit.
You will simulate the Robot move in main()
, based on the diagram above. For your convenience, the Main
class is provided and you don't need to change anything.
The result prints the hall after each move. After the hall is clear, it gives the steps of the robot. The result should be like:
Dai Weide
Dong Ruoxuan
Lin Jiaxin
Wang Jiaxi
Zhang Wenyuan
Zhang Zhourui
Zhu Qixin
Luo Rui
Zheng Hao
Liu Xintong
Zhang Tianyu
Hu Yuqin
Jin Xuanyu
Shen Xinyu
Zhou Jingyi
Zhu Haiqiao
Zhu Yingge
Tu Mai