Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. public class Bus {
  2. public final int number;
  3. private final RoadMap roadMap;
  4. private int x;
  5. private int y;
  6. private boolean stopped;
  7. private int direction;
  8. // 1 = (North), 2 = (East), 3 =
  9.  
  10.  
  11. public Bus(int number, RoadMap roadMap, int x, int y) {
  12. this.number = number;
  13. this.roadMap = roadMap;
  14. this.x = x;
  15. this.y = y;
  16. this.stopped = true;
  17. this.direction = 0;
  18. }
  19.  
  20. public int getX() {
  21. return x;
  22. }
  23.  
  24. public int getY() {
  25. return y;
  26. }
  27. public boolean isNorthOpen() {
  28. if((Roadmap.isRoad[x][y])&&(y-1<ySize)) {
  29. return true ;
  30. }
  31. else return false ;
  32. }
  33. public void moveNorth(){
  34. y = y-1 ;
  35. }
  36. public boolean isSouthOpen(){
  37. if((RoadMap.isRoad[x][y]==true) &&(y+1)<ySize){
  38. return true ;
  39. }
  40. else return false ;
  41. }
  42. public void moveSouth(){
  43. y=y+1 ;
  44. }
  45. public boolean isEastOpen(){
  46. if((Roadmap.isRoad[x][y])&&(x+1)<xSize){
  47. return true ;
  48. }
  49. else return false ;
  50. }
  51. public void moveEast(){
  52. x = x+1 ;
  53. }
  54. public boolean isWestOpen(){
  55. if((Roadmap.isRoad[x][y]) &&(x-1)<xSize){
  56. return true ;
  57. }
  58. else return false ;
  59. }
  60. public void moveWest(){
  61. x = x-1 ;
  62. }
  63.  
  64.  
  65.  
  66. /**
  67. * Move the bus. Buses only move along the cardinal directions
  68. * (north/south/east/west), not diagonally.
  69. *
  70. * If the bus is stopped (that is, if it was just placed, or if it didn't
  71. * move last time move() was called), then it should attempt to move north.
  72. * If it cannot (no road, or off the map), then it should attempt south,
  73. * then east, then west. If no move is available, it should stay in its
  74. * current position.
  75. *
  76. * If the bus is moving (that is, if it successfully moved the last time
  77. * move() was called), then it should attempt to continue moving in the same
  78. * direction.
  79. *
  80. * If it cannot (no road, or off the map), then it should attempt to turn
  81. * right. For example, if the bus was moving north, but there is no more
  82. * road to the north, it should move east if possible.
  83. *
  84. * If it cannot turn right, it should turn left. If it cannot turn left, it
  85. * should reverse direction (that is, move backward, if possible).
  86. * If it cannot do any of these things, it should stay in its current position.
  87. */
  88. public void move() {
  89. if(stopped==true){
  90. if(isNorthOpen()==true){
  91. moveNorth() ;
  92. }
  93. else if(isSouthOpen()==true){
  94. moveSouth() ;
  95. }
  96. else if (isEastOpen()==true){
  97. moveEast() ;
  98. }
  99. else if(isWestOpen()==true){
  100. moveWest() ;
  101. }
  102. }
  103.  
  104. if(stopped==false){
  105. if(direction==1){
  106. if(isNorthOpen()==true){
  107. moveNorth() ;
  108. }
  109.  
  110. else if(isEastOpen()==true){
  111. moveEast() ;
  112. }
  113. else if(isWestOpen()==true){
  114. moveWest() ;
  115. }
  116. else if(isSouthOpen()==true){
  117. moveSouth() ;
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement