News:

MFG's Character of the Month: October 2026 Nominations

Main Menu

Help with Java

Started by ~*Ishida-Uryuu*~, May 21, 2009, 11:00:51 AM

Previous topic - Next topic

~*Ishida-Uryuu*~

Hey guys, I've got this assignment for a class where I've got to make a game about a player trapped in a Dungeon maze with traps and all this other crap in Java, but I'm barely getting started. I'm also very beginner-ish in Java.

So far, I've self-resolved most of my issues, but now I'm having trouble with this array of objects and it's pissing me off. I can't figure out why it won't work. I'm trying to get dungeon.CellArrayTester(dungeon.populateGrid(r,c), choice) to pretty much return the same value as choice, which I want to be a selected dungeon cell's cell#.

I get this error:

Quote
I:\java\d_and_a\MainGame.java:56: CellArrayTester(Cells[],int) in DungeonMaze cannot be applied to (java.lang.Object[],int)
      System.out.println("\nCell " + choice + "is Cell #" + dungeon.CellArrayTester(dungeon.populateGrid(r,c), choice));
                                                                                          ^
1 error

Here's the problem line:

Spoiler: In the main method (click to show)


Code (java) Select

DungeonMaze dungeon = new DungeonMaze();
System.out.println("\nCell " + choice + "is Cell #" + dungeon.CellArrayTester(dungeon.populateGrid(r,c), choice)); <---- this one


Choice is an int specified through input.
R = rows, specified through input.
C = columns, specified through input.

Here's the populateGrid method in DungeonMaze.java:

Spoiler: populateGrid (very messy ATM, but works as desired) (click to show)

Code (java) Select
public static Object[] populateGrid(int numRows, int numCols) {

Cells[] cell = new Cells[(numRows*numCols)];

/*System.out.println("Rows = " + numRows);
System.out.println("Cols = " + numCols);
System.out.println("Rows x Cols = " + numRows*numCols);*/

for(int p = 0; p < numRows*numCols; p++) {

colNo++;
numCells++;

if(colNo == numCols + 1 && colNo != numCols && rowNo < numRows) {

colNo = 1;
rowNo++;
System.out.print(" ;Row #" + rowNo + "\n[" + colNo + "]");

}

else if(colNo == numCols && rowNo == numRows - 1) {

rowNo++;
System.out.print("[" + colNo  + "]" + " ;Row #" + rowNo);

}

else System.out.print("[" + colNo + "]");

// Make a new cell on every go-around - array of objects with reference number for each cell
//Cells newCell = new Cells();
// Set cell's number
cell[numCells].setCellNumber(numCells); //newCell.setCellNumber(numCells);
// Get cell's coordinates
//newCell.setPosX(colNo);
//newCell.setPosY(rowNo);

}

return cell;


}

Here's the CellArrayTester method in DungeonMaze.java:

Spoiler: CellArrayTester (click to show)

Code (java) Select
public static int CellArrayTester(Cells[] cell, int choice) {

return cell[choice].getCellNumber();

}

and here's the whole Cells class:

Spoiler: Cells (click to show)

Code (java) Select
public class Cells {

static int posx, posy;
static int cellNumber;

public static void setCellNumber(int number) {

cellNumber = number;

}

public static int getCellNumber() {

return cellNumber;

}

}

If I'm not mistaken, populateGrid should return an Object array, which is what I thought it needed, but I'm not even sure if that's the problem. Any help here?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Valodim

[code=java] totally improves readability <- :)

It's the return type of your populate method :P

~*Ishida-Uryuu*~

Oh, cool, didn't know we had that type of code. :P Fixed.

What should the return type be to make it work?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Valodim

the main method provides a pretty good hint

~*Ishida-Uryuu*~

#4
Oh wow, I didn't know "classname[]" could be an object type. o_O

It worked ... for a while. Everything compiles fine, but I changed around a lot of stuff and now it keeps throwing this extremely annoying java.lang.NullPointerException error.

Here's the problem line in DungeonMaze.java (apparently, I never changed this line and it used to work):

Code (java) Select
cell[numCells].setCellNumber(numCells);

The array of cells is instantiated here in StartGame.java:

Code (java) Select
Cells[] cellArray = new Cells[r*c];

and reused in the populateGrid method in DungeonMaze.java here:

