This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

last night I tried to build basic fruit machine code (just to see how easy it is)  RSS feed

 
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a  GetRandomNumber class (takes one input variable) so it converts a int to Integer and return a random number between 0 and the input variable

In the main class I use the GetRandomNumber class 3 times to get 3 random numbers one for each wheel and assign them to leftWheel, middleWheel and rightWheel

throw them in a game loop with some conditions and when the points get to zero, exit it the game

I have also added line number and points as each stage to keep track of what's going on
it seems to work 95% of the time but not sure why the score system don't work right every time (line 29, line 34)

comparing the objects int(==)/Integer(.equals) variable value don't seam to work all the time
any help work be great

here's the code









 
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using int not Integer throughout your program. Then you'd need  to change this to:
You didn't need to make GetRandomNumber a class, it would have been easier to create a method for this. And I don't see where the class buys you much. You could have just written
For debugging, I would have printed out all the wheels and the score on line 28 every time through the loop. You probably haven't run across System.out.printf(...) yet but it is extremely useful when you want to get data to line up in columns. For example:
The percent sign says what immeadiately follows it is a formatting instruction. This is followed by the width in characters to allocate to the printed output. And 'd' means digits (aka an int, Integer, short, long, etc).
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks for the help Carey Brown
I did try the double == but I must of done something  wrong so I tried the integer so i could use the .equals

great ideal with the leftWheel = rand.nextInt(range);

and the printf I have used a few times before, but didn't really understand how it worked but I am really starting to get how java works now

what do you think of my updated code ?








 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might make it a bit cleaner:
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes its really good, going to spend some time working out what to do next with the random code

what to see how much control I have over the random.nextInt ( like if points are low, increase the odds of winning by reducing the range)  

any idea that may work ?









 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This might make it a bit cleaner:



you are right that does make it look clearer, just trying to get more used to for loops with arrays and getting clean short code




 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Still trying to work a few things out and help would be great, found some code and it works great in android so, here's the code I am finding difficult to understand
my goal is to stop the wheel spinning after 2-4 seconds without using on click





 
Marshal
Posts: 5637
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specifically are you having trouble understanding?
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, I am having trouble trying to ask the right questions because I feel the code is getting complicated

so I have array of 6 images on 3 wheels to make up the fruit machine, click a button and after a random time in order, first wheel stops followed by the seconded wheel and the third wheel ( this part works great and I have good control and lots of options where I can change the speed, time min/max random spin time so it looks real)

the problem is after the wheels have stopped I try something like this and adding this code makes makes the app crash
just want a way to find the current index value after the thread has finished

 

 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jason edwardes wrote:adding this code makes makes the app crash


"Crash"? Abort? Endless loop? Exception? Wrong result?
Can you post any error messages?
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The main two I can see is

Frames not available for unsuspended thread
Variables paused in another thread

this code runs fine but the current Index all way shows the default value before the threads have finished



// looking for a way to run the code after the threads have finished
 
Marshal
Posts: 62261
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jason edwardes wrote:. . . Frames not available for unsuspended thread
Variables paused in another thread  . . .

I don't understand those two things; please explai more. I hope you haven't used thread#pause() or similar.
What do those expressions after case mean? It looks very peculiar design writing something with that many dots in; besides, whatever appears after case must be a compile‑time constant.
 
Knute Snortum
Marshal
Posts: 5637
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see anything specifically wrong with the code above, except maybe that it has many instance or static variables.  I see wheel1, R, and msg.  Is R and class?  It's not a good name because you can't tell what it is.
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basic fruit machine, press a button and the 3 wheels give you a random image (cherry, seven, bar ect)
after a random time the first wheel stops, followed by the second and then the third

on line 74 (switch) I can set the current item value and display it in the textview

The problem is I need to pause a single method for 3 seconds and get the latest current item value
and store it in a variable to use in a point system

things like sleep,pause, not working for me,
they crash the app or makes the hole app sleep for a short time


 
Campbell Ritchie
Marshal
Posts: 62261
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use Thread#sleep() on a GUI, you will put the Event Dispatch Thread (=EDT) to sleep. Don't; as you have seen that makes the whole GUI sieze solid. Don't use Thread#pause(); that can cause all sorts of nasty problems (as well as not being called pause).
If you want things to happen at 5″ intervals, find out about Timers; I think that will sort out your problem.
Did you actually get that code to compile? I am worried about the cases.
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You everyone for the help

