Reputation: 55
I have this 2D array (say double[10][10]) which contain some 1.0 and 10.0, rest are all in 0.0s. I am trying to loop through this array to find the 1.0 (start point), from there 'move' it randomly (using random.nextInt(4)) up, down, left or right till it gets to 10.0. I created a emptyArray to keep track of how many time it had moved pass each point (or at least I think I did). Nothing came up when I compile but I do not get any result when I try to display it into a frame. Any idea where I had gone wrong or missing?
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
Sample of 2D array.
double[][] getPath(double[][] dataIn) {
double[][] emptyArray = new double[dataIn.length][dataIn[0].length];
double[][] drunkLoc = new double[dataIn.length][dataIn[0].length];
for (int i = 0; i < dataIn.length; i++) {
for (int j = 0; j < dataIn[i].length; j++) {
if (dataIn[i][j] == 1.0) {
double drunkHome = 10.0;
drunkLoc[i][j] = dataIn[i][j];
do {
int dir = getDirection();
switch(dir) {
case 0:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i-1][j];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 1:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i][j-1];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 2:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i+1][j];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 3:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i][j+1];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
default:
}
} while (drunkLoc[i][j] != drunkHome);
}
}
}
return emptyArray;
}
Do let me know if you need any more clarification. Only my second post so still learning on my questioning technique. Thanks in advance.
Upvotes: 0
Views: 951
Reputation: 21795
OK, for you to study, here's some sample code that will give you the answer you're looking for (the number of moves to get from (xpos,ypos) to (destX,destY) on a "board" that is penDimension x penDimension in size:
int penDimension = 10;
int destX = 2;
int destY = 2;
int xpos = 5;
int ypos = 5;
// Add this to keep track of no moves through each square
int[][] moveCounts = new int[penDimension][penDimension];
Random r = new SecureRandom();
long noMoves = 0;
while (xpos != destX || ypos != destY) {
switch (r.nextInt(4)) {
case 0 : xpos++; break;
case 1 : xpos--; break;
case 2 : ypos++; break;
case 3 : ypos--; break;
}
if (xpos < 0) xpos = 0;
if (ypos < 0) ypos = 0;
if (xpos > penDimension) xpos = penDimension;
if (ypos > penDimension) ypos = penDimension;
noMoves++;
// Add this to keep track of no moves through each square
moveCounts[ypos][xpos]++;
}
System.out.println("Number of moves: " + noMoves);
Instead of doing ++ or -- and then checking the bounds afterwards, you could also write (and in real life probably would write) e.g.:
xpos = Math.max(0, xpos - 1);
I just wrote it as I did above because I thought it would be easier to understand.
Instead of writing "new SecureRandom()", you could also write "new Random()", which is probably what you've learnt. But SecureRandom is a much higher quality (but slower) random number generator. In general when writing 'simulations' where you are repeatedly generating a large number of random numbers, it's better to avoid the standard Random class and use a higher quality generator.
Upvotes: 1