Results 1 to 2 of 2
  1. #1
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Need Help! Array of XY post to a Map

    Hey guys(and gals), I'm struggling with some code and was hopping one of you Java Wizards might be able to help....

    The 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).... Any help would be awesome!
    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;
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need Help! Array of XY post to a Map

    One possibility is to create a "2D" String array with dimensions the size of MaxX+1 and MaxY+1. Then you can populate the particular coordinate
    with a character at location (x,y) in the array. For empty locations, the value would be a space. This has problems in that you are using text and the coordinates are large (500 columns or so).

    When you get more experience you could use the Graphics system and draw the path.

    Also, it appears you are using an array so you can return more than one value. A cleaner solution (imho) is to declare a class to hold the values. You
    might even be able to use the Point class in the JDK API.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. first post, array problem
    By jaremah in forum New To Java
    Replies: 19
    Last Post: 10-05-2010, 02:27 AM
  2. Replies: 3
    Last Post: 05-04-2009, 03:15 AM
  3. First post out of the way..
    By sirwiggles in forum Introductions
    Replies: 0
    Last Post: 02-06-2009, 10:44 PM
  4. Storing Array from HTTP Post
    By kskinner in forum New To Java
    Replies: 1
    Last Post: 09-08-2008, 06:00 AM

Posting Permissions

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