forked from meghasharma123/java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LotteryGame
212 lines (177 loc) · 6.43 KB
/
LotteryGame
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.util.*;
public class LotteryGameMain {
public static void main(String[] args) {
/*
* GENERATE UNIQUE RANDOM NUMBERS The program generate 10 unique random numbers
* using the Random class
*/
Random rand = new Random();
int[] arrayRandNumbers = new int[10];
for (int i = 0; i < arrayRandNumbers.length; i++) {
int randNumber = generateRandomNumber(rand);
if (!isDuplicate(arrayRandNumbers, randNumber)) {
arrayRandNumbers[i] = randNumber;
} else {
i--;
}
}
System.out.println("Welcome to the new *Hacktoberfest* LOTTERY GAME!"
+ "\n The lottery is divided in three games, are you ready to start?");
System.out.println("press any letter and enter to continue");
Scanner scan = new Scanner(System.in);
System.out.println(" GAME 1) BONUS GAME: test your luck."
+ "\nfour prizes will be drawn randomly. If two prizes match, you win!");
/*
* BONUS GAME the program display four prizes sum that the user can win to the
* additional lottery winning the sums are generated based on specific
* probabilities if the same sum appears more than once the user wins that sum
*/
int[] arrayBonus = new int[4];
System.out.println("\n" + "BONUS NUMBERS.");
for (int i = 0; i < arrayBonus.length; i++) { // generate random numbers that will determine our probabilities
float chance = rand.nextFloat() * 100;
if (chance < 60.0) { // the sum 10 has 60% chance that will be displayed
arrayBonus[i] = 10;
System.out.println(arrayBonus[i] + " euro");
} else if (chance < 80.0) { // the sum 20 has 20% chance that will be displayed
arrayBonus[i] = 20;
System.out.println(arrayBonus[i] + " euro");
} else if (chance < 90.0) { // the sum 50 has 10% chance that will be displayed
arrayBonus[i] = 50;
System.out.println(arrayBonus[i] + " euro");
} else if (chance < 97.5) { // the sum 200 has 7.5% chance that will be displayed
arrayBonus[i] = 200;
System.out.println(arrayBonus[i] + " euro");
} else { // the sum 1000 will be displayed if the remaining 2.5% is generated (between
// 97.5 and 100)
arrayBonus[i] = 1000;
System.out.println(arrayBonus[i] + " euro\n");
}
}
int sumBonus = 0;
for (int i = 0; i < arrayBonus.length; i++) {
for (int j = i + 1; j < arrayBonus.length; j++) {
if ((arrayBonus[i] == arrayBonus[j]) && (arrayBonus[i] != sumBonus)) {
sumBonus += arrayBonus[i];
}
}
}
if (sumBonus == 0) {
System.out.println("\nToday is not your day. You won: 0 euro");
} else {
System.out.println("\nYou are super lcuky today!! \n*****************\n You won " + sumBonus
+ " euro\n*****************");
}
System.out.println(
"\n\rGAME 2) GUESS WINNING NUMBERS \n Try to guess 5 out of the 10 winning numbers that will be drawn later in the game");
/*
* USER INTERACTION The user is asked to enter 5 numbers if the input number was
* already entered the program ignores it and ask to enter a new number
*/
System.out.println("Enter 5 lucky numbers");
System.out.println("\nwinning numbers");
int[] userArray = new int[5];
for (int i = 0; i < userArray.length; i++) {
System.out.print((i + 1) + "> Enter " + " here: ");
int userInput = scan.nextInt();
userArray[i] = userInput;
}
int numbMatch = countMatches(userArray, arrayRandNumbers);
System.out.println("\nLucky numbers");
displayArray(userArray);
System.out.println("\nwinning numbers");
displayArray(arrayRandNumbers);
//MATCH NUMBERS
/*
* WINNING PRIZES DEPENDING ON HOW MANY MATCHES depending on how many numbers
* the user matched, the user wins the following prizes
*/
System.out.println("\n\r" + "NUMBERS MATCHED");
int sumMatch = 0;
switch (numbMatch) {
case 0:
System.out.println("You didn't match any of the lottery numbers. You will be more lucky next time!");
break;
case 1:
sumMatch = 100;
System.out.println("Total numbers matched: " + numbMatch + ". Incredible! You won " + sumMatch + " euro");
break;
case 2:
sumMatch = 200;
System.out.println("Total numbers matched: " + numbMatch + ". Incredible! You won " + sumMatch + " euro");
break;
case 3:
sumMatch = 300;
System.out.println("Total numbers matched: " + numbMatch + ". Incredible! You won " + sumMatch + " euro");
break;
case 4:
sumMatch = 400;
System.out.println("Total numbers matched: " + numbMatch + ". Incredible! You won " + sumMatch + " euro");
break;
case 5:
sumMatch = 500;
System.out.println("Total numbers matched: " + numbMatch + ". Incredible! You won " + sumMatch + " euro");
break;
}
System.out.println(
"\n\r3) GUESS SUM of WINNING GAME \nlet's test your mathematical skills. Hei, do not cheat! Try to do this without calculator.");
/*
* USER GUESS THE SUM OF THE INITIAL 10 RANDOM NUMBERS the program checks if the
* sum entered by the user is equal to the sum of all random numbers
*/
System.out.print("Guess what is the sum of the winning numbers: ");
int guessSum = 0;
int randNumbSum = sumNumbers(arrayRandNumbers);
guessSum = scan.nextInt();
if (randNumbSum == guessSum) {
System.out.println("You guessed the the total sum of the winning numbers.Congrats, you won");
} else {
System.out.println("Nice try!. The sum was " + randNumbSum);
}
System.out.println("\n Thank you for playing with me");
System.out.println("Made with <3 by Deborah");
}
/* METHOD: generate random integer number */
public static int generateRandomNumber(Random rand) {
return rand.nextInt(99) + 1;
}
/* METHOD: checks if random number is in the array */
public static boolean isDuplicate(int[] array, int randNumber) {
boolean isDuplicate = true;
for (int i = 0; i < array.length; i++) {
if (array[i] == randNumber) {
isDuplicate = true;
break;
} else {
isDuplicate = false;
}
}
return isDuplicate;
}
/* METHOD : display winning numbers (random numbers */
public static void displayArray(int[] array) {
for (int numbers : array) {
System.out.print(numbers + " ");
}
}
/* METHOD: sum the winning numbers */
public static int sumNumbers(int[] array) {
int sum = 0;
for (int number : array) {
sum += number;
}
return sum;
}
/* METHOD: check matches */
public static int countMatches(int[] luckyArray, int[] winArray) {
int matches = 0;
for (int i = 0; i < winArray.length; i++) {
for (int j = 0; j < luckyArray.length; j++) {
if (winArray[i] == luckyArray[j]) {
matches++;
}
}
}
return matches;
}
}