package pylos.ai;

import java.util.Iterator;

public class AlphabetaAi {
	/**
	 * if depth == 0 then AI makes random moves
	 * @param depth
	 * 
	 */
	
	public void AI(int depth) {
		/**
		 * get player	commence tjrs par max
		 * if(depht != 0 || !endGame)
		 * 		Parcourir les coups possibles
		 * 			joue le coup >> new GameState gs
		 * 			calcule MAX (gs, depth - 1)
		 */
		int tmp;
		int alpha = -1000;
		int beta = 1000;
		
		if(depth!=0) {			// if depth = 0 play random
			GameState gs = new GameState();
			gs.generatePosibleMoves();
			GameState betterMove = null;
			Iterator<GameState> iterator = gs.iterator();
			while(iterator.hasNext()) {
				GameState tryMove = iterator.next();
				tmp = calcMinMax(tryMove, depth - 1, alpha, beta);
				if(alpha < tmp) {
					alpha = tmp;
					betterMove = tryMove;
				}
			}
		}
		
	}

	private int calcMinMax(GameState gs, int depth, int alpha, int beta, int which) {
		
		int tmp; 
		
		if(depth == 0 || gs.state.gameFinished()) {
			EvaluateGame.evaluate(gs);
			return gs.score; 
		}
		
		gs.generatePosibleMoves();
		GameState betterMove = null;
		Iterator<GameState> iterator = gs.iterator();
		while(iterator.hasNext()) {
			GameState tryMove = iterator.next();
			tmp = calcMinMax(tryMove, depth - 1, alpha, beta, which * -1);
			if(which > 0 && alpha < tmp) {
				alpha = tmp;
			}
			if(which < 0 && beta > tmp) {
				beta = tmp;
			}
		}
		
		
		return 0;
	}
}

@@ -38,16 +38,68 @@
 terator;
+%0Aimport java.util.LinkedList;%0Aimport java.util.List;
 %0A%0Apublic
@@ -169,16 +169,21 @@
 m moves%0A
+%09 * %0A
 %09 * @par
@@ -201,17 +201,16 @@
 * %0A%09 */%0A
-%09
 %0A%09public
@@ -252,17 +252,17 @@
 t player
-%09
+ 
 commence
@@ -312,18 +312,16 @@
 e)%0A%09%09 * 
-%09%09
 Parcouri
@@ -347,19 +347,16 @@
 es%0A%09%09 * 
-%09%09%09
 joue le 
@@ -385,19 +385,16 @@
 gs%0A%09%09 * 
-%09%09%09
 calcule 
@@ -476,28 +476,105 @@
 ;%0A%09%09
+int which = -1;%0A%09%09List%3CGameState%3E betterMove = new LinkedList%3CGameState%3E();%0A
 %0A%09%09if
+ 
 (depth
+ 
 !=
+ 
 0) %7B
-%09%09%09
+ 
 // i
@@ -666,40 +666,8 @@
 ();%0A
-%09%09%09GameState betterMove = null;%0A
 %09%09%09I
@@ -715,24 +715,25 @@
 ();%0A%09%09%09while
+ 
 (iterator.ha
@@ -836,16 +836,23 @@
 ha, beta
+, which
 );%0A%09%09%09%09i
@@ -852,16 +852,78 @@
 ;%0A%09%09%09%09if
+ (alpha == tmp) %7B%0A%09%09%09%09%09betterMove.add(tryMove);%0A%09%09%09%09%7D else if 
 (alpha %3C
@@ -966,23 +966,73 @@
 rMove = 
+new LinkedList%3CGameState%3E();%0A%09%09%09%09%09betterMove.add(
 tryMove
+)
 ;%0A%09%09%09%09%7D%0A
@@ -1038,20 +1038,146 @@
 %0A%09%09%09%7D%0A%09%09
-%7D%0A%09%09
+%09choose(betterMove).move.makeMove();%0A%09%09%7D%0A%0A%09%7D%0A%0A%09private GameState choose(List%3CGameState%3E betterMove) %7B%0A%09%09return betterMove.get(0);%0A
 %0A%09%7D%0A%0A%09pr
@@ -1256,18 +1256,16 @@
 hich) %7B%0A
-%09%09
 %0A%09%09int t
@@ -1271,17 +1271,15 @@
 tmp;
- %0A%09%09
+%0A
 %0A%09%09if
+ 
 (dep
@@ -1369,16 +1369,13 @@
 ore;
- 
 %0A%09%09%7D%0A
-%09%09
 %0A%09%09g
@@ -1404,39 +1404,8 @@
 ();%0A
-%09%09GameState betterMove = null;%0A
 %09%09It
@@ -1455,16 +1455,17 @@
 %0A%09%09while
+ 
 (iterato
@@ -1586,24 +1586,25 @@
 * -1);%0A%09%09%09if
+ 
 (which %3E 0 &
@@ -1620,31 +1620,30 @@
  tmp) %7B%0A%09%09%09%09
-alpha =
+return
  tmp;%0A%09%09%09%7D%0A%09
@@ -1646,16 +1646,17 @@
 %09%7D%0A%09%09%09if
+ 
 (which %3C
@@ -1679,22 +1679,22 @@
 ) %7B%0A%09%09%09%09
-beta =
+return
  tmp;%0A%09%09
@@ -1700,21 +1700,16 @@
 %09%09%7D%0A%09%09%7D%0A
-%09%09%0A%09%09
 %0A%09%09retur

fix error