Repository navigation
Expand file tree
/
Copy pathLesson38_slotMachine.java
More file actions
126 lines (103 loc) · 4.51 KB
/
Copy pathLesson38_slotMachine.java
File metadata and controls
126 lines (103 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Random;
import java.util.Scanner;
public class Lesson38_slotMachine {
static Scanner sc = new Scanner(System.in);
static Random random = new Random();
static final String[] scatterSymbols = {"🍒", "🍉", "🍋", "🛎️", "⭐️"};
public static void main(String[] args) {
System.out.println("Welcome to the Slot Machine!");
System.out.println("To begin, enter an amount you wish to add into your balance!");
double balance = getBalance();
String playing = "yes";
do {
if(balance <= 0) {
System.out.println("You've run out of money! You'll need to add more to continue playing!");
balance = getBalance();
}
System.out.println("Enter an amount you wish to bet!");
double bet = getBet(balance);
balance = spinMachine(balance, bet);
System.out.print("Do you want to test your luck again? (yes/no): " );
playing = sc.nextLine().toLowerCase();
System.out.println();
} while (playing.equals("yes"));
System.out.println("Thank you for playing!");
sc.close();
}
static double getBalance() {
System.out.print("Enter an amount to add into your balance: +$");
double balance = Double.parseDouble(sc.nextLine());
System.out.println();
// validates user input to be positive
while(balance <= 0) {
System.out.println("Please enter an appropriate response!");
System.out.print("Enter an amount to add into your balance: +$");
balance = Double.parseDouble(sc.nextLine());
System.out.println();
}
return balance;
}
static double getBet(double balance) {
System.out.print("Enter bet amount: -$");
double bet = Double.parseDouble(sc.nextLine());
System.out.println();
// validates user input to be positive and lower than set balance
while(bet < 0 || bet > balance) {
System.out.println("Please enter an appropriate response!");
System.out.print("Enter bet amount: -$");
bet = Double.parseDouble(sc.nextLine());
System.out.println();
}
return bet;
}
static double spinMachine(double balance, double bet) {
balance -= bet;
System.out.println("Remaining Balance: $" + balance);
System.out.println();
String[] slotMachine = {scatterSymbols[random.nextInt(scatterSymbols.length)],
scatterSymbols[random.nextInt(scatterSymbols.length)],
scatterSymbols[random.nextInt(scatterSymbols.length)]};
displaySlot(slotMachine);
double payout = getPayout(slotMachine, bet);
balance += payout;
System.out.println("Remaining Balance: $" + balance);
return balance;
}
static void displaySlot(String[] slotMachine) {
// displays slot machine
System.out.println("=============");
for (String symbol : slotMachine) {
System.out.print("| " + symbol + " ");
}
System.out.print("|\n");
System.out.println("=============");
System.out.println();
}
static double getPayout(String[] slotMachine, double bet) {
// checks if symbols generated match
int matchMultiplier = 0;
int matchIndex = 0;
for(int i = 0; i < (slotMachine.length - 1); i++) {
if(slotMachine[i].equals(slotMachine[i + 1])) {
matchMultiplier++;
matchIndex = i;
}
}
// calculates gross payout
double payout = 0;
if(matchMultiplier > 0) {
switch(slotMachine[matchIndex]) {
case "🍒" -> payout = bet * 3.0 * ((double) matchMultiplier/(slotMachine.length - 1));
case "🍉" -> payout = bet * 4.0 * ((double) matchMultiplier/(slotMachine.length - 1));
case "🍋" -> payout = bet * 5.0 * ((double) matchMultiplier/(slotMachine.length - 1));
case "🛎️" -> payout = bet * 10.0 * ((double) matchMultiplier/(slotMachine.length - 1));
case "⭐️" -> payout = bet * 20.0 * ((double) matchMultiplier/(slotMachine.length - 1));
}
System.out.println("Congratulations! You won +$" + (payout - bet) + "!"); // displays net earnings
}
else {
System.out.println("Unfortunately, you lost -$" + bet + ".");
}
return payout;
}
}