Compare commits

..

1 commit

Author SHA1 Message Date
Malte Tammena f8067ecc18 Preventing tower-building 2017-10-27 10:36:51 +02:00
6 changed files with 64 additions and 260 deletions

View file

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

View file

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

View file

@ -47,7 +47,6 @@ public class MalteAI implements Player{
@Override @Override
public void setPlayerID(int id) { public void setPlayerID(int id) {
this.id = id; this.id = id;
this.enemyID = id == 1 ? 2: 1;
} }
@Override @Override
@ -65,7 +64,6 @@ public class MalteAI implements Player{
for (double d: weights) { for (double d: weights) {
max = max > d ? max: d; max = max > d ? max: d;
} }
System.out.println(Arrays.toString(weights));
for (int i = 0; i < weights.length; i++) { for (int i = 0; i < weights.length; i++) {
if (Math.abs(max - weights[i]) < 0.000001) { if (Math.abs(max - weights[i]) < 0.000001) {
return i; return i;
@ -78,8 +76,6 @@ public class MalteAI implements Player{
double W_ILLEGAL = Double.MIN_VALUE; double W_ILLEGAL = Double.MIN_VALUE;
double W_DONT = 0.01; double W_DONT = 0.01;
double weight = 1.0; double weight = 1.0;
// Prever a place in the center;
weight *= 1.0 - Math.abs(3.0 - column) / (3.0 * 100.0);
// Relative positions like (-1, -1), (0, -1), etc. // Relative positions like (-1, -1), (0, -1), etc.
Position[] relAround = Position.getRelCirclePositions(); Position[] relAround = Position.getRelCirclePositions();
// The position we're looking at // The position we're looking at
@ -89,6 +85,9 @@ public class MalteAI implements Player{
weight *= W_ILLEGAL; weight *= W_ILLEGAL;
return weight; return weight;
} }
// Prever a place in the center;
weight *= 1.0 - Math.abs(3.0 - thisPos.getPosX()) / (3.0 * 100.0);
weight *= 1.0 - Math.abs(5.0 - thisPos.getPosY()) / (5.0 * 100.0);
// Setup Sequences // Setup Sequences
Sequence[] sequences = new Sequence[8]; Sequence[] sequences = new Sequence[8];
Sequence[] sequencesAbove = new Sequence[8]; Sequence[] sequencesAbove = new Sequence[8];
@ -112,32 +111,32 @@ public class MalteAI implements Player{
for (int i = 0; i < sequences.length / 2; i++) { for (int i = 0; i < sequences.length / 2; i++) {
Sequence x = sequences[i]; Sequence x = sequences[i];
Sequence y = sequences[i + 4]; Sequence y = sequences[i + 4];
weight *= getBaseWeightForSequences(x, y); weight *= getBaseWeightForSequences(x, y, false);
Sequence xA = sequencesAbove[i]; Sequence xA = sequencesAbove[i];
Sequence yA = sequencesAbove[i + 4]; Sequence yA = sequencesAbove[i + 4];
// weight *= getBaseWeightForSequences(xA, yA); weight *= Math.sqrt(getBaseWeightForSequences(xA, yA, true));
if ((xA.matches("EEE") ||
yA.matches("EEE")) ||
((xA.matches("E") &&
yA.matches("EE")) ||
(xA.matches("EE") &&
yA.matches("E")))) {
// TODO: Do this right!
weight /= 2<<6;
}
} }
return weight; return weight;
} }
private double getBaseWeightForSequences(Sequence x, Sequence y) { /**
double W_PRIORITY_1 = 2 << 8; *
double W_PRIORITY_2 = 2 << 6; * @param altWeight Weights enemy advances negativ.
double W_PRIORITY_3 = 2 << 4; */
double W_PRIORITY_4 = 2 << 2; private double getBaseWeightForSequences(Sequence x, Sequence y, boolean altWeight) {
double W_PRIORITY_5 = 2 << 0; double W_PRIORITY_1 = 1 << 8;
double W_PRIORITY_2 = 1 << 6;
double W_PRIORITY_3 = 1 << 4;
double W_PRIORITY_4 = 1 << 2;
double W_PRIORITY_5 = 1 << 0;
double W_PRIORITY_6 = 1.0; double W_PRIORITY_6 = 1.0;
double W_START = 1.0; double W_START = 1.0;
double weight = W_START; double weight = W_START;
if (altWeight) {
W_PRIORITY_2 = 1 / 4;
W_PRIORITY_4 = 1 / 3;
W_PRIORITY_6 = 1 / 2;
}
if (x.matches("MMM") || if (x.matches("MMM") ||
y.matches("MMM")) { y.matches("MMM")) {
weight *= W_PRIORITY_1; weight *= W_PRIORITY_1;

View file

@ -1,14 +1,14 @@
1 XMMM // Vierter in der Reihe + 1 XMMM // Vierter in der Reihe
1 MXMM + 1 MXMM
2 XEEE // Vierter beim Gegner - 2 XEEE // Vierter beim Gegner
2 EXEE - 2 EXEE
3 _XMM_ // Sicherer Sieg nächste Runde + 3 _XMM_ // Sicherer Sieg nächste Runde
3 _MXM_ + 3 _MXM_
4 _XEE_ // Sicherer Sieg des Gegners nächste Runde - 4 _XEE_ // Sicherer Sieg des Gegners nächste Runde
4 _EXE_ - 4 _EXE_
5 XMM_ // Möglicher Sieg nächste Runde + 5 XMM_ // Möglicher Sieg nächste Runde
5 _XMM + 5 _XMM
5 MXM_ + 5 MXM_
6 XEE_ // Möglicher Sieg des Gegners in der nächsten Runde - 6 XEE_ // Möglicher Sieg des Gegners in der nächsten Runde
6 _XEE - 6 _XEE
6 EXE_ - 6 EXE_

View file

@ -2,8 +2,6 @@ package player.maurizio;
import java.util.Random; import java.util.Random;
import game.Game; import game.Game;
import java.util.List;
import java.util.LinkedList;
import player.Player; import player.Player;
@ -29,110 +27,28 @@ public class MaurizioAI implements Player {
public int move(int[][] board){ public int move(int[][] board){
// check for one move wins // 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++){ for(int i=0;i<Game.GAME_COLUMNS;i++){
if(board[i][0] == 0 && checkWin(board, i, id)){ if(board[i][0] == 0 && checkWin(board, i, id)){
return i; return i;
} }
} }
return -1;// signals that no winning move exists //if no wins were found try avoiding losing on the next move
} // for(int i=0;i<Game.GAME_COLUMNS;i++){
// boolean loser = false;
private LinkedList<Integer> allMoves(){ // if(board[i][0] == 0){
LinkedList<Integer> ret = new LinkedList<Integer>(); // int[][] boardNew = makeMove(board, i, id);
for(int i=0;i<Game.GAME_COLUMNS;i++){ // for(int j=0;j<Game.GAME_COLUMNS;j++){
ret.add(i); // if(boardNew[j][0] != 0 && checkWin(boardNew, j, 1)){
} // loser = true;
return ret; // break;
} // }
// }
private int winningMove2(int[][] board, int id, LinkedList<Integer> moves){ // if(!loser){
for(int i : moves){ // return i;
if(board[i][0] != 0){ // }
continue; // }
} // }
int[][] boardNew = makeMove(board, i, id); return moveRandom(board);
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){ private int moveRandom(int[][] board){
@ -143,60 +59,6 @@ public class MaurizioAI implements Player {
return choice; 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) { private int[][] makeMove(int[][] board, int choice, int id) {
// Check his choice against the current board. // Check his choice against the current board.
if (choice < 0 || choice > Game.GAME_COLUMNS || board[choice][0] != 0) { if (choice < 0 || choice > Game.GAME_COLUMNS || board[choice][0] != 0) {
@ -208,7 +70,7 @@ public class MaurizioAI implements Player {
while (pos < (board[0].length - 1) && board[choice][pos + 1] == 0) { while (pos < (board[0].length - 1) && board[choice][pos + 1] == 0) {
pos++; pos++;
} }
// Change the board accordingly // Change the board accordingl
int[][] boardNew = copyBoard(board); int[][] boardNew = copyBoard(board);
boardNew[choice][pos] = id; boardNew[choice][pos] = id;
return boardNew; return boardNew;
@ -227,11 +89,10 @@ public class MaurizioAI implements Player {
private boolean withinBoard(int x, int y){ private boolean withinBoard(int x, int y){
return ((x>=0 && x < Game.GAME_COLUMNS) && (y>=0 && y < Game.GAME_ROWS)); 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 // find row of last move -- assuming the move was legal
int row = 0; int row = 0;
while(row < Game.GAME_ROWS-1 && board[col][row+1] == 0){ while(row < Game.GAME_ROWS-1 && board[col][row+1] == 0){
@ -245,7 +106,7 @@ public class MaurizioAI implements Player {
win = false; win = false;
break; break;
} }
if(j != -i && board[col+i+j][row] != iD){ if(j != -i && board[col+i+j][row] != id){
win = false; win = false;
break; break;
} }
@ -261,7 +122,7 @@ public class MaurizioAI implements Player {
win = false; win = false;
break; break;
} }
if(j != -i && board[col][row+i+j] != iD){ if(j != -i && board[col][row+i+j] != id){
win = false; win = false;
break; break;
} }
@ -277,7 +138,7 @@ public class MaurizioAI implements Player {
win = false; win = false;
break; 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; win = false;
break; break;
} }
@ -293,7 +154,7 @@ public class MaurizioAI implements Player {
win = false; win = false;
break; 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; win = false;
break; break;
} }