Guest User

Untitled

a guest
Feb 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package com.practical.demo.Project;
  2.  
  3. public class WalkingRobotWithSwitch {
  4.  
  5. private int x;
  6. private int y;
  7.  
  8. public static void main(String[] args) {
  9.  
  10. WalkingRobotWithSwitch robot = new WalkingRobotWithSwitch();
  11.  
  12. System.out.println(robot.Move(5, 2, "SOUTH", "RW2LW4R"));
  13.  
  14. }
  15.  
  16. public String Move(int inputX, int inputY, String direction, String walkString) {
  17.  
  18. this.x = inputX;
  19. this.y = inputY;
  20. this.faceDirection = direction;
  21.  
  22. int steps = 0;
  23.  
  24. for (int i = 0; i < walkString.length(); i++) {
  25.  
  26. String walkSide = String.valueOf(walkString.charAt(i++));
  27.  
  28. if (i < walkString.length()) {
  29.  
  30. walkString.charAt(i++);
  31.  
  32. steps = Integer.parseInt(String.valueOf(walkString.charAt(i)));
  33.  
  34. doWalk(steps, walkSide);
  35.  
  36. } else {
  37. doWalk( 0, walkSide);
  38. }
  39. }
  40. return new StringBuilder("(").append(this.x).append(", ").append(this.y).append(") ,").append(this.faceDirection).toString();
  41. }
  42.  
  43. private String faceDirection;
  44.  
  45.  
  46. public void doWalk(int steps, String walkDirection) {
  47. switch (faceDirection) {
  48.  
  49. case "SOUTH":
  50. if (walkDirection.equalsIgnoreCase("R")) {
  51. x = x - steps;
  52. faceDirection = "WEST";
  53. } else {
  54. x = x + steps;
  55. faceDirection = "EAST";
  56. }
  57.  
  58. break;
  59. case "NORTH":
  60. if (walkDirection.equalsIgnoreCase("R")) {
  61. x = x + steps;
  62. faceDirection = "EAST";
  63. } else {
  64. x = x - steps;
  65. faceDirection = "WEST";
  66. }
  67.  
  68. break;
  69. case "EAST":
  70. if (walkDirection.equalsIgnoreCase("R")) {
  71. y = y - steps;
  72. faceDirection = "SOUTH";
  73. } else {
  74. y = y + steps;
  75. faceDirection = "NORTH";
  76. }
  77.  
  78. break;
  79. case "WEST":
  80. if (walkDirection.equalsIgnoreCase("R")) {
  81. y = y + steps;
  82. faceDirection = "NORTH";
  83. } else {
  84. y = y - steps;
  85. faceDirection = "SOUTH";
  86. }
  87. break;
  88. }
  89.  
  90. }
  91.  
  92. }
Add Comment
Please, Sign In to add comment