Hunters game

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!

Can’t you set if a location is blocked or not in the array and then check if the new position is open?

I would add another class:

class Position {
    public int x;
    public int y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Then for your players and hunters just instantiate an instance of Position e.g.:

private Position player = new Position(6,6);

// to move up
player.y -= 1;

and finally use ra4king’s suggestion and use the 2D array to store terrain information about the actual tile e.g. if it is blocked or not etc…

If you’re lazy to create that Position class, can use Point :slight_smile:

Thanks for your suggestions, I will experiment a bit and get back to you :)!

Hi,

I have created a separate ‘Position’ class.

I have a String called player which defines what it actually looks like(in the Main) ;

String player = "P";

And placing it at the beginning of the game at 0,0, so ;

 board[0][0] = player;

In my IF statement, for ‘w’(move up) putting your Position code, I get a [quote] player is already defined…
[/quote]
.
How do I tell java how do my player looks like so the “P” is actually moving through the array using WSAD?

Sorry for basic questions, its my first real project :slight_smile:

Thanks

“is already defined” usually refers to duplicate variable’s name. commonly occur when you forget already mentioned it on global.

Hi,

Sorry, I’ve made too much mess in this thread. I’ve decided to take this project into smaller parts(At last, eh?).

I have to make the game playable in the cmd window, not a GUI.

Using VB i was able to pretty much replicate the idea using the Point method. So far, I want to create a 12x12 square which will be used a board, and be able to spawn a player (string “P”) at 0,0 and move him using keys.