-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTT.java
355 lines (306 loc) · 11.2 KB
/
TTT.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
353
354
/*Thomas Abreu Dias aka Boya*/
/*Dépot git: https://github.com/ThomasAbreuDias/-Ultimate-Tic-Tac-Toe */
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.HashMap;
//revoir Tree
//la fonction de parcour de ttt
// Class qui représente un grille de morpion
class Grid {
private Case[][] grid;
public static int MYSIGN;
private int actual_sign;
private int turn_counter;
public final static int ROW = 3;
public final static int COL = 3;
/*Constructeur*/
public Grid(){
grid = new Case[Grid.ROW][Grid.COL];
turn_counter = Grid.ROW * Grid.COL;
actual_sign = 1;
for (int i = 0;i < 3 ;i++ ) {
for (int j = 0;j < 3 ;j++ ) {
grid[i][j] = new Case(i,j);
}
}
}
public boolean isTheEnd(){
if(checkWinner() != 0 || this.turn_counter == 0 )
return true;
else
return false;
}
public boolean isPlayable(GridIndex gi){
if(this.grid[gi.getI()][gi.getJ()].getType() == 0)
return true;
return false;
}
public boolean isPlayable(int i, int j){
if(this.grid[i][j].getType() == 0)
return true;
return false;
}
public void setMYSIGN(int sign){
this.MYSIGN = sign;
}
public Case getCase(GridIndex gi){
return this.grid[gi.getI()][gi.getJ()];
}
public void displayGrid(){
for (int i = 0; i < 3; i++ ) {
for (int j = 0; j < 3; j++ ) {
System.out.print("|"+grid[i][j]);
}
System.out.println("|");
}
}
public int getActual_sign(){
return this.actual_sign;
}
public int getTurn(){
return this.grid.length - this.turn_counter;
}
public Grid play(GridIndex gi){
if(this.isPlayable(gi)){
grid[gi.getI()][gi.getJ()].setType(this.actual_sign);
}
this.turn_counter--;
this.checkWinner();
this.changeTurn();
return this;
}
public void changeTurn(){
if(this.turn_counter%2 == 0)
this.actual_sign = 2;
else
this.actual_sign = 1;
}
public int checkWinner() {
if(this.turn_counter <= 0)
System.out.println(this.turn_counter);
for (int i = 0; i < 3; i++) {
// check rows
if (grid[i][0].getType() > 0 && grid[i][0].getType() == grid[i][1].getType() && grid[i][0].getType() == grid[i][2].getType()) {
return grid[i][0].getType();
}
// check cols
if (grid[0][i].getType() > 0 && grid[0][i].getType() == grid[1][i].getType() && grid[0][i].getType() == grid[2][i].getType()) {
return grid[0][i].getType();
}
}
// check diags
if (grid[0][0].getType() > 0 && grid[0][0].getType() == grid[1][1].getType() && grid[0][0].getType() == grid[2][2].getType()) {
return grid[0][0].getType();
}
if (grid[2][0].getType() > 0 && grid[2][0].getType() == grid[1][1].getType() && grid[2][0].getType() == grid[0][2].getType()) {
return grid[2][0].getType();
}
return 0;
}
public int checkneighbor(){
for (int i = 0; i < 3; i++) {
if(grid[i][0].getType() > 0 && grid[i][1].getType() > 0){ // check up and middle of a row (wich row it is depends on i's value)
if(grid[i][0].getType() == grid[i][1].getType())
return (grid[i][1].getType() == Grid.MYSIGN) ? 10 : -10;
}
if(grid[i][1].getType() > 0 && grid[i][2].getType() > 0){ // check middle and down of a row (wich row it is depends on i's value)
if(grid[i][1].getType() == Grid.MYSIGN && grid[i][2].getType() == Grid.MYSIGN)
return 10;
else if(grid[i][1].getType() != Grid.MYSIGN && grid[i][2].getType() != Grid.MYSIGN)
return -10;
}
if(grid[i][0].getType() > 0 && grid[i][2].getType() > 0){ // Pas vraiment cote a cote
if(grid[i][0].getType() == Grid.MYSIGN && grid[i][2].getType() == Grid.MYSIGN)
return 10;
else if(grid[i][0].getType() != Grid.MYSIGN && grid[i][2].getType() != Grid.MYSIGN)
return -10;
}
if(grid[0][i].getType() > 0 && grid[1][i].getType() > 0){ // check left and middle of a column (wich column it is depends on i's value)
if(grid[0][i].getType() == Grid.MYSIGN && grid[1][i].getType() == Grid.MYSIGN)
return 10;
else if(grid[0][i].getType() != Grid.MYSIGN && grid[1][i].getType() != Grid.MYSIGN)
return -10;
}
if(grid[0][i].getType() > 0 && grid[2][i].getType() > 0){ // check middle and right of a column (wich column it is depends on i's value)
if(grid[0][i].getType() == Grid.MYSIGN && grid[2][i].getType() == Grid.MYSIGN)
return 10;
else if(grid[0][i].getType() != Grid.MYSIGN && grid[2][i].getType() != Grid.MYSIGN)
return -10;
}
if(grid[1][i].getType() > 0 && grid[2][i].getType() > 0){ // check left and right of a column (wich column it is depends on i's value)
if(grid[1][i].getType() == Grid.MYSIGN && grid[2][i].getType() == Grid.MYSIGN)
return 10;
else if(grid[1][i].getType() != Grid.MYSIGN && grid[2][i].getType() != Grid.MYSIGN)
return -10;
}
}
if(grid[0][0].getType() > 0 && grid[1][1].getType() > 0){ //up left and middle
if (grid[0][0].getType() == Grid.MYSIGN && grid[1][1].getType() == Grid.MYSIGN)
return 10;
else if(grid[0][0].getType() != Grid.MYSIGN && grid[1][1].getType() != Grid.MYSIGN)
return -10;
}
if(grid[0][0].getType() > 0 && grid[2][2].getType() > 0){//up left and down right
if (grid[0][0].getType() == Grid.MYSIGN && grid[2][2].getType() == Grid.MYSIGN)
return 10;
else if(grid[0][0].getType() != Grid.MYSIGN && grid[2][2].getType() != Grid.MYSIGN)
return -10;
}
if(grid[1][1].getType() > 0 && grid[2][2].getType() > 0){//middle and down right
if (grid[1][1].getType() == Grid.MYSIGN && grid[2][2].getType() == Grid.MYSIGN)
return 10;
else if(grid[1][1].getType() != Grid.MYSIGN && grid[2][2].getType() != Grid.MYSIGN)
return -10;
}
if(grid[2][0].getType() > 0 && grid[1][1].getType() > 0){ //down left and middle
if (grid[2][0].getType() == Grid.MYSIGN && grid[1][1].getType() == Grid.MYSIGN)
return 10;
else if(grid[2][0].getType() != Grid.MYSIGN && grid[1][1].getType() != Grid.MYSIGN)
return -10;
}
if(grid[2][0].getType() > 0 && grid[0][2].getType() > 0){ //down left and up right
if (grid[2][0].getType() == Grid.MYSIGN && grid[0][2].getType() == Grid.MYSIGN)
return 10;
else if(grid[2][0].getType() != Grid.MYSIGN && grid[0][2].getType() != Grid.MYSIGN)
return -10;
}
if(grid[1][1].getType() > 0 && grid[0][2].getType() > 0){ //middle and up right
if (grid[1][1].getType() == Grid.MYSIGN && grid[0][2].getType() == Grid.MYSIGN)
return 10;
else if(grid[1][1].getType() != Grid.MYSIGN && grid[0][2].getType() != Grid.MYSIGN)
return -10;
}
return 0;
}
}
class Case{
public final static int NEUTRE = 0;
public final static int CROSS = 1;
public final static int ZERO = 2;
private int type;
private GridIndex index;
public Case(int i, int j){
this.type = NEUTRE;
this.index = new GridIndex(i,j);
}
public Case(int type, int i, int j){
this.type = type;
this.index = new GridIndex(i,j);
}
public Case(int type, GridIndex gi){
this.type = type;
this.index = gi;
}
public int getType(){
return this.type;
}
public GridIndex getIndex(){
return this.index;
}
public boolean setType(int actual_sign){
if(actual_sign == 1 || actual_sign == 2){
this.type = actual_sign;
return true;
}else
return false;
}
public String toString(){
if(this.type == ZERO)
return "O";
else if(this.type == CROSS)
return "X";
else
return " ";
}
}
class GridIndex{
private int i, j;
public GridIndex(int i, int j){
this.i = i;
this.j = j;
}
public GridIndex(){
this.i = 0;
this.j = 0;
}
public void setIndex(int i, int j){
this.i = i;
this.j = j;
}
public int getI(){
return this.i;
}
public int getJ(){
return this.j;
}
public String toString(){
return "{ "+this.i+" "+this.j+" }";
}
}
class Tree{
private Map<String, Tree> sons = new HashMap<>();
private Grid grid;
private int depth;
public Tree(Grid g){
this.grid = g;
}
public int getSize(){
return sons.size();
}
public void put(String key, Tree t){
sons.put(key, t);
}
public Tree makeBranch(int depth, Grid g){
if(depth < 0)
return null;
Tree tree = new Tree(g);
for (int i = 0 ; i < 3 && depth > 0; i++){
for (int j = 0 ; j < 3 && depth > 0; j++){
if(g.isPlayable(i,j) && !g.isTheEnd()){
GridIndex gi = new GridIndex(i, j);
tree.put(""+i+""+j,makeBranch(depth-1, g.play(gi)));
}
}
}
return tree;
}
public void showBranch(){
for(Tree son : sons.values())
this.grid.displayGrid();
}
}
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Grid g = new Grid();
GridIndex gi = new GridIndex();
// game loop
//while (true) {
//System.out.println("Entrez vos coordoné");
//int opponentRow = in.nextInt();
//int opponentCol = in.nextInt();
//int validActionCount = in.nextInt();
// for (int i = 0; i < validActionCount; i++) {
// int row = in.nextInt();
// int col = in.nextInt();
// }
// if(opponentRow == -1 && opponentCol == -1 || Integer.parseInt(args[0]) == 1)// si je commence
// g.setMYSIGN(Case.CROSS);
// else{
// gi.setIndex(opponentRow, opponentCol);
// g.setMYSIGN(Case.ZERO);
// }
//gi.setIndex(opponentRow, opponentCol);
//g = testjeu(g,gi);
Tree t = new Tree(g);
t = t.makeBranch(3, g);
System.out.println(t.getSize());
t.showBranch();
//}
}
}