-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
310 lines (295 loc) · 8.74 KB
/
Copy pathMain.java
File metadata and controls
310 lines (295 loc) · 8.74 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Define stacks
Stack s1 = new Stack(24);
Stack temps1 = new Stack (24);
Stack s2 = new Stack(24);
Stack temps2 = new Stack (24);
Stack s3 = new Stack(24);
Stack temps3 = new Stack (24);
Stack n1 = new Stack(4);
Stack n2 = new Stack(4);
Stack n3 = new Stack(4);
Stack n4 = new Stack(4);
Stack n5 = new Stack(4);
Stack n6 = new Stack(4);
Random rnd = new Random();
Scanner scn = new Scanner(System.in);
int number;
int book1 = 0, book2 = 0, turn = 1;
String ask = "You ask";
//Number stacks
for (int i = 0; i < 4; i++) n1.push(1);
for (int i = 0; i < 4; i++) n2.push(2);
for (int i = 0; i < 4; i++) n3.push(3);
for (int i = 0; i < 4; i++) n4.push(4);
for (int i = 0; i < 4; i++) n5.push(5);
for (int i = 0; i < 4; i++) n6.push(6);
// Assign number randomly for table stack(s3)
while(!(s3.isFull())) {
number = rnd.nextInt(6) + 1;
if (!(n1.isEmpty()) &&(number == (int)n1.peek())) {s3.push(n1.pop());}
if (!(n2.isEmpty()) &&(number == (int)n2.peek())) {s3.push(n2.pop());}
if (!(n3.isEmpty()) &&(number == (int)n3.peek())) {s3.push(n3.pop());}
if (!(n4.isEmpty()) &&(number == (int)n4.peek())) {s3.push(n4.pop());}
if (!(n5.isEmpty()) &&(number == (int)n5.peek())) {s3.push(n5.pop());}
if (!(n6.isEmpty()) &&(number == (int)n6.peek())) {s3.push(n6.pop());}
}
//Cards are dealt to the players
for (int i = 0; i < 7; i++) {s1.push(s3.pop());}
for (int i = 0; i < 7; i++) {s2.push(s3.pop());}
boolean isFound = true, randomcheck = true, inputcheck = true, firstcheck = true;
int spacecount1 = 15, spacecount2 = 15;
//GAME CIRCUIT
while(!s1.isEmpty() && !s2.isEmpty()) {
// Designing the menu
System.out.println("Turn: " + turn + " Table");
System.out.print("You: ");
while (!s1.isEmpty()) {
System.out.print(s1.peek() + " ");
temps1.push(s1.pop());
}
while (!(temps1.isEmpty())) s1.push(temps1.pop());
for (int i = 0; i < spacecount1; i++) {
System.out.print(" ");
}
System.out.print("book: " + book1 + " ");
while(!(s3.isEmpty())) {
System.out.print(s3.peek() + " ");
temps3.push(s3.pop());
}
while (!temps3.isEmpty()) s3.push(temps3.pop());
System.out.println();
System.out.print("Computer: ");
while (!s2.isEmpty()) {
System.out.print(s2.peek() + " ");
temps2.push(s2.pop());
}
while(!temps2.isEmpty()) s2.push(temps2.pop());
for (int i = 0; i < spacecount2; i++) {
System.out.print(" ");
}
System.out.println("book: " + book2);
System.out.println();
int input = 0;
int count = 0;
int checknumber = 0;
inputcheck = true;
if (isFound == true) {
//First checking for player
while(!s1.isEmpty()) {
checknumber = (int)s1.peek();
while (!s1.isEmpty()) {
if ((int)s1.peek() == checknumber) {
count++;
}
temps1.push(s1.pop());
}
while (!(temps1.isEmpty())) s1.push(temps1.pop());
if (count == 4) {
System.out.print("Book is created >> ");
for (int i = 0; i < 4; i++) {System.out.print(checknumber + " ");}
System.out.println();
while (!s1.isEmpty()) {
if ((int)s1.peek() == checknumber) {
s1.pop();
spacecount1+=2;
}
else {
temps1.push(s1.pop());
}
}
while (!(temps1.isEmpty())) s1.push(temps1.pop());
book1++;
break;
}
temps2.push(s1.pop());
count = 0;
}
while (!(temps2.isEmpty())) s1.push(temps2.pop());
// input checking
while(inputcheck) {
System.out.print(ask + ": ");
input = scn.nextInt();
while(!s1.isEmpty()) {
if (input == (int)s1.peek()) {
inputcheck = false;
}
temps1.push(s1.pop());
}
if (inputcheck == true) {
System.out.println(" (You can't ask number that you do not have!)");
}
while(!temps1.isEmpty()) s1.push(temps1.pop());
}
System.out.println();
// Transferring cards after input checking
isFound = false;
ask = "Computer ask";
while(!s2.isEmpty()) {
if ((int)s2.peek() == input) {
s1.push(s2.pop());
spacecount1-=2;
spacecount2+=2;
isFound = true;
ask = "You ask";
}
else {
temps2.push(s2.pop());
}
}
while(!temps2.isEmpty()) s2.push(temps2.pop());
//Go Fish
if (isFound == false) {
System.out.println("Computer says “Go Fish”");
s1.push(s3.pop());
spacecount1-=2;
}
// Check book
count = 0;
while(!s1.isEmpty()) {
if ((int)s1.peek() == input) {
count++;
}
temps1.push(s1.pop());
}
while (!(temps1.isEmpty())) s1.push(temps1.pop());
if (count == 4) {
System.out.print("Book is created >> ");
for (int i = 0; i < 4; i++) {System.out.print(input + " ");}
while (!s1.isEmpty()) {
if ((int)s1.peek() == input) {
s1.pop();
spacecount1+=2;
}
else {
temps1.push(s1.pop());
}
}
while (!(temps1.isEmpty())) s1.push(temps1.pop());
book1++;
}
System.out.println();
System.out.println("****************************************************************************");
turn++;
}
else {
//First checking for computer
while(!s2.isEmpty()) {
checknumber = (int)s2.peek();
while (!s2.isEmpty()) {
if ((int)s2.peek() == checknumber) {
count++;
}
temps2.push(s2.pop());
}
while (!(temps2.isEmpty())) s2.push(temps2.pop());
if (count == 4) {
System.out.println();
System.out.print("Book is created >> ");
for (int i = 0; i < 4; i++) {System.out.print(checknumber + " ");}
System.out.println();
while (!s2.isEmpty()) {
if ((int)s2.peek() == checknumber) {
s2.pop();
spacecount2+=2;
}
else {
temps2.push(s2.pop());
}
}
while (!(temps2.isEmpty())) s2.push(temps2.pop());
book2++;
break;
}
temps1.push(s2.pop());
count = 0;
}
while (!(temps1.isEmpty())) s2.push(temps1.pop());
// Random number for computer
randomcheck = true;
input = 0;
while(randomcheck) {
input = rnd.nextInt(6) + 1;
while(!s2.isEmpty()) {
if (input == (int)s2.peek()) {
randomcheck = false;
}
temps2.push(s2.pop());
}
while(!temps2.isEmpty()) s2.push(temps2.pop());
}
System.out.print(ask + ": " + input);
// Transferring cards after random number checking
isFound = true;
ask = "You ask";
while(!s1.isEmpty()) {
if ((int)s1.peek() == input) {
s2.push(s1.pop());
spacecount1+=2;
spacecount2-=2;
isFound = false;
ask = "Computer ask";
}
else {
temps1.push(s1.pop());
}
}
while(!temps1.isEmpty()) s1.push(temps1.pop());
//Go Fish
if (isFound == true) {
System.out.println();
System.out.println("You says “Go Fish”");
s2.push(s3.pop());
spacecount2-=2;
}
// Check book
while(!s2.isEmpty()) {
if ((int)s2.peek() == input) {
count++;
}
temps2.push(s2.pop());
}
while (!(temps2.isEmpty())) s2.push(temps2.pop());
if (count == 4) {
System.out.println();
System.out.print("Book is created >> ");
for (int i = 0; i < 4; i++) {System.out.print(input + " ");}
while (!s2.isEmpty()) {
if ((int)s2.peek() == input) {
s2.pop();
spacecount2+=2;
}
else {
temps2.push(s2.pop());
}
}
while (!(temps2.isEmpty())) s2.push(temps2.pop());
book2++;
}
System.out.println();
System.out.println();
System.out.println("****************************************************************************");
turn++;
}
}
//End of the game
if (book1 > book2) {
System.out.println();
System.out.println("WINNER: YOU");
System.out.println("SCORE IS: " + book1 +" - " + book2);
}
else if (book1 < book2){
System.out.println();
System.out.println("WINNER: COMPUTER");
System.out.println("SCORE IS: " + book1 +" - " + book2);
}
else if (book1 == book2) {
System.out.println();
System.out.println("THERE IS NO WINNER");
System.out.println("SCORE IS: " + book1 +" - " + book2);
}
}
}