Compare commits

...

51 commits

Author SHA1 Message Date
Maurizio Bruetsch 937ba9663d Merge branch 'fix-make' into 'master'
Fix make

See merge request !33
2017-10-27 18:22:58 +02:00
MaurizioBruetsch b60a9b4d06 makefile 2017-10-27 18:19:43 +02:00
MaurizioBruetsch f2fd55ec47 merging conflicts 2017-10-27 18:01:50 +02:00
MaurizioBruetsch 87725b969b Makefile :( 2017-10-27 17:58:55 +02:00
Maurizio Bruetsch afddc1ba6e Merge branch 'fix-make' into 'master'
Fixed Makefile problems

See merge request !32
2017-10-27 17:51:30 +02:00
MaurizioBruetsch 352f991005 Makefile is for UNIX and makeWindows is for Windows 2017-10-27 17:50:04 +02:00
Maurizio Bruetsch f2359321cc Merge branch 'mauriAI' into 'master'
mauriAI

See merge request !26
2017-10-27 17:38:34 +02:00
Malte Tammena 4cb2eecac4 Merge branch 'malte-ai' into 'master'
Malte ai

See merge request !25
2017-10-27 09:46:51 +02:00
Malte Tammena 74320a091a Added informational output 2017-10-27 09:41:56 +02:00
MaurizioBruetsch b717ba4e24 improved AI 2017-10-26 23:42:44 +02:00
MaurizioBruetsch 37d9f8da73 AI sees two move loosing tricks 2017-10-25 15:20:13 +02:00
MaurizioBruetsch 5605076dbf Merge remote-tracking branch 'origin/master' into mauriAI 2017-10-25 14:21:59 +02:00
Maurizio Bruetsch 21f73d6694 windowsed Makefile 2017-10-24 21:32:39 +02:00
Malte Tammena 5496b0d40b Merge branch 'malte-alternative-sequence' into 'master'
Maltes new AI

See merge request !29
2017-10-24 21:26:10 +02:00
Maurizio Bruetsch 047ec9b728 simplified AI code 2017-10-24 12:48:51 +02:00
Maurizio Bruetsch b543da5964 post merge problems 2017-10-23 14:54:01 +02:00
Maurizio Bruetsch d9d4f50e74 resolving merge conflicts 2017-10-23 14:47:55 +02:00
Maurizio Bruetsch 281b7ce321 un-Windowsed Makefile 2017-10-23 13:28:12 +02:00
MaurizioBruetsch fa8ce856f1 AI enhancement 2017-10-22 18:17:49 +02:00
MaurizioBruetsch ea340e5cda AI checking for two-move wins now 2017-10-21 16:30:35 +02:00
MaurizioBruetsch 466c41737e Merge branch 'master' into mauriAI 2017-10-21 13:18:51 +02:00
MaurizioBruetsch ff624e3bff Merge branch '6-colorize-output-with-jansi' into mauriAI 2017-10-21 13:10:16 +02:00
MaurizioBruetsch 101ef5bd89 fixed bug in AI 2017-10-21 12:07:07 +02:00
Maurizio Bruetsch dd68bbf6c7 found bug 2017-10-21 00:12:17 +02:00
Malte Tammena 8bc4af7d8a Removed output 2017-10-20 23:59:05 +02:00
Maurizio Bruetsch d52b8c60a9 furhter AI enhancement 2017-10-20 23:59:05 +02:00
Malte Tammena 1cb455100d IO good enough 2017-10-20 23:59:05 +02:00
Malte Tammena b6214bd9e1 Finished implementation of Human, improving IO 2017-10-20 23:59:05 +02:00
Malte Tammena eef311480f Fixed enemyID bug 2017-10-20 23:58:11 +02:00
Malte Tammena 96758d864d Informational output 2017-10-20 23:57:42 +02:00
Malte Tammena 6a93dd4a05 Implemented twoOfFourPatterns, ai development 2017-10-20 23:57:42 +02:00
Malte Tammena a3d7f190d0 Removed unneccesary output 2017-10-20 23:57:42 +02:00
Malte Tammena f7576ebfe6 AI improvements 2017-10-20 23:57:42 +02:00
Malte Tammena 479cd7f975 Commiting wrongly on this branch to cherry-pick 2017-10-20 23:57:42 +02:00
Malte Tammena 6f55dde659 Did bug work-around 2017-10-20 23:57:42 +02:00
Malte Tammena 62f6466370 Did commenting 2017-10-20 23:57:42 +02:00
Malte Tammena a4110b96a2 Did commenting 2017-10-20 23:57:42 +02:00
Malte Tammena dc1e45fe0c Did commenting 2017-10-20 23:57:42 +02:00
Malte Tammena 1d87754f53 Did commenting 2017-10-20 23:57:41 +02:00
Malte Tammena 2e9347cfb3 AI improvements 2017-10-20 23:55:25 +02:00
Malte Tammena 9769fc0c6d Worked on Patterns for the ai 2017-10-20 23:55:25 +02:00
Malte Tammena b0ed66a786 Strip output, fixing all AI problemes 2017-10-20 23:53:55 +02:00
Malte Tammena 318792c9b0 Having problems with the Pattern, losing 2017-10-20 23:53:55 +02:00
Malte Tammena 499859fd53 Changing Patterns, creating and testing them 2017-10-20 23:53:55 +02:00
Malte Tammena 51a783eb4b Working on pattern implementation 2017-10-20 23:53:55 +02:00
Malte Tammena dee946cb5a Working on Patterns, doing great 2017-10-20 23:53:55 +02:00
MaurizioBruetsch 54afd84fe1 Merge branch 'master' of https://git.informatik.uni-leipzig.de/mt85zybu/VierGewinnt 2017-10-19 11:05:33 +02:00
MaurizioBruetsch 158b5735f2 Merge branch 'master' of https://git.informatik.uni-leipzig.de/mt85zybu/VierGewinnt 2017-10-18 14:12:14 +02:00
MaurizioBruetsch b5dc4406fa Merge branch 'master' of https://git.informatik.uni-leipzig.de/mt85zybu/VierGewinnt 2017-10-18 13:45:35 +02:00
MaurizioBruetsch cff6eb441e trouble merging stuff 2017-10-16 20:28:15 +02:00
MaurizioBruetsch 367edd393b fixed post-merge problems 2017-10-16 20:11:48 +02:00
5 changed files with 227 additions and 30 deletions

View file

@ -9,7 +9,7 @@ JAVADOC_OPTIONS=-Xdoclint:all -private -encoding UTF-8 -charset UTF-8 -docencodi
BUILDS=builds
DOC=doc
CLASSPATH=src
LIBARIES=\
LIBRARIES=\
lib/jansi-1.16.jar
CLASSES=\
src/game/Main.java \
@ -22,6 +22,7 @@ src/player/Human.java \
src/player/malte/MalteAI.java \
src/player/malte/Position.java \
src/player/malte/Sequence.java \
src/player/malte/Item.java \
src/player/maurizio/MaurizioAI.java
OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class)
@ -36,13 +37,13 @@ P2=
all: $(CLASSES) $(FXMLS_BUILD)
$(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(BUILDS)
$(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH):$(LIBRARIES) $(CLASSES) -d $(BUILDS)
run: $(CLASSES)
$(JAVA) -cp $(BUILDS):$(LIBARIES) $(MAIN) $(P1) $(P2)
$(JAVA) -cp $(BUILDS):$(LIBRARIES) $(MAIN) $(P1) $(P2)
simulate: $(CLASSES)
$(JAVA) -cp $(BUILDS):$(LIBARIES) $(MAIN) $(N)
$(JAVA) -cp $(BUILDS):$(LIBRARIES) $(MAIN) $(N)
doc: $(CLASSES)
$(JAVADOC) $(JAVADOC_OPTIONS) -cp $(CLASSPATH):$(LIBARIES) $(CLASSES) -d $(DOC)

55
makeWindows Normal file
View file

@ -0,0 +1,55 @@
JAVA=java
JAVAC=javac
JAVADOC=javadoc
JAR=jar
JAVAC_OPTIONS=-Xlint:all
JAVADOC_OPTIONS=-Xdoclint:all -private -encoding UTF-8 -charset UTF-8 -docencoding UTF-8
BUILDS=builds
DOC=doc
CLASSPATH=src
LIBRARIES=\
lib/jansi-1.16.jar
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/Human.java \
src/player/malte/MalteAI.java \
src/player/malte/Position.java \
src/player/malte/Sequence.java \
src/player/malte/Item.java \
src/player/maurizio/MaurizioAI.java
OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class)
BUILDFILES=$(OBJECTS) $(FXMLS_BUILD)
MAIN=game.Main
N=100
P1=
P2=
all: $(CLASSES) $(FXMLS_BUILD)
$(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH);$(LIBRARIES) $(CLASSES) -d $(BUILDS)
run: $(CLASSES)
$(JAVA) -cp $(BUILDS);$(LIBRARIES) $(MAIN) $(P1) $(P2)
simulate: $(CLASSES)
$(JAVA) -cp $(BUILDS);$(LIBRARIES) $(MAIN) $(N)
doc: $(CLASSES)
$(JAVADOC) $(JAVADOC_OPTIONS) -cp $(CLASSPATH);$(LIBARIES) $(CLASSES) -d $(DOC)
jar: all
$(JAR) -cfe VierGewinnt.jar $(MAIN) -C $(BUILDS) .
clean:
rm -rf $(BUILDS)\* $(DOC)\* VierGewinnt.jar

