Advertisement
Guest User

Untitled

a guest
May 27th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1.  
  2.  
  3.  
  4. public class GameBoardAndMovement {
  5.  
  6.  
  7. private Robot robotOne;
  8. private Robot robotTwo;
  9.  
  10.  
  11.  
  12.  
  13. public static void main(String[] args){
  14. Out.println("What size of board you want to play on!");
  15. Out.println("For 4x4 write s.For 16x16 write b.");
  16. char size=In.readChar();
  17.  
  18. switch(size){
  19. case 's':
  20. GameBoardAndRobot(4);
  21. break;
  22. case 'b':
  23. GameBoardAndRobot(16);
  24. default:
  25. Out.print("Choose either Small or Big.Rest wont be accepted1");
  26. break;
  27.  
  28. }
  29.  
  30. startGame();
  31. }
  32.  
  33. public void startGame(){
  34. while(!robotOne.canSee(robotTwo))
  35. {
  36. char nextMove = Grid.readKey();
  37.  
  38. MovementOfRobots(robotOne, nextMove);
  39. MovementOfRobots(robotTwo, opositeMovementOfRobots(nextMove));
  40. }
  41.  
  42. Grid.showMessage("Nicely Done! :) ");
  43. }
  44. public void GameBoardAndRobot(int gridSize){
  45.  
  46. Grid.create(gridSize,gridSize);
  47.  
  48. robotOne=new Robot(0, 0);
  49. robotTwo=new Robot(gridSize-1,gridSize-1);
  50.  
  51. Grid.placeRobot(robotOne.curRow,robotOne.curColumn);
  52. Grid.placeRobot(robotTwo.curRow,robotTwo.curColumn);
  53.  
  54.  
  55.  
  56.  
  57. }
  58. //MovementOptionts
  59. public void goLeft(Robot robot){
  60. Grid.removeRobot(robot.curRow,robot.curColumn);
  61. robot.curColumn--;
  62. Grid.placeRobot(robot.curRow,robot.curColumn);
  63. }
  64. public static void goUp(Robot robot){
  65. Grid.removeRobot(robot.curRow,robot.curColumn);
  66. robot.curRow--;
  67. Grid.placeRobot(robot.curRow,robot.curColumn);
  68. }
  69. public void goDown(Robot robot){
  70. Grid.removeRobot(robot.curRow,robot.curColumn);
  71. robot.curRow++;
  72. Grid.placeRobot(robot.curRow,robot.curColumn);
  73. }
  74. public void goRight(Robot robot){
  75. Grid.removeRobot(robot.curRow,robot.curColumn);
  76. robot.curColumn++;
  77. Grid.placeRobot(robot.curRow,robot.curColumn);
  78. }
  79.  
  80. //
  81.  
  82. public void MovementOfRobots(Robot robot,char nextMove){
  83. switch(nextMove){
  84. case 'a':
  85. if(Grid.insideBounds(robot.curRow, robot.curColumn-1)&&!Grid.robotAt(robot.curRow, robot.curColumn-1)){
  86. goLeft(robot);
  87.  
  88. }
  89. break;
  90. case 'w':
  91. if(Grid.insideBounds(robot.curRow-1, robot.curColumn)&&!Grid.robotAt(robot.curRow-1, robot.curColumn)){
  92. goUp(robot);
  93. }
  94. break;
  95. case 's':
  96. if(Grid.insideBounds(robot.curRow+1, robot.curColumn)&&!Grid.robotAt(robot.curRow+1, robot.curColumn)){
  97. goDown(robot);
  98. }
  99. break;
  100. case 'd':
  101. if(Grid.insideBounds(robot.curRow, robot.curColumn+1)&&!Grid.robotAt(robot.curRow, robot.curColumn+1)){
  102. goRight(robot);
  103. }
  104. break;
  105. default:
  106. break;
  107. }
  108.  
  109. }//End of MovementOfRobots
  110.  
  111. public char opositeMovementOfRobots(char nextMove){
  112. switch(nextMove){
  113. case 'a':
  114. return 'd';
  115. case 'w':
  116. return 's';
  117. case 's':
  118. return 'w';
  119. case 'd':
  120. return 'a';
  121. default:
  122. return 'E';
  123. }
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement