Two dimensional array

J

JohnIe

Hi.

I'm trying to program a kind of "slot machine". And I've run into a
problem I can't figure out a proper solution to.

To check if there's a winning line in my slot machine, I need to
compare the lines of int's to check if they're similar.

If the table had three rows and three columns, that would be easy
enogh, writing 5 "if"-sentences, but it's going to be up to the user to
choose the number of rows. (rows=columns)

Example:
My "generate random numbers and print assign them to an table"-loop
outputs the following:

3 - 1 - 5
3 - 3 - 3
2 - 4 - 3

How can I get the program to react to the second line and the diagonal
line that contains similar numbers in a way that works for all numbers
of rows (1<rows=columns<10)

Hope someone can help me..

John I
 
P

Patricia Shanahan

JohnIe wrote:
....
My "generate random numbers and print assign them to an table"-loop
outputs the following:

3 - 1 - 5
3 - 3 - 3
2 - 4 - 3

How can I get the program to react to the second line and the diagonal
line that contains similar numbers in a way that works for all numbers
of rows (1<rows=columns<10)
....

I'm not sure which aspect of this is giving you trouble. The basic
solution is your subject line "Two dimensional array". Since Java does
not have multidimensional arrays as such, I would use an array of arrays.

What do you have so far? Do you have an array of arrays of ints?

Patricia
 
A

andrew.butt

Im not sure if I am understanding your problem totally, but I will
offer my hand of what I think your problem is.

First you will need to create a user defined array of n x n for your
slots. This is easily done by int[][] arrayName = new int[n][n] This
creates an array of n values, in which each array holds another array
of n values (Thus creating a 2 dimensional array). Now you need to
fill them, simply iterate through them filling all the first row, then
moving onto the next column and repeating..

for(int i = 0; i < arrayName.length() ; i++)
{
for(int j = 0; j < arrayName.length() ; i++)
{
//Generate random number
arryName[j] = number generated
}
}

That simple loop will put an element in every position of your array.
I believe you had this part complete, but its just my personal
suggestion. Now onto your real question (as I understand it) You need
to check every row, column and the two major diagonals to see if they
have matches. This is fairly similar actually, just requires simple
iteration.
First for a row:
for(int i = 0; i < arrayName.length();i++)
{
temp = arrayName[0]
for(int j = 0; j < arrayName.length();j++)
{
if (temp != arrayName[j])
//Line is not a winner
else
System.out.println("Line " + i + " is a winner");
}
}

The same thing can be done with little modification to check the
opposite, and for diagonals it is again very similar, but even simpler.
Keep in mind that diagonals the row and column position are always the
same.. so you can simply use 1 loop with the say arrayList[n][n] and
iterate thorugh it, if all [n][n] entries are not the same then your
slot machine isnt a winner.

Hope this helps in some way.

Cheers.
Andrew
 
J

JohnIe

That's just great. Looks like the kind of thing I was looking for!
Haven't tested it yet, but I'm pretty sure that'll work out!

Sorry about the bad explanation of my problem, and also for the heading
that didn't quite match the problem I needed solving. But this'll
probably do it!

Thank you very much Andrew!
 
T

Tom Leylan

If you're going to emulate a slot machine I'll suggest that you don't
randomly feed values into a matrix. You can but consider that a slot
machine is traditionally a series of parallel wheels not the grid of values
you see displayed in the window. So each wheel could be an array of integer
constants. What is "spinning" is a pointer in each wheel object that
identifies which element is the one positioned front and center on the
display but the other values are all present on the wheel and never change.

In order to have a diagonal you'll have to have as many rows as there are
"wheels" so the user simply decides how many wheels he/she wants. Given
that, you can actually create a 10 wheel machine and a add a property to the
machine that indicates how many wheels are active. That value is what the
user controls.

If you consider a win all items along a row or diagonal matching don't
forget you can stop checking as you encounter the first non-matching
element.

And just a thought but you might want to think of the integer constants not
as the numbers that get displayed but rather offsets into another structure
that controls what is displayed. If you want numbers they can be numbers
but they could be images of numbers and by extension images of fruits, or
symbols or whatever else your slot machine might have. This gives the user
the ability to choose the number of wheels and which icons they want to see
spinning around.

Hope this helps
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top