AI enhancement

This commit is contained in:
MaurizioBruetsch 2017-10-22 18:17:49 +02:00
parent ea340e5cda
commit fa8ce856f1

View file

@ -66,13 +66,13 @@ public class MaurizioAI implements Player {
return moves1.peek(); return moves1.peek();
} }
//copy moves that do not loose
LinkedList<Integer> moves2 = new LinkedList<Integer>(); LinkedList<Integer> moves2 = new LinkedList<Integer>();
for(int i : moves1){ for(int i : moves1){
moves2.add(i); moves2.add(i);
} }
//find the best of the non losing move //look for two move wins among the non-loosing moves
//look for two move wins
LinkedList<Integer> removes = new LinkedList<Integer>(); LinkedList<Integer> removes = new LinkedList<Integer>();
for(int i : moves2){ for(int i : moves2){
int[][] boardNew = makeMove(board, i, id); int[][] boardNew = makeMove(board, i, id);
@ -83,6 +83,7 @@ public class MaurizioAI implements Player {
int[][] boardNewNew = makeMove(boardNew, j, enemyID); int[][] boardNewNew = makeMove(boardNew, j, enemyID);
for(int k=0;k<Game.GAME_COLUMNS;k++){ for(int k=0;k<Game.GAME_COLUMNS;k++){
if(boardNewNew[k][0] == 0 && checkWin(boardNewNew, k, id)){ if(boardNewNew[k][0] == 0 && checkWin(boardNewNew, k, id)){
// if opponent plays move j than we have a winning move
winner2 = true; winner2 = true;
break; break;
} }
@ -93,6 +94,8 @@ public class MaurizioAI implements Player {
break; break;
} }
} }
// winner1 == false means that the opponent had a reply after which we couldn't
// directly win
if(!winner1){ if(!winner1){
removes.add(i); removes.add(i);
} }
@ -149,7 +152,7 @@ public class MaurizioAI implements Player {
while (pos < (board[0].length - 1) && board[choice][pos + 1] == 0) { while (pos < (board[0].length - 1) && board[choice][pos + 1] == 0) {
pos++; pos++;
} }
// Change the board accordingl // Change the board accordingly
int[][] boardNew = copyBoard(board); int[][] boardNew = copyBoard(board);
boardNew[choice][pos] = id; boardNew[choice][pos] = id;
return boardNew; return boardNew;
@ -169,7 +172,7 @@ public class MaurizioAI implements Player {
return ((x>=0 && x < Game.GAME_COLUMNS) && (y>=0 && y < Game.GAME_ROWS)); return ((x>=0 && x < Game.GAME_COLUMNS) && (y>=0 && y < Game.GAME_ROWS));
} }
/** /**
* checks whether the last Move closed any lines on the board * checks whether the last move closed any lines on the board
*/ */
private boolean checkWin(int[][] board, int col, int iD){ private boolean checkWin(int[][] board, int col, int iD){
// find row of last move -- assuming the move was legal // find row of last move -- assuming the move was legal