-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ia.pde
345 lines (274 loc) · 10.3 KB
/
Ia.pde
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
class Tree{
private int[][] stateIA=new int[8][8];
private int[][] RangesIA=new int[8][8];
private Move move;
private Player J;
private ArrayList <Tree> sons;
private int heuristic;
private int node;
public Tree(int j,int[][] STATE,Move m,int hrtc,int[][] RNGS){
int scoreCPM=0,scoreH=0;
Position p1,p2,p3,p4;
//critical positions
if(stateIA[0][0]==0) p1=new Position(0,0);
else p1=null;
if(stateIA[0][7]==0) p2=new Position(0,7);
else p2=null;
if(stateIA[7][0]==0) p3=new Position(7,0);
else p3=null;
if(stateIA[7][7]==0) p4=new Position(7,7);
else p4=null;
J=new Player(j);
for(int i=0;i<8;i++){
for(int k=0;k<8;k++){
stateIA[i][k]=STATE[i][k];
}
}
for(int i=0;i<8;i++){
for(int k=0;k<8;k++){
RangesIA[i][k]=RNGS[i][k];
}
}
if(m==null) {//root tree
//println("--------la tete--------");
//showMatrice(stateIA);
heuristic=hrtc;
}
else{//son's root
move=m;
stateIA[m.GetPos().GetX()][m.GetPos().GetY()]=j;
J.score(m.GetPos(),false,stateIA);
//changer les poids de l'IA
SetRnages();
J.SetScore(stateIA);
if(J.GetPawn()==cmp.GetPawn()) scoreCPM=J.GetScore();
else{
scoreH=J.GetScore();
}
//next player
J.SetPawn();
J.SetScore(stateIA);
if(J.GetPawn()==cmp.GetPawn()) scoreCPM=J.GetScore();
else{
scoreH=J.GetScore();
}
//println("new son "+m.toString());
//showMatrice(stateIA);
//heuristic add range & harvest of the move
if(J.GetPawn()==human.GetPawn()) heuristic=hrtc+m.GetRange()+3*m.GetRec();
else heuristic=hrtc-2*m.GetRange()-6*m.GetRec();
}
// possible moves next player
for(int i=0;i<8;i++){
for(int k=0;k<8;k++){
if(stateIA[i][k]==J.GetPawn()){
J.MovePossible(new Position(i,k),stateIA,RangesIA);
}
}
}//boucle for
//println("next move's player ="+J.GetPawn());
//heuristique criticle positions
for(int i=0;i<J.GetMovep().size();i++){
//println(J.GetMovep().get(i).toString());
if( J.GetMovep().get(i).GetPos().equals(p1) ||
J.GetMovep().get(i).GetPos().equals(p2) ||
J.GetMovep().get(i).GetPos().equals(p3) ||
J.GetMovep().get(i).GetPos().equals(p4) ){
if(J.GetPawn()==human.GetPawn()){
heuristic-=100;
}
}
}//end for
if(J.GetMovep().size()==0) {
sons=null;
//heuristic
if(!StateGameSatured(stateIA)){
if(J.GetPawn()==human.GetPawn()) {
heuristic+=50;
}
else {
heuristic-=50;
}
}
}else {
sons=new ArrayList <Tree>();
}
//end of game
if(StateGameSatured(stateIA)){
heuristic+=(scoreCPM-scoreH)*100;
}
}// end constructor
//create tree of possibilities
public void CreateTree(int depth,int indx){
Tree son;
if(!StateGameSatured(stateIA)){
if(depth%2==0 ){ //humain turn
if(indx<J.GetMovep().size()){
//println("-\nhuman : profondeur= "+profondeur);
//println(J.GetMovep().get(indx).toString());
son=new Tree(human.GetPawn(),stateIA,J.GetMovep().get(indx),heuristic,RangesIA);
sons.add(son);
CreateTree(depth,indx+1);
}else{
if(depth-1>=0 && sons!=null){for(int i=0;i<sons.size();i++) sons.get(i).CreateTree(depth-1,0);}
else sons=null;
}
}else{ // cmp turn
if(indx<J.GetMovep().size()){
//println("\ncmp : profondeur= "+profondeur);
//println(J.GetMovep().get(indx).toString());
son=new Tree(cmp.GetPawn(),stateIA,J.GetMovep().get(indx),heuristic,RangesIA);
sons.add(son);
CreateTree(depth,indx+1);
}else{
if(depth-1>=0 && sons!=null){for(int i=0;i<sons.size();i++) sons.get(i).CreateTree(depth-1,0);}
else sons=null;
}
}
}
}// end construire arbre
//alphabeta algo
public int ALPHABETA(int maxORmin,int alpha,int beta){
if(sons==null) { node=heuristic; return heuristic;}
else{
if(maxORmin==-1){//Min node
node=infinity; //node=+(l'infini) pire cas
for(int i=0;i<sons.size();i++){
node = min(node, sons.get(i).ALPHABETA(-maxORmin, alpha, beta));
if (alpha >= node) return node; /* elagage */
beta = min(beta, node);
}//for
}else{//Max node
node = -infinity; //node=-(l'infini) pire cas
for(int i=0;i<sons.size();i++){
node=max(node,sons.get(i).ALPHABETA(-maxORmin, alpha, beta));
if(node>= beta) return node; /* elagage */
alpha = max(alpha, node);
}//for
}
return node;
}//else (sons!=null)
}//end ALPHABETA
public int negaMax(int MinOrMax,int alpha,int beta){
if(sons==null) { node=heuristic; return heuristic;}
int bestValue =-infinity;
for(int i=0;i<sons.size();i++){
node = -sons.get(i).negaMax(-MinOrMax, -beta, -alpha);
if(node>bestValue) {
bestValue=node;
if(bestValue>alpha){
alpha=bestValue;
if (alpha >= beta) break;
}
}
}//end for
return bestValue;
}//end negaMax
//return the move the cmp have to play
public Move NextMove(){
if(sons==null) return null;
for(int i=0;i<sons.size();i++){
if(sons.get(i).node==node) {return sons.get(i).move;}
}
return null;
}
//drow precised matrix
public void showMatrice(int[][] STATE){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(j==0) if(STATE[i][j]>=0) print(" "+STATE[i][j]);
else print(STATE[i][j]);
else{
if(STATE[i][j]>=0) print(" "+STATE[i][j]);
else print(" "+STATE[i][j]);
}
}
println();
}
}
//update ranges
public boolean SetRnages(){
boolean set=false;
if(stateIA[0][0]==cmp.GetPawn()){
if(RangesIA[0][1]<0){
RangesIA[0][1]=-3/2*RangesIA[0][1];
RangesIA[1][0]=-3/2*RangesIA[1][0];
RangesIA[1][1]=-3/2*RangesIA[1][1];
set=true;
}
if(RangesIA[0][6]<0){
int i;
for(i=1;i<=7 && stateIA[0][i]==human.GetPawn();i++){}
if(i==6) {RangesIA[0][6]=75; set=true;}
}
if(RangesIA[0][7]==150) {RangesIA[0][7]=2*150;set=true;}
if(RangesIA[6][0]<0){
int i;
for(i=1;i<=7 && stateIA[i][0]==human.GetPawn();i++){}
if(i==6) {RangesIA[6][0]=75;set=true;}
}
if(RangesIA[7][0]==150) {RangesIA[7][0]=150*2;set=true;}
}
if(stateIA[0][7]==cmp.GetPawn()){
if(RangesIA[0][6]<0){
RangesIA[0][6]=-3/2*RangesIA[0][6];
RangesIA[1][6]=-3/2*RangesIA[1][6];
RangesIA[1][7]=-3/2*RangesIA[1][7];
set=true;
}
if(RangesIA[0][1]<0){
int i;
for(i=6;i>=0 && stateIA[0][i]==human.GetPawn();i--){}
if(i==1) {RangesIA[0][1]=75; set=true;}
}
if(RangesIA[0][0]==150) {RangesIA[0][0]=150*2; set=true;}
if(RangesIA[6][7]<0){
int i;
for(i=1;i<=7 && stateIA[i][7]==human.GetPawn();i++){}
if(i==6) {RangesIA[6][7]=75; set=true;}
}
if(RangesIA[7][7]==150) {RangesIA[7][7]=150*2;set=true;}
}
if(stateIA[7][0]==cmp.GetPawn()){
if(RangesIA[6][0]<0){
RangesIA[6][0]=-3/2*RangesIA[6][0];
RangesIA[6][1]=-3/2*RangesIA[6][1];
RangesIA[7][1]=-3/2*RangesIA[7][1];
set=true;
}
if(RangesIA[1][0]<0){
int i;
for(i=6;i>=0 && stateIA[i][0]==human.GetPawn();i--){}
if(i==1) {RangesIA[1][0]=75;set=true; }
}
if(RangesIA[0][0]==150) {RangesIA[0][0]=150*2;set=true;}
if(RangesIA[7][6]<0){
int i;
for(i=1;i<=7 && stateIA[7][i]==human.GetPawn();i++){}
if(i==6) {RangesIA[7][6]=75; set=true;}
}
if(RangesIA[7][7]==150) {RangesIA[7][7]=150*2;set=true;}
}
if(stateIA[7][7]==cmp.GetPawn()){
if(RangesIA[7][6]<0){
RangesIA[7][6]=-3/2*RangesIA[7][6];
RangesIA[6][6]=-3/2*RangesIA[6][6];
RangesIA[6][7]=-3/2*RangesIA[6][7];
set=true;
}
if(RangesIA[7][1]<0){
int i;
for(i=6;i>=0 && stateIA[7][i]==human.GetPawn();i--){}
if(i==1) {RangesIA[7][1]=75; set=true;}
}
if(RangesIA[7][0]==150) RangesIA[7][0]=150*2;
if(RangesIA[1][7]<0){
int i;
for(i=6;i>=0 && stateIA[i][7]==human.GetPawn();i--){}
if(i==1) {RangesIA[1][7]=75;set=true;}
}
if(RangesIA[0][7]==150) {RangesIA[0][7]=150*2; set=true;}
}
return set;
}//end set Ranges
}//class Arbre