Implemeted basic Ai2
This commit is contained in:
parent
a0d6033cb7
commit
b2500b0c86
|
@ -1,6 +1,7 @@
|
|||
package player.maurizio;
|
||||
|
||||
import java.util.Random;
|
||||
import game.Game;
|
||||
|
||||
import player.Player;
|
||||
|
||||
|
@ -25,6 +26,32 @@ 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;
|
||||
}
|
||||
}
|
||||
//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);
|
||||
while (board[choice][0] != 0) {
|
||||
choice = ran.nextInt(7);
|
||||
|
@ -32,6 +59,113 @@ public class MaurizioAI implements Player {
|
|||
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(){
|
||||
return this.name;
|
||||
|
|
Loading…
Reference in a new issue