# Thread: can't find the logic

1. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## can't find the logic

hy all, i can't figure out the logic of this
PHP 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??

2. ## 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:

Java Code:
```for(; xa != xb; xa+= xd)
// do something with coordinate (xa, ya)```
and similar again with ya and yd.

kind regards,

Jos

3. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: can't find the logic

Thanks for the respon. but, i don't know what you mean

4. ## Re: can't find the logic

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 ;-)

5. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: can't find the logic

i have found the way to place A and B randomly. Here is my scrip
Java 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???

6. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

## Re: can't find the logic

You've been cross posting this question on can't find the logic (Beginning Java forum at JavaRanch). The solution given by tarun mall is your solution.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•