-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlotMachine.java
More file actions
107 lines (85 loc) · 3.2 KB
/
SlotMachine.java
File metadata and controls
107 lines (85 loc) · 3.2 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
import java.util.Random;
import java.util.Scanner;
public class SlotMachine {
public static void main(String[] args) {
// Slot Machine
Scanner scanner = new Scanner(System.in);
int balance = 100;
int bet;
int payout;
String[] row = {};
String playAgain;
System.out.println("*****************************");
System.out.println("Welcome to the Slot Machine!");
System.out.println("Symbols: 🍒 🔔 🍉 🍋 ⭐ ");
System.out.println("*****************************");
while(balance > 0) {
System.out.println("Current balance: $" + balance);
System.out.print("Place your bet amount: ");
bet = scanner.nextInt();
scanner.nextLine();
if(bet > balance) {
System.out.println("Insufficient funds!");
continue;
} else if (bet <= 0) {
System.out.println("Bet amount cannot be less than zero.");
continue;
} else {
balance -= bet;
}
System.out.println("Spinning...");
row = spinRow();
printRow(row);
payout = getPayout(row, bet);
if(payout > 0) {
System.out.println("You won " + payout);
balance += payout;
} else {
System.out.println("Sorry, you lost this round.");
}
System.out.print("Do you want to play again? (Y/N): ");
playAgain = scanner.nextLine().toUpperCase();
if(!playAgain.equals("Y")) {
break;
}
}
System.out.println("Game Over! Your final balance is $" + balance);
scanner.close();
}
static String[] spinRow() {
String[] symbols = {":cherry:🍒", ":bell:🔔", ":watermelon:🍉", ":lemon:🍋", ":star:⭐"};
String[] row = new String[3];
Random random = new Random();
for(int i = 0; i < 3; i++) {
row[i] = symbols[random.nextInt(symbols.length)];
}
return row;
}
static void printRow(String[] row) {
System.out.println("*****************************");
System.out.println(String.join(" | ", row));
System.out.println("*****************************");
}
static int getPayout(String[] row, int bet) {
if(row[0].equals(row[1]) && row[1].equals(row[2])) {
return switch(row[0]) {
case ":cherry:🍒" -> bet * 3;
case ":bell:🔔" -> bet * 10;
case ":watermelon:🍉" -> bet * 4;
case ":lemon:🍋" -> bet * 5;
case ":star:⭐" -> bet * 20;
default -> 0;
};
} else if(row[0].equals(row[1]) || row[1].equals(row[2])) {
return switch(row[1]) {
case ":cherry:🍒" -> bet * 2;
case ":bell:🔔" -> bet * 5;
case ":watermelon:🍉" -> bet * 3;
case ":lemon:🍋" -> bet * 4;
case ":star:⭐" -> bet * 10;
default -> 0;
};
}
return 0;
}
}