View file

@ -154,13 +154,13 @@ public class Game {
if(output){
System.out.println(other(currentP).getP().getName() + " wins.");
}
return other(currentP).getID(); // TODO this only works if the players hav IDs 1 and 2
return other(currentP).getID(); // TODO only works if the players have IDs 1 and 2
}
}
// Uninstall Jansi
AnsiConsole.systemUninstall();
System.out.println("BUG!");
return checkState(false); // this is impossible to reach this, but it makes the compiler happy ;)
return checkState(false); // can't be reached
}
/**

View file

@ -47,6 +47,7 @@ public class MalteAI implements Player{
@Override
public void setPlayerID(int id) {
this.id = id;
this.enemyID = id == 1 ? 2: 1;
}
@Override
@ -64,6 +65,7 @@ public class MalteAI implements Player{
for (double d: weights) {
max = max > d ? max: d;
}
System.out.println(Arrays.toString(weights));
for (int i = 0; i < weights.length; i++) {
if (Math.abs(max - weights[i]) < 0.000001) {
return i;

View file

@ -2,6 +2,8 @@ package player.maurizio;
import java.util.Random;
import game.Game;
import java.util.List;
import java.util.LinkedList;
import player.Player;
@ -27,28 +29,110 @@ public class MaurizioAI implements Player {
public int move(int[][] board){
// check for one move wins
int m1 = winningMove(board, id);
if(m1 != -1){
return m1;
}
// create a list of all legal moves
LinkedList<Integer> moves1 = new LinkedList<Integer>();
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0){
moves1.add(i);
}
}
// filter out loosing moves
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0){
int[][] boardNew = makeMove(board, i, id);
int m2 = winningMove(boardNew, enemyID);
if(m2 != -1){
moves1.remove((Object) i);
}
}
}
//move randomly if instant defeat is inevitable
if(moves1.isEmpty()){
return moveRandom(board);
}
//forced move
if(moves1.size() == 1){
return moves1.peek();
}
//look for two move wins among the non-loosing moves
int m2 = winningMove2(board, id, moves1);
if(m2 != -1){
return m2;
}
//copy moves
LinkedList<Integer> moves2 = new LinkedList<Integer>();
for(int i : moves1){
moves2.add(i);
}
// avoid 2-move losses
for(int i : moves1){ // I make a move
int[][] board1 = makeMove(board, i, id);
int m3 = winningMove2(board1, enemyID, allMoves());
if(m3 != -1){
moves2.remove((Object) i);
}
}
if(!moves2.isEmpty()){
return moveCentral(board, moves2);
} else {
if(!moves1.isEmpty()){
return moveCentral(board, moves1);
}
}
return moveCentral(board); // can't be reached
}
private int winningMove(int [][] board, int id){
for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0 && checkWin(board, i, id)){
return i;
}
}
return -1;// signals that no winning move exists
}
private LinkedList<Integer> allMoves(){
LinkedList<Integer> ret = new LinkedList<Integer>();
for(int i=0;i<Game.GAME_COLUMNS;i++){
ret.add(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);
return ret;
}
private int winningMove2(int[][] board, int id, LinkedList<Integer> moves){
for(int i : moves){
if(board[i][0] != 0){
continue;
}
int[][] boardNew = makeMove(board, i, id);
boolean winner1 = true;
for(int j=0;j<Game.GAME_COLUMNS;j++){
if(boardNew[j][0] == 0){
int[][] boardNewNew = makeMove(boardNew, j, enemyID);
int m3 = winningMove(boardNewNew, id);
if(m3 == -1){
winner1 = false;
break;
}
}
}
if(winner1){
return i;
}
}
return -1; // no move found that wins in 2 moves
}
private int moveRandom(int[][] board){
@ -59,6 +143,60 @@ public class MaurizioAI implements Player {
return choice;
}
private int moveCentral(int[][] board){
if(board[3][0] == 0){
return 3;
}
if(board[2][0] == 0){
return 2;
}
if(board[4][0] == 0){
return 4;
}
if(board[1][0] == 0){
return 1;
}
if(board[5][0] == 0){
return 5;
}
if(board[0][0] == 0){
return 0;
}
if(board[6][0] == 0){
return 6;
}
return -1; // board is full
}
private int moveCentral(int[][] board, LinkedList<Integer> moves){
if (moves.isEmpty()) {
return -1; // empty list
}
if(moves.contains(3) && board[3][0] == 0){
return 3;
}
if(moves.contains(2) && board[2][0] == 0){
return 2;
}
if(moves.contains(4) && board[4][0] == 0){
return 4;
}
if(moves.contains(1) && board[1][0] == 0){
return 1;
}
if(moves.contains(5) && board[5][0] == 0){
return 5;
}
if(moves.contains(0) && board[0][0] == 0){
return 0;
}
if(moves.contains(6) && board[6][0] == 0){
return 6;
}
return -1; // Can' t happen
}
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) {
@ -70,7 +208,7 @@ public class MaurizioAI implements Player {
while (pos < (board[0].length - 1) && board[choice][pos + 1] == 0) {
pos++;
}
// Change the board accordingl
// Change the board accordingly
int[][] boardNew = copyBoard(board);
boardNew[choice][pos] = id;
return boardNew;
@ -89,10 +227,11 @@ public class MaurizioAI implements Player {
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
* checks whether the last move closed any lines on the board
*/
private boolean checkWin(int[][] board, int col, int id){
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){
@ -106,7 +245,7 @@ public class MaurizioAI implements Player {
win = false;
break;
}
if(j != -i && board[col+i+j][row] != id){
if(j != -i && board[col+i+j][row] != iD){
win = false;
break;
}
@ -122,7 +261,7 @@ public class MaurizioAI implements Player {
win = false;
break;
}
if(j != -i && board[col][row+i+j] != id){
if(j != -i && board[col][row+i+j] != iD){
win = false;
break;
}
@ -138,7 +277,7 @@ public class MaurizioAI implements Player {
win = false;
break;
}
if(j != -i && board[col+i+j][row-i-j] != id){
if(j != -i && board[col+i+j][row-i-j] != iD){
win = false;
break;
}
@ -154,7 +293,7 @@ public class MaurizioAI implements Player {
win = false;
break;
}
if(j != -i && board[col+i+j][row+i+j] != id){
if(j != -i && board[col+i+j][row+i+j] != iD){
win = false;
break;
}