Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Array game.  (Read 646 times)
0 Members and 1 Guest are viewing this topic.
Offline Arom

Junior Newbie





« Posted 2011-11-30 19:08:05 »

Hi,

I have to create a game in 12x12 array, with a player trying to avoid hunters to gain points. I have done collision, boundaries etc, however I cannot move my hunters, even though the code looks well, if you could, please have a look at it and point me into a right direction/fix my error, thanks!

http://speedy.sh/fpKDP/HunterGame.rar
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-30 21:21:30 »

Most people in this forum won't download zipped files and look through all your code to find your error. You need to learn how to debug your game and if you are still confused, paste the code here in code blocks:
[code] code goes here [/code]

Offline BiGF00T

Junior Newbie





« Reply #2 - Posted 2011-12-01 06:44:46 »

Hi Arom,

I just had a look at your other thread where you had already pasted some of your code. Like ra4king said, I also don't like to download archives of unknown source.

If your code still looks like this, then I would say that you are missing a game loop. The main method draws the map and then there are some conditions which will end after being executed once.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public static void main(String[] args) {    
      ....
      for (int r = 0; r < a2.length; r++) {
      ... }
      System.out.println("Input your move");
      move = in.nextLine();
      if (move.equalsIgnoreCase("w")) {
         // move up
        // repaint
        // check for collision
        // check for health
     } elseif {.... } // no loop here. Main will end and the game will be over.
}


I think there should be a look around your input and conditions. And, of course, you would have to replace the comments by something that is actually doing stuff. If you have done so already, then please post the relevant piece of code.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Arom

Junior Newbie





« Reply #3 - Posted 2011-12-01 07:45:59 »

I have added a lot of stuff, and got the game very much built, however I think I have an error in my Board class;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
public class Board { 

    public String emptyfield = "-";
    public  String [][]a2 = new String[12][12];
 public Hunter hunters[] = new Hunter[5];
       
public void paint(){
   
    int numHunters =5 ;
    for (int i =0; i < numHunters; i ++){
        hunters[i] = new Hunter(11,11,"H");
    }
Player player = new Player();
        for (int r = 0 ; r < a2.length; r++){
            for (int c= 0; c <a2[r].length; c++){
                a2 [r][c] = emptyfield;
                a2[Player.x][Player.y] = Player.name;
                for (int i = 0; i < numHunters; i++){
                     Hunter h = hunters[i];
                     a2[h.x][h.y]=h.name;
                   
                }
                System.out.print(" "+a2[r][c]);
            }
            System.out.println("");
        }
        System.out.println("     Strength:  "+Player.hp);System.out.println("      Score  "+Player.score);

    }

public void route1(){
    Board board = new Board();
    Hunter Hunter = new Hunter(11,11,"H");
    Scanner in = new Scanner(System.in);
    Random number = new Random(2);
    int random = number.nextInt(2);
     if(random ==1)
        Hunter.x = Hunter.x -1;
    else
       Hunter.y = Hunter.y -1;
    }


The
1  
 Route1 
Class is being used for the movement of the hunters as the player is moved around, the value of my Hunter.x and y (possibly) stays at the same value, is the value set back to 11 while executing the
1  
 Paint 
code?

Each Paint and Route1 are executed on the player movement(WSAD - enter).

Offline BiGF00T

Junior Newbie





« Reply #4 - Posted 2011-12-03 13:37:46 »

1  
 Hunter Hunter = new Hunter(11,11,"H");


I did not examine everything in detail but what looked wrong was the piece above.

Can you name the variable same as the class? If you can, then it is at least bad style.
I would prefer something like
1  
Hunter hunter = new Hunter(...);


Quote
The  Route1 Class is being used for...

I see a route1() and paint() method. Maybe you mixed up the terms?

I don't completely understand what you are doing. You say that both methods are called every time the user presses one of the movement keys.
I have now looked a bit more at your code. It seems that you are creating new objects at each cycle of your game loop. And you will always give them the same starting coordinates.

If you want to save the state of player and hunters over the loop, then you should initialize your player once, create the object and use the reference to this one object without throwing it away.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void route1(){
    Board board = new Board();
    Hunter Hunter = new Hunter(11,11,"H");
    Scanner in = new Scanner(System.in);
    Random number = new Random(2);
    int random = number.nextInt(2);
     if(random ==1)
        Hunter.x = Hunter.x -1;
    else
       Hunter.y = Hunter.y -1;
    }

A variable has a certain scope. If you create a variable within a method (like "Hunter" here. use hunter as name, not Hunter. Hunter is your class), it will be in your program's memory until the method ends.
This is what you do:
Player does something. Hunters have to be updated
  • create a hunter object at a position
  • move the newly created hunter into a random direction
  • forget that the hunter ever existed (you don't remember the reference to it)

What you should do:
Your Hunter-class has only a constructor
1  
2  
3  
4  
5  
public Hunter(int x, int y, String name){
    this.x = x;
    this.y = y;
    this.name = name;
}

You should have a move() method and access to your coordinates as well.
Then you could tell the hunter to move. Then you would not create the hunter
within the route1() method but outside your game loop in your Main class. Then,
when it comes to updating the hunters' positions, you do something like this:
1  
2  
3  
for (int i = 0; i < numberofhunters; i++) {
    hunter[i].move();
}


The move() method would then contain all the code to generate the new coordinates
of the hunter. It would store the coordinates within the object and would not forget
them.

There are a lot of strange things in your code. I would suggest doing some Java tutorials
first to get familiar with its basic concepts before doing a bigger project. Learn about
classes, objects and methods. Learn about the scope of variables and their visibility.

A small one class example does not provide as many chances to create bugs and will
improve the understanding. Once you have done this, go back to your project, find the
mistakes and strange things, start new and finish your project.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (26 views)
2014-09-07 01:12:14

Longarmx (32 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!