Browse files

Improved the board scoring a bit, but still needs rework.

  • Loading branch information...
1 parent 331b889 commit 2199761ffbd59b4d687ddf188dfd4bbafb50046b @maherg committed Feb 23, 2012
Showing with 29 additions and 10 deletions.
  1. +29 −10 src/main/java/tictactoe/boards/planar/PlanarBoard.java
View
39 src/main/java/tictactoe/boards/planar/PlanarBoard.java
@@ -137,7 +137,7 @@ public boolean isDiagonalSequenceFound(String symbol, int sequenceSize) {
if (sequenceSize > largestPossibleDiagonalSize) {
return false;
}
-
+ // TODO : Rewrite this block to work on any dimension, it's now written just to check for 3x3.
if ((board[0][0].equals(symbol) && board[1][1].equals(symbol) && board[2][2].equals(symbol))
|| (board[2][0].equals(symbol) && board[1][1].equals(symbol) && board[0][2].equals(symbol))) {
log.debug("Diagonal sequence found for symbol {}", symbol);
@@ -210,37 +210,56 @@ private int heightIndexFor(int coordinate) {
@Override
public int scoreAnalysisFor(Player player) {
- return horizontalAnalysisFor(player) + verticalAnalysisFor(player) + diagonalAnalysisFor(player);
+ return Math.max(Math.max(horizontalAnalysisFor(player), verticalAnalysisFor(player)),
+ diagonalAnalysisFor(player));
}
private int horizontalAnalysisFor(Player player) {
int horizontalScore = 0;
for (int row = 0; row < height; row++) {
- horizontalScore += calculateAnalysisScoreFor(board[row], player);
+ horizontalScore = Math.max(horizontalScore, calculateAnalysisScoreFor(horizontalSlice(row), player));
}
return horizontalScore;
}
private int verticalAnalysisFor(Player player) {
int verticalScore = 0;
for (int column = 0; column < width; column++) {
- verticalScore += calculateAnalysisScoreFor(verticalSlice(column), player);
+ verticalScore = Math.max(verticalScore, calculateAnalysisScoreFor(verticalSlice(column), player));
}
return verticalScore;
}
private int diagonalAnalysisFor(Player player) {
// FIXME : Make the diagonal analysis work on all possible diagonals in rectangular dimensions as well.
- int diagonalScore = 0;
- diagonalScore += calculateAnalysisScoreFor(new String[] { board[0][0], board[1][1], board[2][2] }, player);
- diagonalScore += calculateAnalysisScoreFor(new String[] { board[0][2], board[1][1], board[2][0] }, player);
- return diagonalScore;
+ int diagonal_1 = calculateAnalysisScoreFor(new String[] { board[0][0], board[1][1], board[2][2] }, player);
+ int diagonal_2 = calculateAnalysisScoreFor(new String[] { board[0][2], board[1][1], board[2][0] }, player);
+ return Math.max(diagonal_1, diagonal_2);
}
private int calculateAnalysisScoreFor(String[] slice, Player player) {
+ int score = 0;
String sliceString = StringUtils.join(slice);
- int occurances = StringUtils.countMatches(sliceString, player.getSymbol());
- return occurances * ANALYSIS_FACTOR;
+ String opponentMoves = sliceString.replaceAll("[0-9" + player.getSymbol() + "]", "");
+ int opponentLength = opponentMoves.length();
+ int playerLength = StringUtils.countMatches(sliceString, player.getSymbol());
+ if (playerLength == 2) {
+ return 300;
+ }
+ if (playerLength == 1) {
+ return 200;
+ }
+ if (opponentLength == 1) {
+ return -50;
+ }
+ if (opponentLength == 2) {
+ return -100;
+ }
+ return score;
+ }
+
+ private String[] horizontalSlice(int rowIndex) {
+ return board[rowIndex];
}
private String[] verticalSlice(int columnIndex) {

0 comments on commit 2199761

Please sign in to comment.