File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes counting cherries in a 3 ring slot machine using the equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "counting cherries in a 3 ring slot machine using the equals method" Watch "counting cherries in a 3 ring slot machine using the equals method" New topic
Author

counting cherries in a 3 ring slot machine using the equals method

lee shotwell
Ranch Hand

Joined: Nov 22, 2012
Posts: 34
My method counts the "CHERRIES" correctly except in one situation. That situation is as follows: When "CHERRIES" appears on all three rings, my method only counts two out of the three "CHERRIES". I have reworked my if...else statements countless times. For some reason, I must be making a mistake. Can you assist me, please? Here is my complete method with output added at the end.

output:

The above payout is not correct. Only two "CHERRIES" are being counted, not three "CHERRIES". The correct payout should be $20. All else works correctly in the program and in this method. This one problem is driving me crazy. Can someone help me, please?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Welcome to the Ranch

I would suggest you put those values into an array, then you can iterate the array with a for loop and increment the chrry count. Your three‑element array will give you a cherry count of 3 (I hope). Much neater than those multiple ifs.
It might also be possible to use an enumerated type rather than Strings. Then you can respectably use == CHERRY and not need the equals() method.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3539
    
  15
That code won't produce that output because it won't even compile. You have mismatched brackets on lines 4, 8 and 12.

And even if you fix those, I suspect you still won't get that output from that code (although that is based on just looking at the code, not running it, so I may be wrong).


Joanne
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:It might also be possible to use an enumerated type rather than Strings. Then you can respectably use == CHERRY and not need the equals() method.

Even better: you won't have to type the string "CHERRIES" overnover.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Joanne Neal wrote:And even if you fix those, I suspect you still won't get that output from that code (although that is based on just looking at the code, not running it, so I may be wrong).

The logic seems suspicious to me, too. I would check that again.


The quieter you are, the more you are able to hear.
lee shotwell
Ranch Hand

Joined: Nov 22, 2012
Posts: 34
Reply to Joanne Neal and Kemal Sokolovic: I think I understand what you mean by the mismatched brackets. For example, I should bracket "not equal" like this: if(!(s1.equals("CHERRIES"))) However when I do that it messes up the cherryCnt a lot. Several combinations of CHERRIES, SPACE, BAR, and 7 do not count and pay the correct amount . With the way the code is written ,at this time, my program does compile and the only error, at this time, is that when my slot machine displays: CHERRIES CHERRIES CHERRIES my method only counts two CHERRIES rather than three so my payout is not correct. For sure, I have an error in my logic, but I have not discovered it yet.

Reply to Sheriff Campbell Ritchie: Thanks for your suggestion about using an array, but I am a greenhorn and I have not studied arrays yet, however now may be the time to do just that. On the other hand, I would like to see if I can write my if...else statements correctly so that my cherry jackpot pays correctly like my BAR and 7 jackpots do.

Thanks for all the help,

Lee Shotwell
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
lee shotwell wrote: . . . I should bracket "not equal" like this: if(!(s1.equals("CHERRIES"))) . . .
No, you shouldn’t; the middle pair of () is redundant because the dot operator has a higher precedence than the bang operator !. You can use the () if you wish, but they are not necessary. If you were using && as well, you might need () because bang has a higher precedence than &&.
That is not going to produce any errors. Your errors must be elsewhere.

Your many if blocks for counting the cherries look unwieldy to me. I think you ought to change them.
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 152

What are the rules to your slot machine? Does order count?

Most slot machines that I've played (not that I'm an expert) work like this:

CHERRY - CHERRY - BAR = 10 points
CHERRY - BAR - CHERRY = 3 points

If order does matter, then you can simplify your if statements with one nested if.


lee shotwell
Ranch Hand

