commit
a9095ed38a
28
Makefile
28
Makefile
|
@ -11,16 +11,16 @@ DOC=doc
|
|||
CLASSPATH=src
|
||||
LIBARIES=
|
||||
CLASSES=\
|
||||
src/game/Main.java \
|
||||
src/game/Game.java \
|
||||
src/game/GameHistory.java \
|
||||
src/game/GameEntry.java \
|
||||
src/game/PlayerObject.java \
|
||||
src/player/Player.java \
|
||||
src/player/malte/MalteAI.java \
|
||||
src/player/malte/Pattern.java \
|
||||
src/player/malte/Item.java \
|
||||
src/player/maurizio/MaurizioAI.java
|
||||
src\game\Main.java \
|
||||
src\game\Game.java \
|
||||
src\game\GameHistory.java \
|
||||
src\game\GameEntry.java \
|
||||
src\game\PlayerObject.java \
|
||||
src\player\Player.java \
|
||||
src\player\malte\MalteAI.java \
|
||||
src\player\malte\Pattern.java \
|
||||
src\player\malte\Item.java \
|
||||
src\player\maurizio\MaurizioAI.java
|
||||
|
||||
OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class)
|
||||
|
||||
|
@ -32,13 +32,13 @@ N=100
|
|||
|
||||
|
||||
all: $(CLASSES) $(FXMLS_BUILD)
|
||||
$(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(BUILDS)
|
||||
$(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH) $(CLASSES) -d $(BUILDS)
|
||||
|
||||
run: $(CLASSES)
|
||||
$(JAVA) -cp $(BUILDS):$(LIBARIES) $(MAIN)
|
||||
$(JAVA) -cp $(BUILDS) $(MAIN)
|
||||
|
||||
simulate: $(CLASSES)
|
||||
$(JAVA) -cp $(BUILDS):$(LIBARIES) $(MAIN) $(N)
|
||||
$(JAVA) -cp $(BUILDS) $(MAIN) $(N)
|
||||
|
||||
doc: $(CLASSES)
|
||||
$(JAVADOC) $(JAVADOC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(DOC)
|
||||
|
@ -47,4 +47,4 @@ jar: all
|
|||
$(JAR) -cfe VierGewinnt.jar $(MAIN) -C $(BUILDS) .
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILDS)/* $(DOC)/* VierGewinnt.jar
|
||||
rm -rf $(BUILDS)\* $(DOC)\* VierGewinnt.jar
|
||||
|
|
|
@ -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, id);
|
||||
// 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, int id) {
|
||||
// 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