Results 1 to 3 of 3

Thread: Array question

  1. #1
    Arom is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Array question

    Hi

    I am to design a hunters game in java which is going to be run from cmd(strictly no GUI) I have created a 12x12 array which is my board, I need to move my character using keys, and make a Hunter(NPC) move towards [0][0] from [12][12]. How do I store each players and hunters coordinates (To check for collisions etc)?
    Java Code:
    public class Hunters {
        
            String [][] a2;
            private static int score;
           private static String player = "P";
           private static String move;
           
    
      public static void main(String[] args){
          
           Scanner in = new Scanner(System.in);
    String [][]a2 = new String [12][12];
    String emptyfield = "X";
    score = 5;
    
    for (int r = 0 ; r < a2.length; r++){
        for (int c= 0; c <a2[r].length; c++){
            a2 [r][c] = emptyfield;
            a2[0][0] = player;
            System.out.print(" "+a2[r][c]);
        }
      System.out.println("");
     
    }
    System.out.println("Input your move");
    move = in.nextLine();
    if (move.equalsIgnoreCase("w")){
     //move up
        //repaint
        //check for collision
        //check for health
    }else if(move.equalsIgnoreCase("s")){
        //move down
        //repaint
          //check for collision
        //check for health
    }else if(move.equalsIgnoreCase("d")){
        //move right
        //repaint
          //check for collision
        //check for health
    }else if(move.equalsIgnoreCase("a")){
        //move left
        //repaint
          //check for collision
        //check for health
    }   
    }
    }
    Thanks!

  2. #2
    zirbinator is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default Re: Array question

    The easiest way to store coordinates in a 2D system is to use a pair of integers or a Point. You're structure is very awkward though, a couple of notes on that:

    Putting everything in the main() method only alright for the smallest of programs. A game should definitely use some other methods, even a small one like this - maybe even some other classes.
    Getting input from the command line is not very usual, unless you're making a text-based game. Most games would open a window (JFrame is usually used) and use a Listener (implement KeyListener, MouseListener, etc.) to get input.

    For your original question though, some code would look like this:

    Java Code:
    public class Hunters
    {
    	String [][] a2;
    	private static int score;
    	private static String player = "P";
    	private static String move; 
    	private static Point playerLoc;
    	private static Point hunterLoc;
    	
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner(System.in);
    		String [][]a2 = new String [12][12];
    		String emptyfield = "X";
    		score = 5;
    		playerLoc = new Point(6, 6);
    		hunterLoc = new Point(12, 12);
    		
    		for (int r = 0 ; r < a2.length; r++)
    		{
    			for (int c= 0; c <a2[r].length; c++)
    			{
    			    a2 [r][c] = emptyfield;
    			    a2[0][0] = player;
    			    System.out.print(" "+a2[r][c]);
    			}
    			System.out.println("");
    		}
    		
    		System.out.println("Input your move");
    		move = in.nextLine();
    		if (move.equalsIgnoreCase("w"))
    		{
    			//move up
    			playerLoc.y--;
    			//repaint
    			repaint();
    			//check for collision
    			if (playerLoc.equals(hunterLoc))
    			{
    				// collision! decrease health, etc?
    			}
    			//check for health
    		}
    		else if(move.equalsIgnoreCase("s"))
    		{
    			//move down
    			playerLoc.y++;
    			//repaint
    			repaint();
    			//check for collision
    			if (playerLoc.equals(hunterLoc))
    			{
    				// collision! decrease health, etc?
    			}
    			//check for health
    		}
    		else if(move.equalsIgnoreCase("d"))
    		{
    			//move right
    			playerLoc.x++;
    			//repaint
    			repaint();
    			//check for collision
    			if (playerLoc.equals(hunterLoc))
    			{
    				// collision! decrease health, etc?
    			}
    			//check for health
    		}
    		else if(move.equalsIgnoreCase("a"))
    		{
    			//move left
    			playerLoc.x--;
    			//repaint
    			repaint();
    			//check for collision
    			if (playerLoc.equals(hunterLoc))
    			{
    				// collision! decrease health, etc?
    			}
    			//check for health
    		}
    		// move hunter
    		hunterLoc.x--;
    		hunterLoc.y--;
    		//check for collision
    		if (playerLoc.equals(hunterLoc))
    		{
    			// collision! decrease health, etc?
    		}
    	}
    	
    	public static void repaint()
    	{
    		// drawing code here: text based or JFrame/Graphics based?
    	}
    }
    I left a lot of stuff out that you'll need to add, but that's the main idea. One problem is that if the user types something in that is not "a", "w", "s", or "d" then he doesn't move but the hunter does - you might want to add a final else statement to deal with that, maybe a while loop or something. You can also see that it checks for collisions both after the user moves and after the hunter does, if you want simultaneous moves, you only want to check after the hunter but if you don't, then you will need to wait for a second or so between when the user moves and when the hunter does, the best way is with Thread.sleep().

  3. #3
    Arom is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Array question

    Hi,

    Thanks for the reply, I know that command line assignment is a very strange idea, but there's nothing I can do about it.

    Thing is, I will need about 40+ hunter to spawn throughout the game and each one should follow a defined random path I just don't know where to start with building those NPCs, my code so far :
    Java Code:
    package hunters;
    import java.io.*;
    import java.util.*; 
    import java.awt.*;
    import javax.swing.*;
    
    public class Hunters {
        
            private static int score;
           private static String player = "P";
           private static String move;
         private static   String emptyfield = "X";
         private static   String [][]a2 = new String [12][12];
     private static int pr,pc;
     private static String hunter = "H";
     private static int hr=11,hc=11;
     
     public static void paint_board(){
    for (int r = 0 ; r < a2.length; r++){
        for (int c= 0; c <a2[r].length; c++){
            a2 [r][c] = emptyfield;
            a2[pr][pc] = player;
            a2[hr][hc]= hunter;
            System.out.print(" "+a2[r][c]);
        }
      System.out.println(""); 
    }
      }
      public static void main(String[] args){
         
             Scanner in = new Scanner(System.in);
            score = 0;
    
     paint_board();
     do{
    System.out.println("Input your move");
    move = in.nextLine();
    if (move.equalsIgnoreCase("w")){
     //move up
       pr = pr -1;
        //repaint
       paint_board();
        //check for collision
        //check for health
       
    }else if(move.equalsIgnoreCase("s")){
        //move down
        pr = pr +1;
         for (int i = 0; i <20; i++) System.out.println();
        //repaint
        paint_board();
          //check for collision
        //check for health
       
    }else if(move.equalsIgnoreCase("d")){
        //move right
        pc = pc +1;
        //repaint
        paint_board();
          //check for collision
        //check for health
        
    }else if(move.equalsIgnoreCase("a")){
        //move left
        pc = pc -1;
       for (int i = 0; i < 20; i++) System.out.println("");
        //repaint
        paint_board();
          //check for collision
        //check for health
       
    }
    }while(score !=5);
    }   
    }

Similar Threads

  1. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-06-2011, 11:56 PM
  2. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 01:42 AM
  3. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 03:55 PM
  4. Array Question
    By sc001 in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 04:57 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 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
  •