-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
121 lines (106 loc) Β· 4.85 KB
/
Main.java
File metadata and controls
121 lines (106 loc) Β· 4.85 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
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int balance = 2000;
int bet;
int cashout;
String[] row;
System.out.println("***************************************");
System.out.println("Welcome to Java Slot Betting Machine");
System.out.println("***************************************");
System.out.println("Symbols: π π π π β");
boolean continueGame = true; // Flag to control whether the game continues
while (balance > 0 && continueGame) {
System.out.println("Current balance:$ " + balance);
System.out.println("Enter your betting Amount: ");
// Handle invalid input for bet amount
while (true) {
if (scanner.hasNextInt()) {
bet = scanner.nextInt();
scanner.nextLine(); // To consume the newline character
if (bet <= 0) {
System.out.println("Bet must be greater than $0");
} else if (bet > balance) {
System.out.println("INSUFFICIENT FUNDS! Please deposit or lower bet amount.");
} else {
balance -= bet;
break; // Exit the loop once a valid bet is entered
}
} else {
System.out.println("Invalid input! Please enter a valid number for the bet.");
scanner.nextLine(); // Consume the invalid input
}
}
System.out.println("Spinning...");
row = Spin();
Printrow(row);
cashout = (int) getcashout(row, 0, bet);
if (cashout > 0) {
System.out.println("You won $" + cashout);
balance += cashout;
} else {
System.out.println("Sorry, you lost this round.");
}
while (true) {
// Prompt the user with two options
System.out.println("Do you want to continue? (yes/no): ");
String userInput = scanner.nextLine().toLowerCase(); // Convert to lowercase to make it case-insensitive
// Check if the input is either "yes" or "no"
if (userInput.equals("yes")) {
System.out.println("You chose 'Yes'. Continuing the game...");
break; // Exit the loop and continue with the game
} else if (userInput.equals("no")) {
System.out.println("You chose 'No'. Exiting the game...");
continueGame = false; // Set the flag to false to exit the outer loop
break; // Exit the loop
} else {
// Invalid input, prompt the user again
System.out.println("Invalid input. Please enter 'yes' or 'no'.");
}
}
}
System.out.println("Game Over! Your Final Balance is $" + balance);
scanner.close(); // Close the scanner at the end of the game
}
// Java slot machine spinning method
static String[] Spin() {
String[] symbol = {"π", "π", "π", "π", "β"}; // The available array of symbols
String[] row = new String[3];
Random random = new Random();
for (int i = 0; i < 3; i++) {
row[i] = symbol[random.nextInt(symbol.length)];
}
return row;
}
// Print the row of symbols
static void Printrow(String[] row) {
System.out.println("***************************************");
System.out.println(" " + String.join(" | ", row)); // Join each element in the row array with " | "
System.out.println("***************************************");
}
// Method to calculate the cashout based on the row
static double getcashout(String[] row, int cashout, int bet) {
if (row[0].equals(row[1]) && row[1].equals(row[2])) {
return switch (row[0]) {
case "π" -> bet * 3;
case "π" -> bet * 4;
case "π" -> bet * 5;
case "π" -> bet * 10;
case "β" -> bet * 20;
default -> 0;
};
} else if (row[0].equals(row[1]) || row[1].equals(row[2]) || row[0].equals(row[2])) {
return switch (row[0]) {
case "π" -> bet * 2;
case "π" -> bet * 3;
case "π" -> bet * 4;
case "π" -> bet * 5;
case "β" -> bet * 10;
default -> 0;
};
}
return 0;
}
}