# can't find the logic

• 03-25-2012, 01:34 PM
riris
can't find the logic
hy all, i can't figure out the logic of this
Code:

| | | | | | | | | | |
| | | | | | | | | | |
|A|*|*|*|*| | | | | |
| | | | |*| | | | | |
| | | | |B| | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |

The position of A and B is random. And A and B is linked by *. What is the logic of this?? do i have to make an array 10 x 10, and access the index randomly to put A??? or what??
• 03-25-2012, 01:48 PM
JosAH
Re: can't find the logic
You have coordinates (xa, ya) and (xb, yb) the step to take in the x direction is xd= (xb > xa)?1:-1 and similar for yd; now loop over x:

Code:

for(; xa != xb; xa+= xd)
// do something with coordinate (xa, ya)

and similar again with ya and yd.

kind regards,

Jos
• 03-25-2012, 05:42 PM
riris
Re: can't find the logic
Thanks for the respon. but, i don't know what you mean
• 03-25-2012, 05:52 PM
JosAH
Re: can't find the logic
Quote:

Originally Posted by riris
Thanks for the respon. but, i don't know what you mean

Neither do I, so we're even.

kind regards,

Jos ;-)
• 03-28-2012, 06:07 PM
riris
Re: can't find the logic
i have found the way to place A and B randomly. Here is my scrip
Code:

import java.util.Random;

public class OrderMap {
public static void main(String args[]){

int Y=10;
int X=10;

OrderMap baru = new OrderMap();

int y = baru.ran();
int x = baru.ran();
for(int i=0; i<Y; i++){
for(int j=0; j<=X; j++){
if(y==x){
x=x+1;
}
System.out.print("|");
if(i==y && j==x){
System.out.print("A");
}
else if(i==x && j==y){
System.out.print("B");
}
else if(y>x){
if(i==x && j==x){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
else if(y<x){
if(i==x && j==x){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
}
System.out.println("");
}
System.out.println(y + " " + x);
}

private int rand;

public int ran(){
int nilai[]= new int[10];

Random create = new Random();
rand = create.nextInt(nilai.length);
return rand;
}
}

but still, i can conect A and B with *. Is there any suggestion???
• 03-28-2012, 06:14 PM