diff --git a/Makefile b/Makefile index 48429a8..251c2f2 100644 --- a/Makefile +++ b/Makefile @@ -33,13 +33,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) @@ -48,4 +48,4 @@ jar: all $(JAR) -cfe VierGewinnt.jar $(MAIN) -C $(BUILDS) . clean: - rm -rf $(BUILDS)/* $(DOC)/* VierGewinnt.jar + rm -rf $(BUILDS)\* $(DOC)\* VierGewinnt.jar diff --git a/src/player/maurizio/MaurizioAI.java b/src/player/maurizio/MaurizioAI.java index 476a1ad..62a4ebc 100644 --- a/src/player/maurizio/MaurizioAI.java +++ b/src/player/maurizio/MaurizioAI.java @@ -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 || 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;