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
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -64,7 +64,58 @@ public class MaurizioAI implements Player {
|
|||
}
|
||||
|
||||
//look for two move wins among the non-loosing moves
|
||||
int m2 = winningMove2(board, id, moves1);
|
||||
if(m2 != -1){
|
||||
return m2;
|
||||
}
|
||||
|
||||
//copy moves
|
||||
LinkedList<Integer> moves2 = new LinkedList<Integer>();
|
||||
for(int i : moves1){
|
||||
moves2.add(i);
|
||||
}
|
||||
// avoid 2-move losses
|
||||
for(int i : moves1){ // I make a move
|
||||
int[][] board1 = makeMove(board, i, id);
|
||||
int m3 = winningMove2(board1, enemyID, allMoves());
|
||||
if(m3 != -1){
|
||||
moves2.remove((Object) i);
|
||||
}
|
||||
}
|
||||
|
||||
if(!moves2.isEmpty()){
|
||||
return moveCentral(board, moves2);
|
||||
} else {
|
||||
if(!moves1.isEmpty()){
|
||||
return moveCentral(board, moves1);
|
||||
}
|
||||
}
|
||||
|
||||
return moveCentral(board); // can't be reached
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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++){
|
||||
|
@ -81,56 +132,7 @@ public class MaurizioAI implements Player {
|
|||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
//copy moves
|
||||
LinkedList<Integer> moves2 = new LinkedList<Integer>();
|
||||
for(int i : moves1){
|
||||
moves2.add(i);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!moves2.isEmpty()){
|
||||
this.ran = new Random();
|
||||
int choice = ran.nextInt(moves2.size());
|
||||
return moves2.get(choice);
|
||||
} else {
|
||||
if(!moves1.isEmpty()){
|
||||
this.ran = new Random();
|
||||
int choice = ran.nextInt(moves1.size());
|
||||
return moves1.get(choice);
|
||||
}
|
||||
}
|
||||
|
||||
return moveRandom(board); // can't be reached
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
return -1;// signals that no winning move exists
|
||||
return -1; // no move found that wins in 2 moves
|
||||
}
|
||||
|
||||
private int moveRandom(int[][] board){
|
||||
|
@ -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