-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlotMachine.java
More file actions
127 lines (96 loc) · 3.35 KB
/
SlotMachine.java
File metadata and controls
127 lines (96 loc) · 3.35 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
package LearningWithExercises;
import java.util.Random;
import java.util.Scanner;
public class SlotMachine {
public static void main(String [] args){
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 java slot");
System.out.println("Symbols: 🍒 🍉 🍋 🔔 ⭐ ");
System.out.println("***************************");
while (balance > 0){
System.out.println("Current balance $"+balance);
System.out.println("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 must be greater than 0");
continue;
}else {
balance-=bet;
}
System.out.println("Spinning... ");
row=spinRow();
printRow(row);
payout= getPayout(row,bet);
if(payout>0){
System.out.println("you won Sh."+payout);
balance+=payout;
}
else {
System.out.println("Sorry you lost");
}
System.out.println("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: Sh."+balance );
scanner.close();
}
static int getPayout(String[] row, 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])) {
return switch (row[0]){
case "🍒" ->bet*2;
case "🍉" ->bet*3;
case "🍋" ->bet*4;
case "🔔" ->bet*5;
case "⭐" ->bet*10;
default -> 0;
};
}
else if (row[1].equals(row[2])) {
return switch (row[1]){
case "🍒" ->bet*2;
case "🍉" ->bet*3;
case "🍋" ->bet*4;
case "🔔" ->bet*5;
case "⭐" ->bet*10;
default -> 0;
};
}
return 0;
}
static String [] spinRow(){
String [] symbols={"🍒","🍉","🍋","🔔", "⭐"};
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));
}
}