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