Implemeted basic Ai2

This commit is contained in:
Maurizio Bruetsch 2017-10-20 21:37:01 +02:00
parent a0d6033cb7
commit b2500b0c86

View file

@ -1,6 +1,7 @@
package player.maurizio; package player.maurizio;
import java.util.Random; import java.util.Random;
import game.Game;
import player.Player; import player.Player;
@ -25,6 +26,32 @@ public class MaurizioAI implements Player {
} }
public int move(int[][] board){ 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;
}
}
//if no wins were found try avoiding losing on the next move
for(int i=0;i<Game.GAME_COLUMNS;i++){
boolean loser = false;
if(board[i][0] == 0){
int[][] boardNew = makeMove(board, i);
for(int j=0;j<Game.GAME_COLUMNS;j++){
if(boardNew[j][0] != 0 && checkWin(boardNew,j,1)){
loser = true;
break;
}
}
if(!loser){
return i;
}
}
}
return moveRandom(board);
}
private int moveRandom(int[][] board){
int choice = ran.nextInt(7); int choice = ran.nextInt(7);
while (board[choice][0] != 0) { while (board[choice][0] != 0) {
choice = ran.nextInt(7); choice = ran.nextInt(7);
@ -32,6 +59,113 @@ public class MaurizioAI implements Player {
return choice; return choice;
} }
private int[][] makeMove(int[][] board, int choice) {
// Check his choice against the current board.
if (choice < 0 || choice > Game.GAME_COLUMNS || board[choice][0] != 0) {
System.err.println("Illegal Move!");
return null;
}
// Find the lowest empty field in the board in the choosen column.
int pos = 0;
while (pos < (board[0].length - 1) && board[choice][pos + 1] == 0) {
pos++;
}
// Change the board accordingl
int[][] boardNew = copyBoard(board);
boardNew[choice][pos] = id;
return boardNew;
}
private int[][] copyBoard(int[][] board) {
int[][] ret = new int[Game.GAME_COLUMNS][Game.GAME_ROWS];
for (int i = 0; i < Game.GAME_COLUMNS; i++) {
for (int j = 0; j < Game.GAME_ROWS; j++) {
ret[i][j] = board[i][j];
}
}
return ret;
}
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
*/
private boolean checkWin(int[][] board, int col, int id){
// find row of last move -- assuming the move was legal
int row = 0;
while(row < Game.GAME_ROWS-1 && board[col][row+1] == 0){
row++;
}
for(int i=-3;i<=0;i++){
boolean win = true;
//check row
for(int j=0;j<4;j++){
if(!withinBoard(col+i+j,row)){
win = false;
break;
}
if(j != -i && board[col+i+j][row] != id){
win = false;
break;
}
}
if(win){
return true;
}
// check cols
win = true;
for(int j=0;j<4;j++){
if(!withinBoard(col,row+i+j)){
win = false;
break;
}
if(j != -i && board[col][row+i+j] != id){
win = false;
break;
}
}
if(win){
return true;
}
//check bottom left -> top-right diags
win = true;
for(int j=0;j<4;j++){
if(!withinBoard(col+i+j,row-i-j)){
win = false;
break;
}
if(j != -i && board[col+i+j][row-i-j] != id){
win = false;
break;
}
}
if(win){
return true;
}
//check top left -> bottom right diags
win = true;
for(int j=0;j<4;j++){
if(!withinBoard(col+i+j,row+i+j)){
win = false;
break;
}
if(j != -i && board[col+i+j][row+i+j] != id){
win = false;
break;
}
}
if(win){
return true;
}
}
return false;
}
public String getName(){ public String getName(){
return this.name; return this.name;