Validate movement of a randomly moving object












0














I am to simulate a robot moving around a 9x9 grid, the first robot starts at (0,0) and another starts at (9,9). The program is to generate random moves until they both land on the same spot.



The issue is that there is supposed to be a validation to make sure that the robot doesn't move off grid. So they should never land on coordinates like (-1,3) or (10,2). The program I wrote is below, I'm just trying to get the validation part to work but I'm not sure how to implement this problem using ONLY the methods I'm given... To clarify, I can only have the methods as written below but the methods themselves can be changed.



I thought about having the robot move first, land on an illegal coordinate but have the validateNextMove() return false and then just have it move in the reverse direction, but that's not what the assignment wants. The program needs to "look ahead and know".



So I'm thinking maybe I should have a method variable to store the next move and validate those coordinates, but I'm not sure how to do that without breaking the assignments rule of only having certain methods and variables.



Other more elegant solutions are welcome!



Thanks!



Robot.java



package assignment1;

public class Robot {

public static void main(String args) {
// TODO Auto-generated method stub

}

int x;
int y;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int LEFT_DOWN_CORNER = 3;
public static final int LEFT_UP_CORNER = 4;
public static final int RIGHT = 5;
public static final int RIGHT_DOWN_CORNER = 6;
public static final int RIGHT_UP_CORNER = 7;
public static final int UP = 8;


public Robot(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}


}


MovingRobot.java



package assignment1;

import java.util.ArrayList;

public class MovingRobot extends Robot {

public static void main(String args) {

MovingRobot r1 = new MovingRobot(0, 0);
MovingRobot r2 = new MovingRobot(9, 9);

r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
r1.move();
System.out.println(r1.x + " " + r1.y);
/*
* while (sameSlot(r1, r2) == false) { r1.move(); r2.move(); }
*/
}

ArrayList<Integer> moves;
int nextMove;

public boolean validateNextMove() {
if (x < 0 || x > 9 || y < 0 || y > 9) {
return false;
}

else
return true;

}

public MovingRobot(int x, int y) {
super(x, y);
}

public int generateNextMove() {
return (int) (Math.random() * 8 + 1);
}

public static boolean sameSlot(Robot r1, Robot r2) {
return (r1.x == r2.x && r1.y == r2.y);
}

public void move() {
switch (nextMove = generateNextMove()) {

case 1:
x = x - 1;
System.out.println(nextMove);
//LEFT
break;
case 2:
x = x - 1;
y = y + 1;
System.out.println(nextMove);
//LEFT DOWN
break;
case 3:
x = x - 1;
y = y - 1;
System.out.println(nextMove);
//LEFT UP
break;
case 4:
x = x + 1;

System.out.println(nextMove);
break;
//RIGHT
case 5:
x = x + 1;
y = y + 1;
System.out.println(nextMove);
break;
// Right down
case 6:
x = x + 1;
y = y - 1;
System.out.println(nextMove);
break;
// right up
case 7:

y = y -1;
System.out.println(nextMove);
break;
// up
case 8:
y = y + 1;
System.out.println(nextMove);
break;
//DOWN
}

}

}









share|improve this question







