From b2500b0c86e87e1192eaaf55667053df6fe7905d Mon Sep 17 00:00:00 2001 From: Maurizio Bruetsch Date: Fri, 20 Oct 2017 21:37:01 +0200 Subject: [PATCH 1/4] Implemeted basic Ai2 --- src/player/maurizio/MaurizioAI.java | 134 ++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/src/player/maurizio/MaurizioAI.java b/src/player/maurizio/MaurizioAI.java index 476a1ad..23c2a35 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; From 0f3490cc65cdc9d03782e76a12340d1d0d4753fb Mon Sep 17 00:00:00 2001 From: Maurizio Bruetsch Date: Fri, 20 Oct 2017 21:45:09 +0200 Subject: [PATCH 2/4] bug fixing --- src/player/maurizio/MaurizioAI.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/player/maurizio/MaurizioAI.java b/src/player/maurizio/MaurizioAI.java index 23c2a35..6d4ae52 100644 --- a/src/player/maurizio/MaurizioAI.java +++ b/src/player/maurizio/MaurizioAI.java @@ -36,7 +36,7 @@ public class MaurizioAI implements Player { for(int i=0;i Game.GAME_COLUMNS || board[choice][0] != 0) { System.err.println("Illegal Move!"); From a0b734e4c49519e9b33ea0ed8e5cf231a5444247 Mon Sep 17 00:00:00 2001 From: Maurizio Bruetsch Date: Fri, 20 Oct 2017 21:49:05 +0200 Subject: [PATCH 3/4] changed name of Makefile, because of Bill Gates --- Makefile | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index e134f54..3b45812 100644 --- a/Makefile +++ b/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 From f0c3999797de2e850aedcfe87ea6752b6cd5988a Mon Sep 17 00:00:00 2001 From: Maurizio Bruetsch Date: Fri, 20 Oct 2017 22:05:39 +0200 Subject: [PATCH 4/4] bug fixing --- src/player/maurizio/MaurizioAI.java | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/player/maurizio/MaurizioAI.java b/src/player/maurizio/MaurizioAI.java index 6d4ae52..62a4ebc 100644 --- a/src/player/maurizio/MaurizioAI.java +++ b/src/player/maurizio/MaurizioAI.java @@ -33,21 +33,21 @@ public class MaurizioAI implements Player { } } //if no wins were found try avoiding losing on the next move - for(int i=0;i