class - Problems with random # generators in c# (EDITED) -
edited
i have in main, set objects , call player class. sorry lack of comments in code.
player myplayer; myplayer = new player(); hiders[] myhiders = new hiders[4]; (i = 0; < 4; i++) { myhiders[i] = new hiders(); } while (foundall == false) { myplayer.move(); (i = 0; < 4; i++) { myhiders[i].displaydistance(myplayer.x, myplayer.y); } (i = 0; < 4; i++) { myhiders[i].checkcapture(myplayer.x, myplayer.y); } foundall = true; (i = 0; < 4; i++) { if (myhiders[i].found == false) { foundall = false; } } } console.writeline("you win!");
and these classes
class player { public int x; public int y; public void move() { string buffer; console.writeline("where move?"); buffer = console.readline(); if (buffer == "u") { x++; } if (buffer == "d") { x--; } if (buffer == "l") { y--; } if (buffer == "r") { y++; } } } class hiders { private int x; private int y; public bool found; int[,] map = new int[10, 10]; random myrandom = new random(); int randomhx; int randomhy; public hiders(int hx, int hy) { myrandom = new random(); randomhx = myrandom.next(1, 10); randomhy = myrandom.next(1, 10); } public hiders() { } public void displaydistance(int px, int py) { double distance; distance = math.sqrt(math.pow(x - px, 2) + math.pow(y - py, 2)); console.writeline(distance); } public void checkcapture(int px, int py) { if (randomhx == px & randomhy == py) { found = true; } } }
all of hiders getting same value. how make change 1-10. should working.
each instance of hiders instantiating own instance of random. since being instantiated simultaneously, , random uses system clock default seed, instances of random produce same value sequence. recommendation have instances of hiders share single instance of random.
Comments
Post a Comment