java - pattern when using Math.random -
java - pattern when using Math.random -
i trying have mouse go through rooms target room. using graph-like scheme x , y axis. have problem computer doesn't seem want add together or subtract existing variable.
console:
the mouse in room (5,4) mouse in room (5,6) mouse in room (6,5) mouse in room (5,4) mouse in room (5,6) mouse in room (5,6) code mouse:
package mouse_maze; public class mouse { private int xcord = 5; private int ycord = 5; //position of mouse when starts public int getxcord() { homecoming this.xcord; } public int getycord() { homecoming this.ycord; } public void move() { //method motion of mouse boolean verticalmove = math.random() < .5; boolean horizontalmove; if (verticalmove == true) horizontalmove = false; else horizontalmove = true; int moveby = 1; if (math.random() < .5) moveby = -1; if (verticalmove) { int test = this.ycord + moveby; if(test < 1 || test > 9) return; this.ycord += moveby; } if (horizontalmove) { int test = this.xcord + moveby; if(test < 1 || test > 9) return; this.xcord += moveby; } system.out.println("the mouse in room (" + xcord + "," + ycord + ")"); } } code maze:
package mouse_maze; public class maze { private boolean ongoing = false; private int tarx; private int tary; //creates target mouse. public static void main(string[] args) { new maze(6, 8).init(); } public maze(int tarx, int tary) { this.tarx = tarx; this.tary = tary; } public void init() { this.ongoing = true; while(this.ongoing) this.iterate(); } public void iterate() { mouse m = new mouse(); m.move(); if (m.getxcord() == tarx && m.getycord() == tary) { this.ongoing = false; system.out.println("the mouse has beat maze!"); //checks if mouse has gotten target room. } } }
first, larn utilize debugger, or @ to the lowest degree larn debug whatever means. meaningless "assume" problem without proving it.
your whole problem has nil random etc.
in iterate() method, creating new mouse every time, instead of having same mouse maintain on moving.
java random
Comments
Post a Comment