I finally finished my simple slot machine code, however am struggling to understand classes a little bit. I want to Split this into two classes and not sure how to go about it. My idea is to take the first half which is doing the random generation. Creating a class called Reels, and putting the code in method like public void spin(). Although, when I split it into another class and tried to call the spin method things got crazy... I apologize if this was not the easiest explanation and hope/appreciate any help I can get.
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int numReels = 3; //Declare how many reels spin on machine
Random random = new Random(); // Create Random
int[ ] symbols = new int[numReels]; // Create an array of three random numbers
String[ ] logo = new String[numReels]; // Second Array to assign numbers to symbols
final String[] allSymbols = {"Cherry", "7", "Grape", "Orange", "Lemon", "Spade", "Club", "Diamond", "Heart", "Ace"}; // A final list of what symbols there are
for (int i = 0; i < symbols.length; ++i) { // Loop through the integer list to obtain random numbers
symbols[ i ] = random.nextInt(10); //Random numbers from 0 - 9
//System.out.println(symbols[i]); Test to make sure list is looping properly
}
for (int i = 0; i < symbols.length; ++i) { // Assign second array to the correct symbol according to number
logo[i] = allSymbols[symbols[i]];
}
//Test each element in list to make sure obtaining correct values
//System.out.println("symbol #: " + symbols[0] + " logo: " + logo[0]);
//System.out.println("symbol #: " + symbols[1] + " logo: " + logo[1]);
//System.out.println("symbol #: " + symbols[2] + " logo: " + logo[2]);
// Simplify each element by assigning to single letter variables
int a = symbols[0];
int b = symbols[1];
int c = symbols[2];
String answer = "Y"; // Variable to be used in while loop (continue playing or not)
int userBet; // Variable for amount user bets
int doub; //Variable for reward when user matches two symbols
int trip; //Variable for reward when user matches three symbols
int jack; //Variable for reward when user matches three jackpot symbols
int amountWon = 0;
System.out.println("Welcome to the Simple Slot Machine! Enter a betting amount to spin!!"); // Print intro to user
do { //do-while loop for when answer variable is Y or y
userBet = scnr.nextInt(); // Take input for betting amount
if (userBet < 10) { // Bet must be higher than $10
System.out.println("Bet must be higher then $10");
}
if (a != b && a != c && b != c) { // When no symbols match the user loses amount that was bet
System.out.println("You lost your $" + userBet);
amountWon -= userBet;
}
else if (a == 1 && b == 1 && c == 1) { // When all jackpot symbols match user wins Jackpot and adds to winnings
jack = userBet * 100;
System.out.println("Congratulations! You have won with a " + logo[0] + ", " + logo[1] + ", " + logo[2] + " the jackpot of $" + jack);
amountWon += jack;
}
else if (a == b && b == c) { // When three symbols match user wins triple award and adds to winnings
trip = userBet * 70;
System.out.println("Congratulations, you have won with a Triple!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + trip);
amountWon += trip;
}
else if (a == b || a == c || b == c) { // When two symbols match user wins award and add to winnings
doub = userBet * 30;
System.out.println("Congratulations, you have won with a Double!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + doub);
amountWon += doub;
}
System.out.println("Continue? Y/N "); // Once awards accounted for ask if player continues or not
answer = scnr.next();
} while (answer.equals("y") || answer.equals("Y"));
if (!answer.equals("Y")) {
if (amountWon < 0) { //Print statement for when player is in the hold
System.out.println("You ended losing " + amountWon +" dollars" + ", but people always stop before they hit it big!");
}
else { // Print statement for when player is in positive
System.out.println("You ended with $" + amountWon + " come back again!");
}
}
}
}
Better visual of how I am splitting code:
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class Reel {
public void spin() {
final int numReels = 3; //Declare how many reels spin on machine
Random random = new Random(); // Create Random
int[ ] symbols = new int[numReels]; // Create an array of three random numbers
String[ ] logo = new String[numReels]; // Second Array to assign numbers to symbols
final String[] allSymbols = {"Cherry", "7", "Grape", "Orange", "Lemon", "Spade", "Club", "Diamond", "Heart", "Ace"}; // A final list of what symbols there are
for (int i = 0; i < symbols.length; ++i) { // Loop through the integer list to obtain random numbers
symbols[ i ] = random.nextInt(10); //Random numbers from 0 - 9
//System.out.println(symbols[i]); Test to make sure list is looping properly
}
for (int i = 0; i < symbols.length; ++i) { // Assign second array to the correct symbol according to number
logo[i] = allSymbols[symbols[i]];
}
}
}
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Reel action = new Reel();
action.spin();
//Test each element in list to make sure obtaining correct values
//System.out.println("symbol #: " + symbols[0] + " logo: " + logo[0]);
//System.out.println("symbol #: " + symbols[1] + " logo: " + logo[1]);
//System.out.println("symbol #: " + symbols[2] + " logo: " + logo[2]);
// Simplify each element by assigning to single letter variables
int a = symbols[0];
int b = symbols[1];
int c = symbols[2];
String answer = "Y"; // Variable to be used in while loop (continue playing or not)
int userBet; // Variable for amount user bets
int doub; //Variable for reward when user matches two symbols
int trip; //Variable for reward when user matches three symbols
int jack; //Variable for reward when user matches three jackpot symbols
int amountWon = 0;
System.out.println("Welcome to the Simple Slot Machine! Enter a betting amount to spin!!"); // Print intro to user
do { //do-while loop for when answer variable is Y or y
userBet = scnr.nextInt(); // Take input for betting amount
if (userBet < 10) { // Bet must be higher than $10
System.out.println("Bet must be higher then $10");
}
if (a != b && a != c && b != c) { // When no symbols match the user loses amount that was bet
System.out.println("You lost your $" + userBet);
amountWon -= userBet;
}
else if (a == 1 && b == 1 && c == 1) { // When all jackpot symbols match user wins Jackpot and adds to winnings
jack = userBet * 100;
System.out.println("Congratulations! You have won with a " + logo[0] + ", " + logo[1] + ", " + logo[2] + " the jackpot of $" + jack);
amountWon += jack;
}
else if (a == b && b == c) { // When three symbols match user wins triple award and adds to winnings
trip = userBet * 70;
System.out.println("Congratulations, you have won with a Triple!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + trip);
amountWon += trip;
}
else if (a == b || a == c || b == c) { // When two symbols match user wins award and add to winnings
doub = userBet * 30;
System.out.println("Congratulations, you have won with a Double!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + doub);
amountWon += doub;
}
System.out.println("Continue? Y/N "); // Once awards accounted for ask if player continues or not
answer = scnr.next();
} while (answer.equals("y") || answer.equals("Y"));
if (!answer.equals("Y")) {
if (amountWon < 0) { //Print statement for when player is in the hold
System.out.println("You ended losing " + amountWon +" dollars" + ", but people always stop before they hit it big!");
}
else { // Print statement for when player is in positive
System.out.println("You ended with $" + amountWon + " come back again!");
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire