From 227cd1f628d8b1d173c9edcd81beb23ecf8d02c7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 11 Jul 2019 19:57:54 +0200 Subject: [PATCH] Added positional board evaluation --- src/dev/kske/chess/board/Board.java | 73 ++++++++++++++++++----- src/dev/kske/chess/ui/GameModeDialog.java | 6 +- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index 24594a3..93bd777 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -21,6 +21,43 @@ public class Board implements Cloneable { private Map kingPos; private Log log; + private static final Map positionScores; + + static { + positionScores = new HashMap<>(); + positionScores.put(Type.KING, + new int[][] { new int[] { -3, -4, -4, -5, -5, -4, -4, -3 }, + new int[] { -3, -4, -4, -5, -4, -4, -4, -3 }, new int[] { -3, -4, -4, -5, -4, -4, -4, -3 }, + new int[] { -3, -4, -4, -5, -4, -4, -4, -3 }, new int[] { -2, -3, -3, -2, -2, -2, -2, -1 }, + new int[] { -1, -2, -2, -2, -2, -2, -2, -1 }, new int[] { 2, 2, 0, 0, 0, 0, 2, 2 }, + new int[] { 2, 3, 1, 0, 0, 1, 3, 2 } }); + positionScores.put(Type.QUEEN, + new int[][] { new int[] { -2, -1, -1, -1, -1, -1, -1, -2 }, new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, + new int[] { -1, 0, 1, 1, 1, 1, 0, -1 }, new int[] { -1, 0, 1, 1, 1, 1, 0, -1 }, + new int[] { 0, 0, 1, 1, 1, 1, 0, -1 }, new int[] { -1, 1, 1, 1, 1, 1, 0, -1 }, + new int[] { -1, 0, 1, 0, 0, 0, 0, -1 }, new int[] { -2, -1, -1, -1, -1, -1, -1, -2 } }); + positionScores.put(Type.ROOK, + new int[][] { new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1 }, + new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, + new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, + new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, new int[] { 0, 0, 0, 1, 1, 0, 0, 0 } }); + positionScores.put(Type.KNIGHT, + new int[][] { new int[] { -5, -4, -3, -3, -3, -3, -4, -5 }, new int[] { -4, -2, 0, 0, 0, 0, -2, -4 }, + new int[] { -3, 0, 1, 2, 2, 1, 0, -3 }, new int[] { -3, 1, 2, 2, 2, 2, 1, -3 }, + new int[] { -3, 0, 2, 2, 2, 2, 0, -1 }, new int[] { -3, 1, 1, 2, 2, 1, 1, -3 }, + new int[] { -4, -2, 0, 1, 1, 0, -2, -4 }, new int[] { -5, -4, -3, -3, -3, -3, -4, -5 } }); + positionScores.put(Type.BISHOP, + new int[][] { new int[] { -2, -1, -1, -1, -1, -1, -1, 2 }, new int[] { -1, 0, 0, 0, 0, 0, 0, -1 }, + new int[] { -1, 0, 1, 1, 1, 1, 0, -1 }, new int[] { -1, 1, 1, 1, 1, 1, 1, -1 }, + new int[] { -1, 0, 1, 1, 1, 1, 0, -1 }, new int[] { -1, 1, 1, 1, 1, 1, 1, -1 }, + new int[] { -1, 1, 0, 0, 0, 0, 1, -1 }, new int[] { -2, -1, -1, -1, -1, -1, -2 } }); + positionScores.put(Type.PAWN, + new int[][] { new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 5, 5, 5, 5, 5, 5, 5, 5 }, + new int[] { 1, 1, 2, 3, 3, 2, 1, 1 }, new int[] { 0, 0, 1, 3, 3, 1, 0, 0 }, + new int[] { 0, 0, 0, 2, 2, 0, 0, 0 }, new int[] { 0, 0, -1, 0, 0, -1, 0, 0 }, + new int[] { 0, 1, 1, -2, -2, 1, 1, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 } }); + } + public Board() { boardArr = new Piece[8][8]; kingPos = new HashMap<>(); @@ -182,22 +219,26 @@ public class Board implements Cloneable { int score = 0; for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) - if (boardArr[i][j] != null && boardArr[i][j].getColor() == color) switch (boardArr[i][j].getType()) { - case QUEEN: - score += 8; - break; - case ROOK: - score += 5; - break; - case KNIGHT: - score += 3; - break; - case BISHOP: - score += 3; - break; - case PAWN: - score += 1; - break; + if (boardArr[i][j] != null && boardArr[i][j].getColor() == color) { + switch (boardArr[i][j].getType()) { + case QUEEN: + score += 90; + break; + case ROOK: + score += 50; + break; + case KNIGHT: + score += 30; + break; + case BISHOP: + score += 30; + break; + case PAWN: + score += 10; + break; + } + if (positionScores.containsKey(boardArr[i][j].getType())) + score += positionScores.get(boardArr[i][j].getType())[i][color == Color.WHITE ? j : 7 - j]; } return score; } diff --git a/src/dev/kske/chess/ui/GameModeDialog.java b/src/dev/kske/chess/ui/GameModeDialog.java index f9d7dcf..aa65134 100644 --- a/src/dev/kske/chess/ui/GameModeDialog.java +++ b/src/dev/kske/chess/ui/GameModeDialog.java @@ -53,7 +53,7 @@ public class GameModeDialog extends JDialog { btnAI.addActionListener((evt) -> { Map players = new HashMap<>(); players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent)); - players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 4)); + players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 5)); new Game(players, boardComponent).start(); dispose(); }); @@ -62,8 +62,8 @@ public class GameModeDialog extends JDialog { JButton btnAI2 = new JButton("AI against AI"); btnAI2.addActionListener((evt) -> { Map players = new HashMap<>(); - players.put(Color.WHITE, new AIPlayer(board, Color.WHITE, 4)); - players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 3)); + players.put(Color.WHITE, new AIPlayer(board, Color.WHITE, 5)); + players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 4)); new Game(players, boardComponent).start(); dispose(); });