Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package simulator; public class Bus { public final int number; private final Roa

ID: 3883908 • Letter: P

Question

package simulator;

public class Bus {

   public final int number;

   private final RoadMap roadMap;

   private int x;

   private int y;

   public Bus(int number, RoadMap roadMap, int x, int y) {

       this.number = number;

       this.roadMap = roadMap;

       this.x = x;

       this.y = y;

   }

   public int getX() {

       return x;

   }

   public int getY() {

       return y;

   }

   /**

   * Move the bus. Buses only move along the cardinal directions

   * (north/south/east/west), not diagonally.

   *

   * If the bus is stopped (that is, if it was just placed, or if it didn't

   * move last time move() was called), then it should attempt to move north.

   * If it cannot (no road, or off the map), then it should attempt south,

   * then east, then west. If no move is available, it should stay in its

   * current position.

   *

   * If the bus is moving (that is, if it successfully moved the last time

   * move() was called), then it should attempt to continue moving in the same

   * direction.

   *

   * If it cannot (no road, or off the map), then it should attempt to turn

   * right. For example, if the bus was moving north, but there is no more

   * road to the north, it should move east if possible.

   *

   * If it cannot turn right, it should turn left. If it cannot turn left, it

   * should reverse direction (that is, move backward, if possible).

   * If it cannot do any of these things, it should stay in its current position.

   * @param x

   * @param y

   */

   public void move() {

  

       if(roadMap.isRoad(x,y-1)){ //north

           y=y-1;

       }

       else if(roadMap.isRoad(x,y+1)){ //south

           y=y+1;

       }

       else if(roadMap.isRoad(x+1,y)){ //east

           x=x+1;

       }

       else if(roadMap.isRoad(x-1, y)){ //west

       x=x-1;

       }

       else {

           //stay//

       }

   }

}

please fix the move() method

thank you so much

Explanation / Answer

//Bus.java

package simulator;

public class Bus {

public final int number;

private final RoadMap roadMap;

private int x;

private int y;

private int store;

private boolean moved;

public Bus(int number, RoadMap roadMap, int x, int y) {

this.number = number;

this.roadMap = roadMap;

this.x = x;

this.y = y;

this.store = -1;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

public void moveNorth(){

y-=1;

store = 1;

}

public void moveSouth(){

y+=1;

store = 2;

}

public void moveWest(){

x-=1;

store = 3;

}

public void moveEast(){

x+=1;

store = 4;

}

public int moveInSameDirection(){

if(store == 1 && roadMap.isRoad(x, y-1)){

moveNorth();

}

else if(store ==2 && roadMap.isRoad(x, y+1)){

moveSouth();

}

else if(store ==3 && roadMap.isRoad(x-1, y)){

moveWest();

}

else if(store ==4 && roadMap.isRoad(x+1, y)){

moveEast();

}

else {

return 0;

}

return 1;

}

public void move() {

int curX = this.x;

int curY = this.y;

if(store == -1) {

if(roadMap.isRoad(curX, curY-1)){

moveNorth();

}

else if(roadMap.isRoad(curX, curY+1)){

moveSouth();

}

else if(roadMap.isRoad(curX+1, curY)){

moveWest();

}

else if(roadMap.isRoad(curX-1, curY)){

moveEast();

}

}

else if(store != -1){

if(moveInSameDirection() == 0){

if(store == 1){

if(roadMap.isRoad(curX+1, curY)){

moveEast();

}

else if(roadMap.isRoad(curX-1, curY)){

moveWest();

}

else if(roadMap.isRoad(curX, curY+1)){

moveSouth();

}

else{

store = -1;

}

}

else if(store == 2){

if(roadMap.isRoad(curX-1, curY)){

moveWest();

}

else if(roadMap.isRoad(curX+1, curY)){

moveEast();

}

else if(roadMap.isRoad(curX, curY+1)){

moveNorth();

}

else{

store = -1;

}

}

}

else if(store == 4){

if(roadMap.isRoad(curX, curY-1)){

moveNorth();

}

else if(roadMap.isRoad(curX, curY+1)){

moveSouth();

}

else if(roadMap.isRoad(curX+1, curY)){

moveEast();

}

else{

store = -1;

}

}

else if(store == 3){

if(roadMap.isRoad(curX, curY+1)){

moveSouth();

}

else if(roadMap.isRoad(curX, curY-1)){

moveNorth();

}

else if(roadMap.isRoad(curX-1, curY)){

moveWest();

}

else{

store = -1;

}

}

else if(store!=1 && store!=2 && store!=3 && store!=4)

store=-1;

}

}

}

=====================================================================================

//BusVisualizer.java

package simulator;

import processing.core.PApplet;

public class BusVisualizer extends PApplet {

static RoadMap roadMap;

static Bus[] buses;

static int busCount;

public static void main(String[] args) {

PApplet.main("simulator.BusVisualizer");

}

public void settings() {

roadMap = new RoadMap(10, 10); // you could replace this with a call to

// RoadMap.fromString(); see the tests for an example.

buses = new Bus[5];

busCount = 0;

size(roadMap.xSize * 20, roadMap.ySize * 20);

}

public void setup() {

}

public void draw() {

background(223);

// first draw the road/non-road

stroke(255);

for (int y = 0; y < roadMap.ySize; y++) {

for (int x = 0; x < roadMap.xSize; x++) {

if (roadMap.isRoad(x, y)) {

fill(31);

}

else {

fill(223);

}

rect(x*20,y*20,20,20);

}

}

// now draw the buses (numbers)

fill(255);

stroke(255);

for (int i = 0; i < busCount; i++) {

Bus b = buses[i];

text(Integer.toString(b.number), b.getX()*20, b.getY()*20, 20, 20);

}

}

public void keyPressed() {

if (key == ' ') {

for (int i = 0; i < busCount; i++) {

buses[i].move();

}

}

else if (key == BACKSPACE || key == DELETE) {

if (busCount > 0) {

busCount--;

buses[busCount] = null;

}

}

}

public void mousePressed() {

int x = mouseX / 20;

int y = mouseY / 20;

if (mouseButton == LEFT) {

boolean isClear = true;

for (int i = 0; i < busCount; i++) {

Bus b = buses[i];

if (b.getX() == x && b.getY() == y) {

isClear = false;

break;

}

}

if (isClear) {

roadMap.setRoad(x, y, !roadMap.isRoad(x, y));

}

}

if (mouseButton == RIGHT) {

// is there space for a new bus and is the cell a road?

if ((busCount < buses.length) && (roadMap.isRoad(x, y))) {

// and, is it clear (no bus)?

boolean isClear = true;

for (int i = 0; i < busCount; i++) {

Bus b = buses[i];

if (b.getX() == x && b.getY() == y) {

isClear = false;

break;

}

}

if (isClear) {

buses[busCount] = new Bus(busCount, roadMap, x, y);

busCount++;

}

}

}

}

}

=================================================================================

// RoadMap.java

package simulator;

public class RoadMap {

public final int xSize;

public final int ySize;

private final boolean[][] isRoad;

public RoadMap(int x, int y) {

isRoad = new boolean[x][y];

xSize = x;

ySize = y;

}

/**

* Use this method to create a RoadMap from a String. Use 'X' for non-road and

* '.' for road.

*

* For example, the string

*

* "XXX

* X..

* X.X"

*

* (including whitespace!) creates a 3x3 map, where there is a short road

* that travels north from the center of the bottom of the map, and then

* turns east to the center of the right-hand side of the map.

*

* @param s a string representing a map of roads

* @return a new RoadMap instance

*/

public static RoadMap fromString(String s) {

String[] lines = s.trim().split("\s+");

final int xSize = lines[0].length();

final int ySize = lines.length;

RoadMap roadMap = new RoadMap(xSize, ySize);

for (int y = 0; y < ySize; y++) {

for (int x = 0; x < xSize; x++) {

if (lines[y].charAt(x) == '.') {

roadMap.isRoad[x][y] = true;

}

}

}

return roadMap;

}

public String toString() {

StringBuilder sb = new StringBuilder();

for (int y = 0; y < ySize; y++) {

for (int x = 0; x < xSize; x++) {

if (isRoad[x][y]) {

sb.append('.');

} else {

sb.append('X');

}

}

sb.append(' ');

}

return sb.substring(0, sb.length() - 1);

}

public boolean isRoad(int x, int y) {

if(x<0||x>xSize-1||y<0||y>ySize-1){

return false;

}

return isRoad[x][y];

}

public void setRoad(int x, int y, boolean isRoad) {

this.isRoad[x][y] = isRoad;

}

}

==================================================================================