Pursue and Evade: A Look at “Cat and Mouse”

(An undergrad project)

The point of the attached PowerPoint was to act as a class project demonstrating how it might be possible to populate a game matrix with the appropriate payoffs, assuming one can model/simulate the game on a computer.

CatAndMouse

It should be understood, this is only meant as a demonstration and not a rigorous exercise.


The following is the code used:

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

public class GameTheory2 extends JPanel
{
 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;
 int count=1;

 while(mousepost!=catpost && mousepost[0]>=0 && mousepost[0]<=500 &&mousepost[1]>=0 && mousepost[1]<=500 && count<1000)
 {
 int direction[]=Choice(catpost, mousepost);
 tempmouse=mousepost;//To be used later
 tempcat=catpost;//To be used later
 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);
 count++;
 }
 System.out.println(count+" Cat: "+c);
 System.out.println(count+" Mouse: "+m);
 
 
 /////////////////////***GUI***/////////////////////
 /*
 JFrame frame=new JFrame("Pursuit and Evation");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 JPanel main_window = new JPanel();
 main_window.setPreferredSize(new Dimension(1000, 1000));
 frame.getContentPane().add(main_window);
 frame.pack();
 frame.setVisible(true);
 */
 
 }
 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[] Choice(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)//Move X
 {
 direction[0]=1;
 
 }
 else if(cardinal==1)//Move X
 {
 direction[0]=-1;
 
 }
 else//Move Y
 {
 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;
 }
}
Categories:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s