forked from Subhasish-Behera/GAMES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdup.java
352 lines (319 loc) · 10.6 KB
/
dup.java
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//package com.greendoor;
/*
/*String [] myStrings = {
"alpha",
"beta",
"gamma",
"delta"
};
for(String currentString : myStrings) {
System.out.println(currentString);
}
*/
import java.util.*;
import java.util.Scanner;
public class dup{
//declaring global stuff for all functions
static ArrayList<Integer> player1pos = new ArrayList<Integer>();
static ArrayList<Integer> player2pos = new ArrayList<Integer>();
//real matrix to be used in algo
static char[][] m2DArray = new char[3][3];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char Hash[][] = {{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '}};
//Random rand = new Random();
//initializing real matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m2DArray[i][j] = ' ';
}
}
//showboard(Hash);
RULES();
while (true) {
System.out.println("PLAYER 1,Enter your placement (1-9):");
int pos1 = in.nextInt();
placing(pos1, Hash, 1, m2DArray);
// setrealmatrix(pos1, 1, m2DArray);
checkWinner2(m2DArray);
if (checkWinner2(m2DArray) == true) {
if ((player1pos.size() + player2pos.size()) != 9) {
System.out.println("player 1 won");
} else {
System.out.println("The game is tied ");
}
break;
}
//int pos2= rand.nextInt(9)+1;
System.out.println("PLAYER 2,Enter your placement (1-9):");
int pos2 = in.nextInt();
// System.out.println(pos1);
// placing(pos1,Hash,1);
// checkwinner();
placing(pos2, Hash, 2, m2DArray);
// setrealmatrix(pos2, 2, m2DArray);
checkWinner2(m2DArray);
if (checkWinner2(m2DArray) == true) {
System.out.println("player 2 won");
break;
}
}
}
public static void showboard(char[][] Hash) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(Hash[i][j]);
}
System.out.println();
}
}
public static void RULES() {
System.out.println("INTRODUCTION");
System.out.println("GAME BOARD");
System.out.println("1|2|3)");
System.out.println("-+-+-");
System.out.println("4|5|6");
System.out.println("-+-+-");
System.out.println("7|8|9");
System.out.println("******");
}
public static void placing(int pos, char[][] Hash, int player, char[][] m2DArray) {
char symbol;
if (player == 1) {
symbol = 'X';
player1pos.add(pos);
} else {
symbol = 'O';
player2pos.add(pos);
}
switch (pos) {
case 1:
Hash[0][0] = symbol;
m2DArray[0][0] = symbol;
break;
case 2:
Hash[0][2] = symbol;
m2DArray[0][1] = symbol;
break;
case 3:
Hash[0][4] = symbol;
m2DArray[0][2] = symbol;
break;
case 4:
Hash[2][0] = symbol;
m2DArray[1][0] = symbol;
break;
case 5:
Hash[2][2] = symbol;
m2DArray[1][1] = symbol;
break;
case 6:
Hash[2][4] = symbol;
m2DArray[1][2] = symbol;
break;
case 7:
Hash[4][0] = symbol;
m2DArray[2][0] = symbol;
break;
case 8:
Hash[4][2] = symbol;
m2DArray[2][1] = symbol;
break;
case 9:
Hash[4][4] = symbol;
m2DArray[2][2] = symbol;
break;
}
showboard(Hash);
}
public static boolean checkwinner() {
if ((player1pos.size() + player2pos.size()) == 9) {
System.out.println("The game is tied ");
return true;
}
List zerorow = Arrays.asList(1, 2, 3);
List onerow = Arrays.asList(4, 5, 6);
List tworow = Arrays.asList(7, 8, 9);
List zerocol = Arrays.asList(1, 4, 7);
List onecol = Arrays.asList(2, 5, 8);
List twocol = Arrays.asList(3, 6, 9);
List dig1 = Arrays.asList(1, 5, 9);
List dig2 = Arrays.asList(7, 5, 3);
List<List> winning = new ArrayList<List>();
winning.add(zerorow);
winning.add(onerow);
winning.add(tworow);
winning.add(zerorow);
winning.add(onecol);
winning.add(twocol);
winning.add(dig1);
winning.add(dig2);
for (List l : winning) {
if (player1pos.containsAll(l)) {
System.out.println("player 1 won");
return true;
} else if (player2pos.containsAll(l)) {
System.out.println("player 2 won");
return true;
}
}
return false;
}
public static void setrealmatrix(int pos, int player, char[][] matrix) {
char symbol;
if (player == 1) {
symbol = 'X';
player1pos.add(pos);
} else {
symbol = 'O';
player2pos.add(pos);
}
switch (pos) {
case 1:
matrix[0][0] = symbol;
break;
case 2:
matrix[0][1] = symbol;
break;
case 3:
matrix[0][2] = symbol;
break;
case 4:
matrix[1][0] = symbol;
break;
case 5:
matrix[1][2] = symbol;
break;
case 6:
matrix[1][2] = symbol;
break;
case 7:
matrix[2][0] = symbol;
break;
case 8:
matrix[2][1] = symbol;
break;
case 9:
matrix[2][2] = symbol;
}
}
public static void bestmove(char[][] m2DArray) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
}
}
}
public static void bestscore(char[][] m2DArray) {
// AI to make its turn
int bestScore = Integer.MIN_VALUE;
int[] move = {0, 0};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Is the spot available?
if (m2DArray[i][j] == ' ') {
m2DArray[i][j] = 'X';
int score = minimax(m2DArray, 0, false);
m2DArray[i][j] = ' ';
if (score > bestScore) {
bestScore = score;
move[0] = i;
move[1] = j;
}
}
}
}
m2DArray[move[0]][move[1]] = 'X';
//currentPlayerIsHuman = true;
}
public static int minimax(char[][] m2DArray, int depth, boolean isMaximizing) {
int utility;
if (checkWinner2(m2DArray) == true) {
if (isMaximizing == true) {
utility = (1) * (sizeof2Darray(m2DArray) - 1);
} else {
utility = (-1) * (sizeof2Darray(m2DArray) - 1);
}
return utility;
}
if (isMaximizing) {
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Is the spot available?
if (m2DArray[i][j] == ' ') {
m2DArray[i][j] = 'X';
int score = minimax(m2DArray, depth + 1, false);
m2DArray[i][j] = ' ';
bestScore = Math.max(score, bestScore);
}
}
}
return bestScore;
} else {
int bestScore = Integer.MAX_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Is the spot available?
if (m2DArray[i][j] == ' ') {
m2DArray[i][j] = 'O';
int score = minimax(m2DArray, depth + 1, true);
m2DArray[i][j] = ' ';
bestScore = Math.min(score, bestScore);
}
}
}
return bestScore;
}
}
public static int sizeof2Darray(char[][] m2DArray) {
int count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Is the spot available?
if (m2DArray[i][j] == ' ') {
count++;
}
}
}
return count;
}
public static boolean checkWinner2(char[][] m2DArray) {
if ((player1pos.size() + player2pos.size()) == 9) {
System.out.println("The game is tied ");
return true;
}
char a = m2DArray[0][0];
char b = m2DArray[1][1];
char c = m2DArray[2][2];
char d = m2DArray[0][2];
char e = m2DArray[2][0];
for (int i = 0; i < 3; i++) {
char x = m2DArray[i][0];
char y = m2DArray[i][1];
char z = m2DArray[i][2];
if ((x == y && y == z && z == x) && (x != ' ') && (y != ' ') && (z != ' ')) {
return true;
}
}
// Vertical
for (int i = 0; i < 3; i++) {
char p = m2DArray[0][i];
char q = m2DArray[1][i];
char r = m2DArray[2][i];
if ((p == q && q == r && p == r) && (p != ' ') && (q != ' ') && (r != ' ')) {
return true;
}
}
// Diagonal
if (((a == b && b == c) && (a != ' ') && (b != ' ') && (c != ' ')) || ((d == b && b == e) && (d != ' ') && (b != ' ') && (e != ' '))) {
return true;
}
return false;
}
public static void checkWinner(char[][] m2DArray) {
}
}