# Robot

**IMPORTANT: Main Class is provided for you(See the bottom of this page). You don't have to modify Main Class.**

<img src="https://141412694-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjlGwoKQuhiC7lJSobO5J%2Fuploads%2FB3JNyZXkxHlEMcuYMmZJ%2Fimage.png?alt=media&#x26;token=9ce767af-0353-4763-94f9-e1e840ed57ea" alt="" data-size="original">

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.

1. If there are any items on the current tile, then one item is removed.
2. If there are more items on the current tile, then the robot remains on the current tile facing the same direction.
3. If there are no more items on the current tile
   1. if the robot can move forward, it advances to the next tile in the direction that it is facing;
   2. 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.

<figure><img src="https://141412694-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjlGwoKQuhiC7lJSobO5J%2Fuploads%2FHcH3IInN7hl55LaUe82X%2Fimage.png?alt=media&#x26;token=d36ec01a-e64d-4d3d-9448-6a8bdb3e356f" alt=""><figcaption></figcaption></figure>



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.

```java
public class Robot {
    private int[] hall;
    private int pos;
    private boolean facingRight;
    
    
    public Robot(int[] hall, int pos, boolean facingRight) {
        this.hall = hall;
        this.pos = pos;
        this.facingRight = facingRight;
    }
    
    private boolean forwardMoveBlocked() {
        boolean result = false;
        //implement the method here.
        
        return result;
        
    }
    
    private void move() {
        //implement the mothod here.
    
     
        for(int i: hall) {         //after the move, print the current hall.
            System.out.print(i);
        }
        System.out.println(); 

    }
    
    public int clearHall() {
        int result = 0;
        
        //implemenet the method here.
        

        return result;
    }
    
    private boolean hallIsClear() {
        for(int i:hall) {
            if(i!=0) return false;
        }
        return true;
    }
    
}
```

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)**&#x20;

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)**&#x20;

Second, write the Robot method `move()`.&#x20;

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.

1. If there are any items on the current tile, then one item is removed.
2. If there are more items on the current tile, then the robot remains on the current tile facing the same direction.
3. If there are no more items on the current tile
   1. if the robot can move forward, it advances to the next tile in the direction that it is facing
   2. 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)**&#x20;

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.&#x20;

You have to call hallIsClear() and move() to receive full credit.

#### How to Test: <a href="#how-to-test" id="how-to-test"></a>

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.

```java
public class Main
{
    public static void main(String[] args) {
      int[] hall = {1,1,2,2}; 
		
      Robot r = new Robot(hall, 1, true); //simulate the diagram above
		
      System.out.println(r.clearHall()); //let's make the robot clear the hall!
    }
}
```

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:

```
1022
1012
1002
1001
1000
1000
1000
1000
0000
9
```



### FULL SCORE

* Dai Weide
* Dong Ruoxuan
* Lin Jiaxin
* Wang Jiaxi
* Zhang Wenyuan
* Zhang Zhourui
* Zhu Qixin

### ALMOST THERE

* Luo Rui
* Zheng Hao
* Liu Xintong
* Zhang Tianyu
* Hu Yuqin
* Jin Xuanyu
* Shen Xinyu
* Zhou Jingyi
* Zhu Haiqiao
* Zhu Yingge
* Tu Mai
