• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2d array problem

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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|

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working now I think:



Seems like the "*" appears on the right spot now.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic