improved AI
This commit is contained in:
parent
37d9f8da73
commit
b717ba4e24
|
@ -154,13 +154,13 @@ public class Game {
|
|||
if(output){
|
||||
System.out.println(other(currentP).getP().getName() + " wins.");
|
||||
}
|
||||
return other(currentP).getID(); // TODO this only works if the players hav IDs 1 and 2
|
||||
return other(currentP).getID(); // TODO only works if the players have IDs 1 and 2
|
||||
}
|
||||
}
|
||||
// Uninstall Jansi
|
||||
AnsiConsole.systemUninstall();
|
||||
System.out.println("BUG!");
|
||||
return checkState(false); // this is impossible to reach this, but it makes the compiler happy ;)
|
||||
return checkState(false); // can't be reached
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -30,9 +30,9 @@ public class MaurizioAI implements Player {
|
|||
public int move(int[][] board){
|
||||
// check for one move wins
|
||||
int m1 = winningMove(board, id);
|
||||
if(m1 != -1){
|
||||
return m1;
|
||||
}
|
||||
if(m1 != -1){
|
||||
return m1;
|
||||
}
|
||||
|
||||
// create a list of all legal moves
|
||||
LinkedList<Integer> moves1 = new LinkedList<Integer>();
|
||||
|
@ -46,10 +46,10 @@ public class MaurizioAI implements Player {
|
|||
for(int i=0;i<Game.GAME_COLUMNS;i++){
|
||||
if(board[i][0] == 0){
|
||||
int[][] boardNew = makeMove(board, i, id);
|
||||
int m2 = winningMove(boardNew, enemyID);
|
||||
if(m2 != -1){
|
||||
moves1.remove((Object) i);
|
||||
}
|
||||
int m2 = winningMove(boardNew, enemyID);
|
||||
if(m2 != -1){
|
||||
moves1.remove((Object) i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,22 +64,9 @@ public class MaurizioAI implements Player {
|
|||
}
|
||||
|
||||
//look for two move wins among the non-loosing moves
|
||||
for(int i : moves1){
|
||||
int[][] boardNew = makeMove(board, i, id);
|
||||
boolean winner1 = true;
|
||||
for(int j=0;j<Game.GAME_COLUMNS;j++){
|
||||
if(boardNew[j][0] == 0){
|
||||
int[][] boardNewNew = makeMove(boardNew, j, enemyID);
|
||||
int m3 = winningMove(boardNewNew, id);
|
||||
if(m3 == -1){
|
||||
winner1 = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(winner1){
|
||||
return i;
|
||||
}
|
||||
int m2 = winningMove2(board, id, moves1);
|
||||
if(m2 != -1){
|
||||
return m2;
|
||||
}
|
||||
|
||||
//copy moves
|
||||
|
@ -90,41 +77,24 @@ public class MaurizioAI implements Player {
|
|||
// avoid 2-move losses
|
||||
for(int i : moves1){ // I make a move
|
||||
int[][] board1 = makeMove(board, i, id);
|
||||
for(int j=0;j<Game.GAME_COLUMNS;j++){ // opponent makes a move
|
||||
if(board1[j][0] != 0){
|
||||
continue;
|
||||
}
|
||||
int[][] board2 = makeMove(board1, j, enemyID);
|
||||
for(int k=0;k<Game.GAME_COLUMNS;k++){ // I make another move
|
||||
if(board2[k][0] != 0){
|
||||
continue;
|
||||
}
|
||||
int[][] board3 = makeMove(board2, k, id);
|
||||
//have to make sure that no move of my opponent wins
|
||||
int move = winningMove(board3, enemyID);
|
||||
if(move != -1){
|
||||
moves2.remove((Object) i);
|
||||
}
|
||||
}
|
||||
int m3 = winningMove2(board1, enemyID, allMoves());
|
||||
if(m3 != -1){
|
||||
moves2.remove((Object) i);
|
||||
}
|
||||
}
|
||||
|
||||
if(!moves2.isEmpty()){
|
||||
this.ran = new Random();
|
||||
int choice = ran.nextInt(moves2.size());
|
||||
return moves2.get(choice);
|
||||
return moveCentral(board, moves2);
|
||||
} else {
|
||||
if(!moves1.isEmpty()){
|
||||
this.ran = new Random();
|
||||
int choice = ran.nextInt(moves1.size());
|
||||
return moves1.get(choice);
|
||||
return moveCentral(board, moves1);
|
||||
}
|
||||
}
|
||||
|
||||
return moveRandom(board); // can't be reached
|
||||
return moveCentral(board); // can't be reached
|
||||
}
|
||||
|
||||
private int winningMove(int [][] board,int id){
|
||||
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;
|
||||
|
@ -133,6 +103,38 @@ public class MaurizioAI implements Player {
|
|||
return -1;// signals that no winning move exists
|
||||
}
|
||||
|
||||
private LinkedList<Integer> allMoves(){
|
||||
LinkedList<Integer> ret = new LinkedList<Integer>();
|
||||
for(int i=0;i<Game.GAME_COLUMNS;i++){
|
||||
ret.add(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int winningMove2(int[][] board, int id, LinkedList<Integer> moves){
|
||||
for(int i : moves){
|
||||
if(board[i][0] != 0){
|
||||
continue;
|
||||
}
|
||||
int[][] boardNew = makeMove(board, i, id);
|
||||
boolean winner1 = true;
|
||||
for(int j=0;j<Game.GAME_COLUMNS;j++){
|
||||
if(boardNew[j][0] == 0){
|
||||
int[][] boardNewNew = makeMove(boardNew, j, enemyID);
|
||||
int m3 = winningMove(boardNewNew, id);
|
||||
if(m3 == -1){
|
||||
winner1 = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(winner1){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1; // no move found that wins in 2 moves
|
||||
}
|
||||
|
||||
private int moveRandom(int[][] board){
|
||||
int choice = ran.nextInt(7);
|
||||
while (board[choice][0] != 0) {
|
||||
|
@ -141,6 +143,60 @@ public class MaurizioAI implements Player {
|
|||
return choice;
|
||||
}
|
||||
|
||||
private int moveCentral(int[][] board){
|
||||
if(board[3][0] == 0){
|
||||
return 3;
|
||||
}
|
||||
if(board[2][0] == 0){
|
||||
return 2;
|
||||
}
|
||||
if(board[4][0] == 0){
|
||||
return 4;
|
||||
}
|
||||
if(board[1][0] == 0){
|
||||
return 1;
|
||||
}
|
||||
if(board[5][0] == 0){
|
||||
return 5;
|
||||
}
|
||||
if(board[0][0] == 0){
|
||||
return 0;
|
||||
}
|
||||
if(board[6][0] == 0){
|
||||
return 6;
|
||||
}
|
||||
return -1; // board is full
|
||||
}
|
||||
|
||||
private int moveCentral(int[][] board, LinkedList<Integer> moves){
|
||||
if (moves.isEmpty()) {
|
||||
return -1; // empty list
|
||||
}
|
||||
if(moves.contains(3) && board[3][0] == 0){
|
||||
return 3;
|
||||
}
|
||||
if(moves.contains(2) && board[2][0] == 0){
|
||||
return 2;
|
||||
}
|
||||
if(moves.contains(4) && board[4][0] == 0){
|
||||
return 4;
|
||||
}
|
||||
if(moves.contains(1) && board[1][0] == 0){
|
||||
return 1;
|
||||
}
|
||||
if(moves.contains(5) && board[5][0] == 0){
|
||||
return 5;
|
||||
}
|
||||
if(moves.contains(0) && board[0][0] == 0){
|
||||
return 0;
|
||||
}
|
||||
if(moves.contains(6) && board[6][0] == 0){
|
||||
return 6;
|
||||
}
|
||||
|
||||
return -1; // Can' t happen
|
||||
}
|
||||
|
||||
private int[][] makeMove(int[][] board, int choice, int id) {
|
||||
// Check his choice against the current board.
|
||||
if (choice < 0 || choice > Game.GAME_COLUMNS || board[choice][0] != 0) {
|
||||
|
|
Loading…
Reference in a new issue