Code (java) Select
public Cells[] populateGrid(int numRows, int numCols, Cells[] cell) {

Here's the entire re-made populateGrid method:

Spoiler (click to show)
Code (java) Select
public Cells[] populateGrid(int numRows, int numCols, Cells[] cell) {

System.out.print("\nCell Array size = " + cell.length);

for(int p = 1; p < numRows*numCols; p++) {

System.out.println(" ");
System.out.println("SUCCESS!!");
numCells++;

// Set cell's number
cell[numCells].setCellNumber(numCells);

// Get cell's coordinates
//newCell.setPosX(colNo);
//newCell.setPosY(rowNo);

}

return cell;


}

The method setCellNumber() in Cells.java is here, if important at all:

Code (java) Select
public void setCellNumber(int number) {

this.cellNumber = number;

}


The SUCCESS thing is to see how many loops the for loop actually went though, and so far, it only prints out one, then it throws the error. :(

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Valodim

the
Stuff[] a = new Stuff[];
part only creates an array of Stuff[] references, not the objects themselves. you have to initialize them one by one to do that:
a[0] = new Stuff(lol);

~*Ishida-Uryuu*~

Haha, how could I be so stupid. :XD: I thought what I did actually created the objects.

Thanks Val, it works perfectly now. :D I'll keep this thread open for any future problems, because this will be a difficult program for me, and I'll probably need a lot more help later on.

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Valodim

you might also want to make that a two-dimensional array, if you want to have a rows/columns model. makes a bunch of operations on those easier

~*Ishida-Uryuu*~

Well, for one I don't know how to make those yet, and I got some advice from a person experienced in the field, and he says those suck. :P Anyway, I had it working the way I wanted to for a little while, then the shit fell apart on me and said "FUCK YOU".

Don't feel like typing out another explanation, so here's a repost from another forum:

QuoteI'm making a game for a project due this Tuesday. You're in a dungeon with rows and columns, and for each coordinate there is a cell and there are 3 trap cells that you instantly die from if you walk into them. Also, you die if you hit 3 walls.

For debugging purposes, I worked out a grid printing system, and various things that tell you where the player is, cell #, cell coordinates, player coordinates, etc, so I know exactly where everything is. In the final version, there will be no grid and none of that.

Everything ATM, works how I want it to, except for ONE thing. If a trap or the player gets placed in Column 0, Row 4 its cell number gets increased by anywhere from 1 to 3, sharing a cell number with another one of the cells, and I have no idea why this happens. I've tried putting in countermeasures for this, but they don't seem to take effect.

I used to have a working system where it displayed on the grid the character's position every time I moved and all of the traps' positions, but now it only displays either if it's in the glitched cell.

Here is the entire program, 5 files zipped up in a .rar:

http://www.sendspace.com/file/t3rsxj

The problem files are DungeonMaze.java, Player.java and Cells.java. I apologize if it's messy. Also, ignore the various profanities placed about, it's from anger. They're not gonna be there when I turn this in. :P

I'm in desperate need of help, guys. I'm VERY beginner with Java (since our teacher "teaches" us jack squat) so there may be some very noobish mistakes. Anything is appreciated! Thank you!


                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Duo Solo

You, beginner-ish? I always thought of you as the guy who knew everything lol. Anyways, I think there is a tutorial on Newgrounds.com. I think theres one for both flash and java.

~*Ishida-Uryuu*~

Ok, solved the last problem thanks to another forum.

Getting an exception here, though:

Code (java) Select
System.out.print("\nWhich direction would you like to move?: ");
String dir = input.nextLine();
char d = dir.charAt(0);
dir = Character.toString(d);


In the program, this prints out:

Which direction would you like to move?: Exception in thread "main" java.lang.StringIndexOutOfBounds
Exception: String index out of range: 0
        at java.lang.String.charAt(String.java:687)
        at Game.MoveCharacter(Game.java:71)
        at Game.StartGame(Game.java:47)
        at Main.main(Main.java:35)

No clue what's wrong. It doesn't seem to be waiting for my input before trying to set char d to its value.

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Iced

print d and any other array movement , and print the total length of the array.

~*Ishida-Uryuu*~

What exactly are you trying to say? Are you telling me to do that? If so, why?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Iced

 java.lang.StringIndexOutOfBounds

The index is out of bounds, im telling you to do that so you can see which of the indexes is going over the size of the full thing.

~*Ishida-Uryuu*~

I don't see what that will do. I'm not working with any actual arrays here for this part.

I'm just trying to set dir to a user-defined input of any length, then take the first letter and set dir to that, but it's like, skipping the String dir = input.nextLine() line, and going straight to the char d line.

It used to work as it is, but that was until I put this in the method that MoveCharacter() (that section of code's method) is called from:

Code (java) Select
System.out.print("\nSet mode difficulty (1: SUPER EASY - Grid display) (2: NIGHTMARE MODE - No grid display): ");
int grid = input.nextInt();

while(grid > 2 || grid < 1) {

System.out.print("Please enter either 1 or 2: ");
grid = input.nextInt();

}

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Iced

I misread, thought that this
dir = Character.toString(d);
had [d] instead of (d)


I think it might be reading the empty line, and failing.Make a check to be sure that its not equal to null before attempting reading.

aokmaniac13

#16
AH DUN TOLD YOU BOY.
AH TOLD YA TO GET RID OF SCANNER.
IT'S BAD NEWS.

(You can't ignore me this time because this is exactly where your problem is now)

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

You're welcome.

Spoiler (click to show)
This is how you get inputs from it

String input = br.readLine();
int i = Integer.parseInt(br.readLine());
double d = Double.parseDouble(br.readLine());

~*Ishida-Uryuu*~

#17
Gwehehehe >:]

Unnecessary! >:P:

Fixed it by adding input.nextLine(); before my MoveCharacter() call to clear the input buffer, and my program works flawlessly now. >:D

MISSHUN KUMPREET


EDIT: QUICK QUESTION!

How do I make an exe that runs the game so I can send it to people who don't have compilers?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

Valodim

Quote from: ~*Ishida-Uryuu*~ on May 25, 2009, 11:00:33 PM
How do I make an exe that runs the game so I can send it to people who don't have compilers?

..with java, you don't. that's why people have to download those runtimes. what you can create is a jar file, which is basically just an archive of the class files.. there are tutorials for these kinda very routine things, y'know ;P

and two-dimensional arrays work just like one-dimensional ones.. just with two dimensional quantifiers :P
ie:
Code (java) Select

int[][] stuff = new int[3][3];
for(int i = 0; i < 9; i++)
  stuff[i%3][2-i%3] = i;


Insanius: What's wrong with a Scanner?

aokmaniac13

Sometimes the Scanner will behave erratically
You have very little control over the amount of data it reads
Error handling is a nightmare