Joined: Nov 22, 2012
Posts: 34
Reply to Campbell Ritchie: When I was entering my method to the ranch I noticed that I had an errant single bracket in my if(s2... statement. That errant bracket was causing my method to only count one cherry and to only payout for one when CHERRIES CHERRIES CHERRIES appeared on one line. When I got rid of that errant bracket, my method counted two cherries and the payout increased. I then looked at my code carefully, hoping to find another errant bracket so as to increase my count to three cherries, but no such luck came my way. Yes, I am using the && operator. I tried removing the bracket after ! (!(s2.equals("CHERRIES")) and I got these errors: CHERRIES CHERRIES 7, You won $3. CHERRIES CHERRIES CHERRIES, You won $3. Therefore, I need that bracket.

Reply to Bill Clar: Here are the rules to my slot machine (a.k.a. one armed bandit) and yes, order does count.
1. Each of the 3 positions in the String could be one of the following: "BAR", "7", "CHERRIES" or "SPACE".
2. Each of the 3 output positions are determined randomly with a 25% chance of any one of the 4 in any position.
3. Here are the combinations that should pay the bet as shown:
CHERRIES [not CHERRIES] [any] pays 3 x the bet
CHERRIES CHERRIES [not CHERRIES] pays 10 x the bet
CHERRIES CHERRIES CHERRIES pays 20 x the bet
BAR BAR BAR pays 35 x the bet
7 7 7 pays 50 x the bet
My problem has been with the CHERRIES and making sure that a payout occurs only when they appear in the above positions. The second problem: If the CHERRIES appear in the correct position, my method must count them correctly so that the payout is correct. Currently, my method only counts two CHERRIES, hence the payout is for only two CHERRIES when actually all positions have CHERRIES. My method works fine with all the other combinations. In the beginning, my method was counting the CHERRIES when they appeared in any position. I have spent a lot of time getting rid of those errors and now I have one remaining. I have not tried a nested if yet, like you suggested so I will try that and let you know how it works.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
What about
CHERRY [notCherry] CHERRY? How much does that pay? Or [notCherry] CHERRY CHERRY?

When you have answered that, I may make some unwelcome suggestions.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7784
    
  21

lee shotwell wrote:My problem has been with the CHERRIES and making sure that a payout occurs only when they appear in the above positions. The second problem: If the CHERRIES appear in the correct position, my method must count them correctly so that the payout is correct.

Sounds to me like you're overthinking this.

My advice: StopCoding (←click) and write out your combination logic in English.

It seems to me that you're getting yourself tied up in knots trying to work out all the combinations, rather than looking carefully at your rules.
Tip: What should you do if the first position is NOT a cherry? That might get you started in the right direction.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Winston Gutkowski wrote: . . . My advice: StopCoding (←click) and write out your combination logic in English.
. . .
My unwelcome suggestion would have been to remove that black wire leading from the computer to the wall.
lee shotwell
Ranch Hand

Joined: Nov 22, 2012
Posts: 34
Reply to CAMPBELL RITCHIE and WINSTON GUTKOWSKI: First, Campbell Ritchie asked what would my method do if CHERRIES [not CHERRIES] CHERRIES appeared and if
[not CHERRIES] CHERRIES CHERRIES appeared. Here is my output.

I respect what you have said to me. I know that my method does not cover that situation (where position one is not a CHERRIES) but as you can see my output is fine. However, you may be hinting that that the reason my code does not count CHERRIES CHERRIES CHERRIES correctly because I have not included those situations in my if..else statements. At any rate, Winston I will stop coding and work out my logic first and then enter it into my code. In closing though, I am somewhat confused. Tomorrow I must take my family shopping, but the following day I will go to a cafe away from my computer and try to work out the logic. I have the bad habit of firing up my Eclipse Europa compiler and doing the logic and code at the same time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
The reason for asking about BAR‑CHERRY‑CHERRY was that you cannot tell from your code whether that possibility is covered at all. Remember a fruit machine has three wheels with twenty pictures on each, so you get 20³=8000 possible combinations.Also there might be an easier way to work out the combinations. Remember you can ignore most of those 8000 as losing combinations, as evidenced by the existence of Las Vegas
lee shotwell
Ranch Hand

Joined: Nov 22, 2012
Posts: 34
Reply to CAMPBELL RITCHIE: So far, I have not ran into a combination on my one armed bandit that my method , (as presently written)cannot handle. It is the count of three CHERRIES in a row that it cannot handle. By the way, when I used to visit Las Vegas with my wife for a holiday, she would take one day to record the slots that were paying by writing down the slot machine number of each machine. Therefore, every morning she would play those slots (nickel and dime machines) and collect enough for three meals each day. I was never able to perfect my card counting in the game of blackjack, especially when the dealer was using a shoe containing several decks. Hence, I better stick to my slot machine program.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7784
    
  21

lee shotwell wrote:I was never able to perfect my card counting in the game of blackjack, especially when the dealer was using a shoe containing several decks.

That's a pity, since I understand that that's when it works best.

Winston
lee shotwell
Ranch Hand

Joined: Nov 22, 2012
Posts: 34
Reply to CAMPBELL RITCHIE & WINSTON GUTKOWSKI: Good News! As you suggested, I pulled the plug on my computer, went to my favourite cafe and wrote the logic to be used in the method on a piece of paper. When I got home I changed my method on the computer and tried it. A miracle occurred, the method worked perfectly so the count of my three cherries is now correct. Here is my revised method.

Output:

Both of you have helped me a lot, thanks again. Winston, concerning the use of the shoe in blackjack, it is best to find a dealer using one deck and a full table. Remember, you are playing against the dealer and with a full table the face cards should come out quicker on the other hand, if you play one on one with the dealer it is easier to know the exact count. If you know that all the face cards are out, except for the king and ace in your hand you can bet the ranch and you will feel like a power ball winner. Pit falls? It is hard to find a one deck game and the dealer will often only play through half the deck or less, then he grabs a new deck. Furthermore, if the house knows you are counting, chances are, you will be asked to leave. Have a nice weekend.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
Your logic is overly complex, take the following for example:

Basically this saying if both s2 and s1 have cherries and s3 either has or does not have a cherry increment the count. Or to put it more concisely if both s2 and s1 have cherries increment the count. You could leave off the s3 check and remove the else part of the statement.

EDIT: BTW when posting code please make sure it is the code you are running - the code posted won't compile as there are missing closing brackets on the Bar and 7 if statements
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
lee shotwell wrote:. . . A miracle occurred,. . . .
Nothing miraculous. If you do it right, it will work.

Well done

I don’t know whether you can shorten that block of if statements.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: counting cherries in a 3 ring slot machine using the equals method