diff --git a/src/player/malte/MalteAI.java b/src/player/malte/MalteAI.java index 9fcab0d..2450240 100644 --- a/src/player/malte/MalteAI.java +++ b/src/player/malte/MalteAI.java @@ -4,6 +4,7 @@ import java.util.Random; import java.util.Set; import java.util.HashSet; import java.util.Arrays; +import java.lang.Math; import player.Player; import game.Game; @@ -59,13 +60,116 @@ public class MalteAI implements Player{ for (int i = 0; i < weights.length; i++) { weights[i] = calculateWeight(board, i); } - return 0; + double max = -1; + 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.01) { + return i; + } + } + return 0; } public double calculateWeight(int[][] board, int row) { + /* The amount of equal ideas, stretching out in star-like form. + * ^ ^ ^ + * \ | / + * 701 + * <-6X2-> + * 543 + * / | \ + * V V V + * ids contains the id of the direction. + */ + int[] nrs = new int[8]; + int[] ids = new int[8]; + Position[] relAround = Position.getRelCirclePositions(); + Position thisPos = getLastEmpty(board, row); + if (thisPos == null) { + return -1.0; + } + for (int i = 0; i < nrs.length; i++) { + Position curPos = thisPos.add(relAround[i]); + if (isPosOnBoard(curPos)) { + ids[i] = getIDFromBoard(board, curPos); + nrs[i] = countIDsInDir(board, ids[i], curPos, relAround[i]); + } else { + ids[i] = -1; + nrs[i] = -1; + } + } + for (int i = 0; i < 8; i++) { + if (ids[i] == this.id && + nrs[i] == 3) { + return 1.0; + } else if (ids[i] == this.enemyID && + nrs[i] == 3) { + return 0.9; + } + } + for (int i = 0; i < 4; i++) { + if (ids[i] == ids[i + 4]) { + if (nrs[i] + nrs[i + 4] > 3) { + if (ids[i] == this.id) { + return 1.0; + } else if (ids[i] == this.enemyID) { + return 0.9; + } + } + } + } return 0.0; } + /** + * + */ + private Position getLastEmpty(int[][] board, int column) { + if (board[column][0] != 0) { + return null; + } + int row = 0; + while (row < Game.GAME_ROWS - 1 && + board[column][row + 1] == 0) { + row++; + } + return new Position(column, row); + } + + /** + * + */ + private int countIDsInDir(int[][] board, int id, Position pos, Position dir) { + if (isPosOnBoard(pos) && + board[pos.getPosX()][pos.getPosY()] == id) { + return countIDsInDir(board, id, pos.add(dir), dir) + 1; + } + return 0; + } + + /** + * + */ + private boolean isPosOnBoard(Position pos) { + if (pos.getPosX() <= Game.GAME_COLUMNS - 1 && + pos.getPosX() >= 0 && + pos.getPosY() <= Game.GAME_ROWS - 1 && + pos.getPosY() >= 0) { + return true; + } + return false; + } + + /** + * + */ + private int getIDFromBoard(int[][] board, Position pos) { + return board[pos.getPosX()][pos.getPosY()]; + } + /** * Copies the given board. * diff --git a/src/player/malte/Position.java b/src/player/malte/Position.java index 3f28b12..54d559f 100644 --- a/src/player/malte/Position.java +++ b/src/player/malte/Position.java @@ -20,8 +20,26 @@ public class Position { return this.posY; } + public Position add(Position pos) { + return new Position(this.getPosX() + pos.getPosX(), + this.getPosY() + pos.getPosY()); + } + @Override public String toString() { return String.format("(%d, %d)", posX, posY); } + + public static Position[] getRelCirclePositions() { + return new Position[]{ + new Position(0, -1), + new Position(1, -1), + new Position(1, 0), + new Position(1, 1), + new Position(0, 1), + new Position(-1, 1), + new Position(-1, 0), + new Position(-1, -1) + }; + } }