Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Bus {
- public final int number;
- private final RoadMap roadMap;
- private int x;
- private int y;
- private boolean stopped;
- private int direction;
- // 1 = (North), 2 = (East), 3 =
- public Bus(int number, RoadMap roadMap, int x, int y) {
- this.number = number;
- this.roadMap = roadMap;
- this.x = x;
- this.y = y;
- this.stopped = true;
- this.direction = 0;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public boolean isNorthOpen() {
- if((Roadmap.isRoad[x][y])&&(y-1<ySize)) {
- return true ;
- }
- else return false ;
- }
- public void moveNorth(){
- y = y-1 ;
- }
- public boolean isSouthOpen(){
- if((RoadMap.isRoad[x][y]==true) &&(y+1)<ySize){
- return true ;
- }
- else return false ;
- }
- public void moveSouth(){
- y=y+1 ;
- }
- public boolean isEastOpen(){
- if((Roadmap.isRoad[x][y])&&(x+1)<xSize){
- return true ;
- }
- else return false ;
- }
- public void moveEast(){
- x = x+1 ;
- }
- public boolean isWestOpen(){
- if((Roadmap.isRoad[x][y]) &&(x-1)<xSize){
- return true ;
- }
- else return false ;
- }
- public void moveWest(){
- x = x-1 ;
- }
- /**
- * 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.
- */
- public void move() {
- if(stopped==true){
- if(isNorthOpen()==true){
- moveNorth() ;
- }
- else if(isSouthOpen()==true){
- moveSouth() ;
- }
- else if (isEastOpen()==true){
- moveEast() ;
- }
- else if(isWestOpen()==true){
- moveWest() ;
- }
- }
- if(stopped==false){
- if(direction==1){
- if(isNorthOpen()==true){
- moveNorth() ;
- }
- else if(isEastOpen()==true){
- moveEast() ;
- }
- else if(isWestOpen()==true){
- moveWest() ;
- }
- else if(isSouthOpen()==true){
- moveSouth() ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement