programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# 2d array problem

Daniel Ungerfält
Ranch Hand
Posts: 53
Hi!
Im trying to make a little game where you have to guess a position in a 2d array.
If you guess right the program just congrats you and brakes. Else its suppose to show you with a "*" where the missed "shot" hits the array but there´s my problem.

No mather what I do i cant get the "shots" in the right position or to show upp at all.

RESULT:

Guess position in x and y:
2
3
|1|2|3|4|
1|*
2|*
3|
4|

Dave Tolls
Ranch Foreman
Posts: 3065
37

Your two for loops use row and col for the array entries, but you are printing x and y.

That's the first thing that leapt out at me.

Campbell Ritchie
Marshal
Posts: 56570
172
There ain't no such thing as a 2D array. What you have is an array of arrays.
You are probably using random wrongly. Why +1?
Don't believe what it says in the Java® Tutorials about Math#random being easy to use. Have a look at this thread where there is lots of discussion.

Campbell Ritchie
Marshal
Posts: 56570
172
You need to take the logic of that game apart and break it into small parts. Start by getting out of the main method.

Daniel Ungerfält
Ranch Hand
Posts: 53
Campbell Ritchie:

What do you mean by there´s no 2d array? Is my teacher fooling me ? ;-)

I use +1 in the math.random cause I want to generate from 1-4 not 0-3.

I know how to fill a array[][] with random numbers I just dont know how to display that*" at the position (x,y) i give it.

If my guess is x: 2 and y: 4 I would like it to show up like:

|1|2|3|4|
1|
2|
3|
4| *

Daniel Ungerfält
Ranch Hand
Posts: 53
Got it working now I think:

Seems like the "*" appears on the right spot now.

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
java does not have true 2-d arrays.

A true 2-d array would only hold one kind of thing. You believe that your "spel" array holds strings, but it doesn't. It holds arrays. Those arrays hold strings.

Think of java 2-d arrays like a shipping crate that holds egg cartons. Those cartons then hold eggs. The egg cartons do not all have to be the same size.

Whereas a true 2-d array would be a big rectangle that holds eggs in each spot.

Liutauras Vilda
Sheriff
Posts: 4923
334
fred rosenberger wrote:Think of java 2-d arrays like a shipping crate that holds egg cartons. Those cartons then hold eggs. The egg cartons do not all have to be the same size.

I like that expressive rich explanation. I'm sure it will stuck to my mind for future references

Piet Souris
Master Rancher
Posts: 2044
75
fred rosenberger wrote:java does not have true 2-d arrays.

A true 2-d array would only hold one kind of thing. You believe that your "spel" array holds strings, but it doesn't. It holds arrays. Those arrays hold strings.

Think of java 2-d arrays like a shipping crate that holds egg cartons. Those cartons then hold eggs. The egg cartons do not all have to be the same size.

Whereas a true 2-d array would be a big rectangle that holds eggs in each spot.

I agree!

In the first case, one would retrieve an egg with: Egg egg = eggs[2][3].
In the second case, however, one would retrieve that egg with: Egg egg = eggs[2][3].

So it is indeed obvious that the first one is not a genuine 2d array, while the second one is.

Greetz,
Piet

Campbell Ritchie
Marshal
Posts: 56570
172
Anyway, is that all the game does? Asks you to guess one out of sixteen squares?
Please write down what you think the logic for printing the square would be.

 It is sorta covered in the JavaRanch Style Guide.