Ok, so I'm taking a class in Java. I've already turned in this assignment but after doing so I got to thinking about a piece of code that I came up with to solve a problem. I was wondering what better way there might be. As a rule (because the class hasn't learned it, though I know it already) we can't use arrays, which I complained to the teacher because I think it would make the whole assigment easier.
Bascially, it's a slot machine, 3 slots and 6 symbols (using text to simulate) at the end I have to count matching slots and pay accordingly. When you see my code you'll see a method that I came up with, but I was wondering if there were an easier way. Mainly because I want to up scale it to 5 slots I can see how I would have to modify my existing code, but not sure if then there was an easier way.
Here's the entire code:
package slot.machine; import java.util.Random; import java.util.Scanner; public class SlotMachine { public static void main(String[] args) { Scanner kb = new Scanner(System.in); Random rnd = new Random(); int rndSlot1, rndSlot2, rndSlot3, betAmnt, matches, multiplier; int totalWinning = 0, totalBet = 0, cntPlays = 0; do { // Let's get the slots first, casino style programming ;) rndSlot1 = rnd.nextInt(5); rndSlot2 = rnd.nextInt(5); rndSlot3 = rnd.nextInt(5); // Let's (re)set some things matches = 0; multiplier = 0; // Get bet from user System.out.print("Please enter bet amount (0 to quit): "); betAmnt = kb.nextInt(); if(betAmnt > 0) // If they want to play... { cntPlays++; //Number of games player...just for fun. totalBet += betAmnt; // Accumulate total bets // Display Results switch(rndSlot1) { case 0: System.out.print("\nCherries "); break; case 1: System.out.print("\nOranges "); break; case 2: System.out.print("\nPlums "); break; case 3: System.out.print("\nBells "); break; case 4: System.out.print("\nMelons "); break; case 5: System.out.print("\nBars "); break; default: // Shouldn't Get here... System.out.print("\nTILT! "); // Or "Error!" or something like that break; } switch(rndSlot2) { case 0: System.out.print("Cherries "); break; case 1: System.out.print("Oranges "); break; case 2: System.out.print("Plums "); break; case 3: System.out.print("Bells "); break; case 4: System.out.print("Melons "); break; case 5: System.out.print("Bars "); break; default: // Shouldn't Get here... System.out.print("TILT! "); // Or "Error!" or something like that break; } switch(rndSlot3) { case 0: System.out.println("Cherries"); break; case 1: System.out.println("Oranges"); break; case 2: System.out.println("Plums"); break; case 3: System.out.println("Bells"); break; case 4: System.out.println("Melons"); break; case 5: System.out.println("Bars"); break; default: // Shouldn't Get here... System.out.print("TILT!"); // Or "Error!" or something like that break; } // Count the matches if(rndSlot1 == rndSlot2 || rndSlot1 == rndSlot3) matches++; if(rndSlot2 == rndSlot3) matches++; switch(matches) { case 0: // No match, no winner multiplier = 0; break; case 1: // 2 match multiplier = 2; break; case 2: // 3 match multiplier = 3; break; default: //Shouldn't get here System.out.println("Sorry, cheaters are losers."); multiplier = 0; break; } if(multiplier > 0) // See if they won anything this time { totalWinning += (betAmnt * multiplier); // Accumulate Total Winnings System.out.println("\nCongratulations, you've won : $" + (betAmnt * multiplier)); } else { System.out.println("\nSorry, please try again"); } } } while(betAmnt > 0); System.out.println("\nThank you for playing.\n"); System.out.println("Your total winnings: " + totalWinning); System.out.println("Your total bets: " + totalBet); System.out.println("You lost/gained: " + (totalWinning - totalBet) + " in " + cntPlays + " pulls."); } }
Particularly these lines:
// Count the matches if(rndSlot1 == rndSlot2 || rndSlot1 == rndSlot3) matches++; if(rndSlot2 == rndSlot3) matches++; switch(matches) { case 0: // No match, no winner multiplier = 0; break; case 1: // 2 match multiplier = 2; break; case 2: // 3 match multiplier = 3; break; default: //Shouldn't get here System.out.println("Sorry, cheaters are losers."); multiplier = 0; break; }
So, thoughts? What is a better way to find matches between variables (all ints) without over counting (My first code made that mistake, but I fixed it and resubmitted it to my teacher in time)