Pursue and Evade 2D Model, need help !

0 replies
Hey, I'm struggling with some code and was hopping one of you Java Wizards might be able to help.

Plan is to translate the raw array data into lines..
so basically when the program is done running, I want it to provide me with an image of the path used by Cat(red) and Mouse(Blue).

Java Code:

import java.awt.*;
import java.util.Arrays;
import java.util.Random;
import javax.swing.*;

public class GameTheory
{
public static void main(String[] args)
{
int[] catpost = Start();
int[] mousepost = Start();
String m = Arrays.toString(mousepost);
String c = Arrays.toString(catpost);
Random rn= new Random();
int turn;
int[] tempmouse;
int[] tempcat;
while(mousepost!=catpost && mousepost[0]>=0 && mousepost[0]<=500 &&mousepost[1]>=0 && mousepost[1]<=500)
{
int direction[]=Choise(catpost, mousepost);
tempmouse=mousepost;
tempcat=catpost;
catpost[0] = catpost[0]+30*direction[0];//new catpost
catpost[1] = catpost[1]+30*direction[1];//new catpost
mousepost[0] = mousepost[0]+10*direction[0];//new mousepost
mousepost[1] = mousepost[1]+10*direction[1];//new mousepost
m=m+" "+Arrays.toString(mousepost);
c=c+" "+Arrays.toString(catpost);
System.out.println("Cat: "+c);
System.out.println("Mouse: "+m);

}


}
public static int[] Start()//Initializing location of the Character
{
int[] post= new int[2];//Array with x,y location
Random rn= new Random();
int x = rn.nextInt(501);//Random x-post
int y = rn.nextInt(501);//Random y-post
post[0]=x;//sets x location
post[1]=y;//sets y location
return post;//returns x,y location

}

public static int[] Choise(int[] catpost, int[] mousepost)//Defines cardinal direction of motion---Unit Vector
{
int direction[]={0,0};
Random rn= new Random();
int cardinal;
if(catpost[0]<mousepost[0] && catpost[1]<mousepost[1])//If cat is above and left of mouse
{
cardinal = rn.nextInt(2);
if(cardinal==0)//move X
{
direction[0]=1;
}
else//move Y
{
direction[1]=1;
}

}
else if (catpost[0]>mousepost[0] && catpost[1]<mousepost[1])//If cat is above and right of mouse
{
cardinal = rn.nextInt(2);
if(cardinal==0)
{
direction[0]=-1;
}
else
{
direction[1]=1;
}

}
else if (catpost[0]==mousepost[0] && catpost[1]<mousepost[1])//If cat is directly above mouse
{
cardinal = rn.nextInt(3);
if(cardinal==0)
{
direction[0]=1;

}
else if(cardinal==1)
{
direction[0]=-1;

}
else
{
direction[1]=1;

}

}
else if (catpost[0]<mousepost[0] && catpost[1]>mousepost[1])//If cat is below and left of mouse
{
cardinal = rn.nextInt(2);
if(cardinal==0)
{
direction[0]=1;
}
else
{
direction[1]=-1;
}

}
else if (catpost[0]>mousepost[0] && catpost[1]>mousepost[1])//If cat is below and right of mouse
{
cardinal = rn.nextInt(2);
if(cardinal==0)
{
direction[0]=-1;
}
else
{
direction[1]=-1;
}

}
else if (catpost[0]==mousepost[0] && catpost[1]>mousepost[1])//If cat is directly below of mouse
{
cardinal = rn.nextInt(3);
if(cardinal==0)
{
direction[0]=1;

}
else if(cardinal==1)
{
direction[0]=-1;

}
else
{
direction[1]=-1;

}

}
else if(catpost[0]<mousepost[0] && catpost[1]==mousepost[1])//If cat is directly to the left of mouse
{
cardinal = rn.nextInt(3);
if(cardinal==0)
{
direction[0]=1;

}
else if(cardinal==1)
{
direction[1]=1;

}
else
{
direction[1]=-1;

}

}
else if (catpost[0]>mousepost[0] && catpost[1]==mousepost[1])//If cat is directly to the right of mouse
{
cardinal = rn.nextInt(3);
if(cardinal==0)
{
direction[0]=-1;

}
else if(cardinal==1)
{
direction[1]=1;

}
else
{
direction[1]=-1;

}

}
return direction;
}
}
Avatar of Unregistered

Trending Topics