I have found most the answers I was looking for and would be happy to share them with anybody thinking about using some type of animation in there app.
The way I was trying to build the app was not the best way and I think it was going include lots of bugs and problems

so I started again and look at it from a different angle (animation-list) like an old rolling move clip I could use images to make the wheels






 
Knute Snortum
Marshal
Posts: 5637
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jason edwardes wrote:I have found most the answers I was looking for and would be happy to share them with anybody thinking about using some type of animation in there app.


That's great!  I think sharing what you discovered would be a wonderful way to give back to this community.
 
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically it looks good, but I think you can take this further...
Your machine just allows user to earn 15 points or 4 points. that too based on rigid rules: All three match or just 2 matches.
I have played slot machines where it first asks you to place a bet over what you think would win. The user can place multiple bets including diagonal match, middle line, top line, etc.. There are also bets on zigzag patterns. Once the user has invested all his bets, he cranks up the slot machine.

There are slot machine variations with 4 wheels and 5 wheels too instead of 3.

User may win some bets and lose some... But no matter what, the house always wins
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Technically it looks good, but I think you can take this further...
Your machine just allows user to earn 15 points or 4 points. that too based on rigid rules: All three match or just 2 matches.
I have played slot machines where it first asks you to place a bet over what you think would win. The user can place multiple bets including diagonal match, middle line, top line, etc.. There are also bets on zigzag patterns. Once the user has invested all his bets, he cranks up the slot machine.

There are slot machine variations with 4 wheels and 5 wheels too instead of 3.

User may win some bets and lose some... But no matter what, the house always wins



you would be surprised with my code, adding extra wheels is easy and some of the point system looks like this

points is a random number, at this point up to 85
adding wheels is as simple as adding an extra IndextNumber and as my wheels all show 3 items vertical on the wheel, adding diagonal match would be easy, the way I have build this app means there going to
be a lot of code but easy simple code and the same goes for the point system, low money in, high random number and as the money goes up the random number goes down till the odds of winning is very high, could even add things like 3x wins spin and use random number 7



     if (points == 1){leftIndextNumber =1; midleIndextNumber=1 ;rightIndextNumber =1;credit +=100; win = "Jackpot";}
       else if (points == 2){leftIndextNumber =2; midleIndextNumber=2 ;rightIndextNumber =2;credit += 50; win = "winner";}
       else if (points == 3){leftIndextNumber =3; midleIndextNumber=3 ;rightIndextNumber =3;credit +=40; win = "winner";}
       else if (points == 4){leftIndextNumber =4; midleIndextNumber=4 ;rightIndextNumber =4;credit +=40; win = "winner";}
       else if (points == 5){leftIndextNumber =5; midleIndextNumber=5 ;rightIndextNumber =5;credit +=40; win = "winner";}
       else if (points == 6){leftIndextNumber =6; midleIndextNumber=6 ;rightIndextNumber =6;credit +=40; win = "winner";}
       else if (points == 7){leftIndextNumber =7; midleIndextNumber=7 ;rightIndextNumber =7;credit +=30; win = "winner";}

 
salvin francis
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a lot of code and it would be a maintenance nightmare.

Here's my suggestion with comments to help you out:


Each SlotWinRule maintains its own "matches" method to see if it matches the current WheelData. It can also contain it's own points and type.
 
jason edwardes
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Thats a lot of code and it would be a maintenance nightmare.

Here's my suggestion with comments to help you out:


Each SlotWinRule maintains its own "matches" method to see if it matches the current WheelData. It can also contain it's own points and type.




This looks great and I have spent some time last night and today seeing how far I can get,
really can't work out how to get the imageview current image after the Animation has stopped







 
salvin francis
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jason edwardes wrote:... today seeing how far I can get, really can't work out how to get the imageview current image after the Animation has stopped


As your program is related to Android, I think that this post can be moved to a different forum. All that I can advise is keep your UI implementation logic separate from your Game workflow logic. I suggest to keep UI as the last task. First, get your main logic done and test it thoroughly. Once that is done, then work on UI.

Also wanted to point out, do not quote the complete posts of other users, just crop the sentences you want to point out. In your previous post, you quoted my entire code just to respond that it looks great.
 
salvin francis
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:...Is R and class?  It's not a good name because you can't tell what it is.


Agree, "R" is not a good name, but that's the way Google wants you to code . The "R" stands for Resources.
https://developer.android.com/reference/android/R
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!