Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Two dimensional array

Thread view: 
JohnIe - 19 Mar 2006 17:15 GMT
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
Patricia Shanahan - 19 Mar 2006 19:35 GMT
...
> My "generate random numbers and print assign them to an table"-loop
> outputs the following:
[quoted text clipped - 6 lines]
> 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
andrew.butt@gmail.com - 19 Mar 2006 21:38 GMT
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[i][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[i][0]
 for(int j = 0; j < arrayName.length();j++)
 {
    if (temp != arrayName[i][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
JohnIe - 19 Mar 2006 22:58 GMT
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!
Roedy Green - 19 Mar 2006 22:21 GMT
>3 - 1 - 5
>3 - 3 - 3
>2 - 4 - 3

see http://mindprod.com/jgloss/gotchas.html#MATRIX
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Tom Leylan - 19 Mar 2006 23:14 GMT
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

"JohnIe" <johellin@online.no> wrote...

> Hi.
>
[quoted text clipped - 23 lines]
>
> John I


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.