New contributor




Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    I am to simulate a robot moving around a 9x9 grid, the first robot starts at (0,0) and another starts at (9,9). The program is to generate random moves until they both land on the same spot.



    The issue is that there is supposed to be a validation to make sure that the robot doesn't move off grid. So they should never land on coordinates like (-1,3) or (10,2). The program I wrote is below, I'm just trying to get the validation part to work but I'm not sure how to implement this problem using ONLY the methods I'm given... To clarify, I can only have the methods as written below but the methods themselves can be changed.



    I thought about having the robot move first, land on an illegal coordinate but have the validateNextMove() return false and then just have it move in the reverse direction, but that's not what the assignment wants. The program needs to "look ahead and know".



    So I'm thinking maybe I should have a method variable to store the next move and validate those coordinates, but I'm not sure how to do that without breaking the assignments rule of only having certain methods and variables.



    Other more elegant solutions are welcome!



    Thanks!



    Robot.java



    package assignment1;

    public class Robot {

    public static void main(String args) {
    // TODO Auto-generated method stub

    }

    int x;
    int y;
    public static final int DOWN = 1;
    public static final int LEFT = 2;
    public static final int LEFT_DOWN_CORNER = 3;
    public static final int LEFT_UP_CORNER = 4;
    public static final int RIGHT = 5;
    public static final int RIGHT_DOWN_CORNER = 6;
    public static final int RIGHT_UP_CORNER = 7;
    public static final int UP = 8;


    public Robot(int x, int y) {
    this.x = x;
    this.y = y;
    }

    public int getX() {
    return x;
    }
    public void setX(int x) {
    this.x = x;
    }
    public int getY() {
    return y;
    }
    public void setY(int y) {
    this.y = y;
    }


    }


    MovingRobot.java



    package assignment1;

    import java.util.ArrayList;

    public class MovingRobot extends Robot {

    public static void main(String args) {

    MovingRobot r1 = new MovingRobot(0, 0);
    MovingRobot r2 = new MovingRobot(9, 9);

    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    r1.move();
    System.out.println(r1.x + " " + r1.y);
    /*
    * while (sameSlot(r1, r2) == false) { r1.move(); r2.move(); }
    */
    }

    ArrayList<Integer> moves;
    int nextMove;

    public boolean validateNextMove() {
    if (x < 0 || x > 9 || y < 0 || y > 9) {
    return false;
    }

    else
    return true;

    }

    public MovingRobot(int x, int y) {
    super(x, y);
    }

    public int generateNextMove() {
    return (int) (Math.random() * 8 + 1);
    }

    public static boolean sameSlot(Robot r1, Robot r2) {
    return (r1.x == r2.x && r1.y == r2.y);
    }

    public void move() {
    switch (nextMove = generateNextMove()) {

    case 1:
    x = x - 1;
    System.out.println(nextMove);
    //LEFT
    break;
    case 2:
    x = x - 1;
    y = y + 1;
    System.out.println(nextMove);
    //LEFT DOWN
    break;
    case 3:
    x = x - 1;
    y = y - 1;
    System.out.println(nextMove);
    //LEFT UP
    break;
    case 4:
    x = x + 1;

    System.out.println(nextMove);
    break;
    //RIGHT
    case 5:
    x = x + 1;
    y = y + 1;
    System.out.println(nextMove);
    break;
    // Right down
    case 6:
    x = x + 1;
    y = y - 1;
    System.out.println(nextMove);
    break;
    // right up
    case 7:

    y = y -1;
    System.out.println(nextMove);
    break;
    // up
    case 8:
    y = y + 1;
    System.out.println(nextMove);
    break;
    //DOWN
    }

    }

    }









    share|improve this question







    New contributor




    Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      I am to simulate a robot moving around a 9x9 grid, the first robot starts at (0,0) and another starts at (9,9). The program is to generate random moves until they both land on the same spot.



      The issue is that there is supposed to be a validation to make sure that the robot doesn't move off grid. So they should never land on coordinates like (-1,3) or (10,2). The program I wrote is below, I'm just trying to get the validation part to work but I'm not sure how to implement this problem using ONLY the methods I'm given... To clarify, I can only have the methods as written below but the methods themselves can be changed.



      I thought about having the robot move first, land on an illegal coordinate but have the validateNextMove() return false and then just have it move in the reverse direction, but that's not what the assignment wants. The program needs to "look ahead and know".



      So I'm thinking maybe I should have a method variable to store the next move and validate those coordinates, but I'm not sure how to do that without breaking the assignments rule of only having certain methods and variables.



      Other more elegant solutions are welcome!



      Thanks!



      Robot.java



      package assignment1;

      public class Robot {

      public static void main(String args) {
      // TODO Auto-generated method stub

      }

      int x;
      int y;
      public static final int DOWN = 1;
      public static final int LEFT = 2;
      public static final int LEFT_DOWN_CORNER = 3;
      public static final int LEFT_UP_CORNER = 4;
      public static final int RIGHT = 5;
      public static final int RIGHT_DOWN_CORNER = 6;
      public static final int RIGHT_UP_CORNER = 7;
      public static final int UP = 8;


      public Robot(int x, int y) {
      this.x = x;
      this.y = y;
      }

      public int getX() {
      return x;
      }
      public void setX(int x) {
      this.x = x;
      }
      public int getY() {
      return y;
      }
      public void setY(int y) {
      this.y = y;
      }


      }


      MovingRobot.java



      package assignment1;

      import java.util.ArrayList;

      public class MovingRobot extends Robot {

      public static void main(String args) {

      MovingRobot r1 = new MovingRobot(0, 0);
      MovingRobot r2 = new MovingRobot(9, 9);

      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      /*
      * while (sameSlot(r1, r2) == false) { r1.move(); r2.move(); }
      */
      }

      ArrayList<Integer> moves;
      int nextMove;

      public boolean validateNextMove() {
      if (x < 0 || x > 9 || y < 0 || y > 9) {
      return false;
      }

      else
      return true;

      }

      public MovingRobot(int x, int y) {
      super(x, y);
      }

      public int generateNextMove() {
      return (int) (Math.random() * 8 + 1);
      }

      public static boolean sameSlot(Robot r1, Robot r2) {
      return (r1.x == r2.x && r1.y == r2.y);
      }

      public void move() {
      switch (nextMove = generateNextMove()) {

      case 1:
      x = x - 1;
      System.out.println(nextMove);
      //LEFT
      break;
      case 2:
      x = x - 1;
      y = y + 1;
      System.out.println(nextMove);
      //LEFT DOWN
      break;
      case 3:
      x = x - 1;
      y = y - 1;
      System.out.println(nextMove);
      //LEFT UP
      break;
      case 4:
      x = x + 1;

      System.out.println(nextMove);
      break;
      //RIGHT
      case 5:
      x = x + 1;
      y = y + 1;
      System.out.println(nextMove);
      break;
      // Right down
      case 6:
      x = x + 1;
      y = y - 1;
      System.out.println(nextMove);
      break;
      // right up
      case 7:

      y = y -1;
      System.out.println(nextMove);
      break;
      // up
      case 8:
      y = y + 1;
      System.out.println(nextMove);
      break;
      //DOWN
      }

      }

      }









      share|improve this question







      New contributor




      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am to simulate a robot moving around a 9x9 grid, the first robot starts at (0,0) and another starts at (9,9). The program is to generate random moves until they both land on the same spot.



      The issue is that there is supposed to be a validation to make sure that the robot doesn't move off grid. So they should never land on coordinates like (-1,3) or (10,2). The program I wrote is below, I'm just trying to get the validation part to work but I'm not sure how to implement this problem using ONLY the methods I'm given... To clarify, I can only have the methods as written below but the methods themselves can be changed.



      I thought about having the robot move first, land on an illegal coordinate but have the validateNextMove() return false and then just have it move in the reverse direction, but that's not what the assignment wants. The program needs to "look ahead and know".



      So I'm thinking maybe I should have a method variable to store the next move and validate those coordinates, but I'm not sure how to do that without breaking the assignments rule of only having certain methods and variables.



      Other more elegant solutions are welcome!



      Thanks!



      Robot.java



      package assignment1;

      public class Robot {

      public static void main(String args) {
      // TODO Auto-generated method stub

      }

      int x;
      int y;
      public static final int DOWN = 1;
      public static final int LEFT = 2;
      public static final int LEFT_DOWN_CORNER = 3;
      public static final int LEFT_UP_CORNER = 4;
      public static final int RIGHT = 5;
      public static final int RIGHT_DOWN_CORNER = 6;
      public static final int RIGHT_UP_CORNER = 7;
      public static final int UP = 8;


      public Robot(int x, int y) {
      this.x = x;
      this.y = y;
      }

      public int getX() {
      return x;
      }
      public void setX(int x) {
      this.x = x;
      }
      public int getY() {
      return y;
      }
      public void setY(int y) {
      this.y = y;
      }


      }


      MovingRobot.java



      package assignment1;

      import java.util.ArrayList;

      public class MovingRobot extends Robot {

      public static void main(String args) {

      MovingRobot r1 = new MovingRobot(0, 0);
      MovingRobot r2 = new MovingRobot(9, 9);

      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      r1.move();
      System.out.println(r1.x + " " + r1.y);
      /*
      * while (sameSlot(r1, r2) == false) { r1.move(); r2.move(); }
      */
      }

      ArrayList<Integer> moves;
      int nextMove;

      public boolean validateNextMove() {
      if (x < 0 || x > 9 || y < 0 || y > 9) {
      return false;
      }

      else
      return true;

      }

      public MovingRobot(int x, int y) {
      super(x, y);
      }

      public int generateNextMove() {
      return (int) (Math.random() * 8 + 1);
      }

      public static boolean sameSlot(Robot r1, Robot r2) {
      return (r1.x == r2.x && r1.y == r2.y);
      }

      public void move() {
      switch (nextMove = generateNextMove()) {

      case 1:
      x = x - 1;
      System.out.println(nextMove);
      //LEFT
      break;
      case 2:
      x = x - 1;
      y = y + 1;
      System.out.println(nextMove);
      //LEFT DOWN
      break;
      case 3:
      x = x - 1;
      y = y - 1;
      System.out.println(nextMove);
      //LEFT UP
      break;
      case 4:
      x = x + 1;

      System.out.println(nextMove);
      break;
      //RIGHT
      case 5:
      x = x + 1;
      y = y + 1;
      System.out.println(nextMove);
      break;
      // Right down
      case 6:
      x = x + 1;
      y = y - 1;
      System.out.println(nextMove);
      break;
      // right up
      case 7:

      y = y -1;
      System.out.println(nextMove);
      break;
      // up
      case 8:
      y = y + 1;
      System.out.println(nextMove);
      break;
      //DOWN
      }

      }

      }






      java






      share|improve this question







      New contributor




      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 15 mins ago









      Billy Yin

      1




      1




      New contributor




      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Billy Yin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Billy Yin is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210718%2fvalidate-movement-of-a-randomly-moving-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Billy Yin is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Billy Yin is a new contributor. Be nice, and check out our Code of Conduct.













          Billy Yin is a new contributor. Be nice, and check out our Code of Conduct.












          Billy Yin is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210718%2fvalidate-movement-of-a-randomly-moving-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre