simplified AI code

This commit is contained in:
Maurizio Bruetsch 2017-10-24 12:48:51 +02:00
parent b543da5964
commit 047ec9b728

View file

@ -29,36 +29,33 @@ public class MaurizioAI implements Player {
public int move(int[][] board){
// check for one move wins
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0 && checkWin(board, i, id)){
return i;
}
}
int m1 = winningMove(board, id);
if(m1 != -1){
return m1;
}
// create a list of all possible/legal moves
// create a list of all legal moves
LinkedList<Integer> moves1 = new LinkedList<Integer>();
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0){
moves1.add(i);
}
}
// filter out loosing moves
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0){
int[][] boardNew = makeMove(board, i, id);
for(int j=0;j<Game.GAME_COLUMNS;j++){
if(boardNew[j][0] == 0 && checkWin(boardNew, j, enemyID)){
moves1.remove((Object) i);
break;
}
}
int m2 = winningMove(boardNew, enemyID);
if(m2 != -1){
moves1.remove((Object) i);
}
}
}
//move randomly if instant defeat is inevitable
if(moves1.isEmpty()){
return moveRandom(board);
}
//forced move
@ -66,47 +63,27 @@ public class MaurizioAI implements Player {
return moves1.peek();
}
//copy moves that do not loose
LinkedList<Integer> moves2 = new LinkedList<Integer>();
for(int i : moves1){
moves2.add(i);
}
//look for two move wins among the non-loosing moves
LinkedList<Integer> removes = new LinkedList<Integer>();
for(int i : moves2){
for(int i : moves1){
int[][] boardNew = makeMove(board, i, id);
boolean winner1 = true;
for(int j=0;j<Game.GAME_COLUMNS;j++){
boolean winner2 = false;
if(boardNew[j][0] == 0){
int[][] boardNewNew = makeMove(boardNew, j, enemyID);
for(int k=0;k<Game.GAME_COLUMNS;k++){
if(boardNewNew[k][0] == 0 && checkWin(boardNewNew, k, id)){
// if opponent plays move j than we have a winning move
winner2 = true;
break;
}
}
}
if(!winner2){
winner1 = false;
break;
int m3 = winningMove(boardNewNew, id);
if(m3 == -1){
winner1 = false;
break;
}
}
}
// winner1 == false means that the opponent had a reply after which we couldn't
// directly win
if(!winner1){
removes.add(i);
if(winner1){
return i;
}
}
for(int i : removes){
moves2.remove((Object) i);
}
//no 2-move winning moves, then choose a non-loosing move
if(moves2.isEmpty()){
if(moves1.isEmpty()){
this.ran = new Random();
int choice = ran.nextInt(moves1.size());
return moves1.get(choice);
@ -115,22 +92,13 @@ public class MaurizioAI implements Player {
return moveRandom(board); // can't be reached
}
private void logGame(int[][] board) {
for(int i = 0; i < board[0].length; i++){
for(int j = 0;j < board.length; j++){
if(board[j][i] == 0){
System.out.print("| ");
}
if(board[j][i] == 1){
System.out.print("|1");
}
if (board[j][i] == 2){
System.out.print("|2");
}
private int winningMove(int [][] board,int id){
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0 && checkWin(board, i, id)){
return i;
}
System.out.println("|");
}
System.out.println();
}
return -1;// signals that no winning move exists
}
private int moveRandom(int[][] board){
@ -171,6 +139,7 @@ public class MaurizioAI implements Player {
private boolean withinBoard(int x, int y){
return ((x>=0 && x < Game.GAME_COLUMNS) && (y>=0 && y < Game.GAME_ROWS));
}
/**
* checks whether the last move closed any lines on the board
*/