-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlotMachineProgram.java
More file actions
156 lines (135 loc) · 5.51 KB
/
SlotMachineProgram.java
File metadata and controls
156 lines (135 loc) · 5.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.Random;
import java.util.Scanner;
public class SlotMachineProgram {
// Array of symbols
static String[] slotSymbols = {"🍋", "🤑", "⭐️", "🍉", "🍒"};
static String[] slotRow = new String[3];
static double balance = 100;
static double userBet;
public static void main(String[] args) {
// JAVA SLOT MACHINE PROGRAM
// Declare variables
Scanner scanner = new Scanner(System.in);
boolean continuePlaying = true;
char response;
double payout;
// Welcome menu - show symbols + show current balance
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(" $ Welcome to JAVA SLOTS - Test Your Luck! $");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
System.out.print("Symbols: ");
for (String slotSymbol : slotSymbols) {
System.out.print(slotSymbol + " "); // Displaying the symbols
}
System.out.println();
while (continuePlaying) {
// Shows current balance of user - make sure player is prompted for valid input
placeBets(scanner);
// Slot machine mechanics:
System.out.println("Spinning...");
System.out.println("****************");
spinSlotMachine(); // Randomly spins for three symbols
System.out.println("****************");
// Check to see for matching symbols and see what player wins
payout = getPayout(slotRow, userBet); // getPayout method checks for matching symbols
balance = balance + payout; // Update the balance
// Output if user won or lost
if (payout > 0) {
System.out.println("You won: $" + payout);
}
else {
System.out.println("Sorry you lost this round");
}
// Play again?
do {
System.out.print("Do you like to spin again? (y/n): ");
response = scanner.nextLine().toLowerCase().charAt(0);
if (response != 'y' && response != 'n') {
System.out.println("INVALID ANSWER! Please enter 'y' for YES or 'n' for NO");
}
} while (response != 'y' && response != 'n');
continuePlaying = (response == 'y');
if (!continuePlaying) {
System.out.println("GAME OVER! Your final balance is $" + balance);
}
}
scanner.close();
}
static void placeBets(Scanner scanner) {
do {
System.out.printf("Current Balance: $%.2f\n", balance);
// Bets - if user loses, they lose that money, if they win, they gain that money
System.out.print("Place your bets ($): ");
userBet = scanner.nextDouble();
scanner.nextLine(); // Clear buffer
// Verify your bet (needs to be less than or equal to balance but greater than 0)
if (userBet > balance) {
System.out.println("INSUFFICIENT FUNDS! You don't have that much money.");
}
else if (userBet <= 0) {
System.out.println("INVALID NUMBER! Bets must be greater than 0.");
}
} while (userBet > balance || userBet <= 0);
// If bet is made, make sure to subtract it from balance
balance -= userBet;
}
static void spinSlotMachine() {
// Declare variables
Random random = new Random();
int symbolIndex;
for (int i = 0; i < 3; i++) {
symbolIndex = random.nextInt(5);
slotRow[i] = slotSymbols[symbolIndex];
}
// Output the 3 randomly chosen symbols
System.out.println(" " + String.join(" | ", slotRow));
}
static double getPayout(String[] slotRow, double userBet) {
// If all three symbols match
if (slotRow[0].equals(slotRow[1]) && slotRow[1].equals(slotRow[2])) {
return switch(slotRow[0]) {
case "🍒" -> userBet * 3;
case "🍉" -> userBet * 4;
case "🍋" -> userBet * 5;
case "🤑" -> userBet * 10;
case "⭐️" -> userBet * 20;
default -> 0;
};
} // If first two symbols match
else if (slotRow[0].equals(slotRow[1])) {
return switch (slotRow[0]) {
case "🍒" -> userBet * 2;
case "🍉" -> userBet * 3;
case "🍋" -> userBet * 4;
case "🤑" -> userBet * 5;
case "⭐️" -> userBet * 10;
default -> 0;
};
} // If last two symbols match
else if (slotRow[1].equals(slotRow[2])) {
return switch (slotRow[1]) {
case "🍒" -> userBet * 2;
case "🍉" -> userBet * 3;
case "🍋" -> userBet * 4;
case "🤑" -> userBet * 5;
case "⭐️" -> userBet * 10;
default -> 0;
};
}
return 0;
}
// This is a prototype method I tried to do
// static void SlotMachine() {
//
// for (int i = 0; i < 3; i++) {
// slotIndex = random.nextInt(5);
// String slotSymbol = slotSymbols[slotIndex];
// if (i == 2) {
// System.out.print(slotSymbol);
// break;
// }
// System.out.print(slotSymbol + " | ");
// }
// System.out.println();
// }
}