From bd0fc7edfd20c4c1cb52f44409a914c77d53a30a Mon Sep 17 00:00:00 2001 From: william Date: Fri, 24 Mar 2023 14:36:13 -0400 Subject: [PATCH] Fixes? --- .idea/misc.xml | 2 +- .idea/workspace.xml | 59 ++++-- src/main/java/laboratoire4/Client.java | 11 +- src/main/java/laboratoire4/IPawn.java | 13 ++ src/main/java/laboratoire4/MiniMax.java | 208 +++++++++++-------- src/main/java/laboratoire4/MovingBoard.java | 86 ++++++++ src/main/java/laboratoire4/MovingPawn.java | 14 ++ src/main/java/laboratoire4/MovingPushed.java | 25 +++ src/main/java/laboratoire4/MovingPusher.java | 24 +++ src/main/java/laboratoire4/Pawn.java | 16 +- src/main/java/laboratoire4/Player.java | 20 +- src/main/java/laboratoire4/Pushed.java | 26 +-- src/main/java/laboratoire4/Pusher.java | 21 +- src/main/java/laboratoire4/PusherBoard.java | 105 +++------- 14 files changed, 413 insertions(+), 217 deletions(-) create mode 100644 src/main/java/laboratoire4/IPawn.java create mode 100644 src/main/java/laboratoire4/MovingBoard.java create mode 100644 src/main/java/laboratoire4/MovingPawn.java create mode 100644 src/main/java/laboratoire4/MovingPushed.java create mode 100644 src/main/java/laboratoire4/MovingPusher.java diff --git a/.idea/misc.xml b/.idea/misc.xml index accd629..062a7aa 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 471e4ff..9b1e07b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,10 +4,20 @@ - + + + + + + + + + + + @@ -20,13 +30,14 @@ @@ -43,6 +54,9 @@ + + @@ -51,21 +65,21 @@ - { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "codeWithMe.voiceChat.enabledByDefault": "false", + "last_opened_file_path": "/home/william/Dev/Projects", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" } -}]]> +} - - - file://$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java - 60 - - + + + + \ No newline at end of file diff --git a/src/main/java/laboratoire4/Client.java b/src/main/java/laboratoire4/Client.java index 2f486be..d42ce78 100644 --- a/src/main/java/laboratoire4/Client.java +++ b/src/main/java/laboratoire4/Client.java @@ -76,7 +76,7 @@ public class Client { board.move(previousMove); } -// Thread.sleep(1000); + Thread.sleep(500); String nextMove = board.runNextMove(); System.out.println("Prochain mouvement: " + nextMove); @@ -85,15 +85,18 @@ public class Client { output.flush(); } - private void handleInvalidMove() throws InterruptedException { + private void handleInvalidMove() throws InterruptedException, IOException { System.err.println("Mouvement invalide!"); Thread.sleep(500); - board.printBoard(); + + PusherBoard.printBoard(board.getBoard()); + +// play(); } private void stopGame() throws IOException { System.out.println("GG well played!"); - socket.close(); +// socket.close(); } } diff --git a/src/main/java/laboratoire4/IPawn.java b/src/main/java/laboratoire4/IPawn.java new file mode 100644 index 0000000..f3eefb1 --- /dev/null +++ b/src/main/java/laboratoire4/IPawn.java @@ -0,0 +1,13 @@ +package laboratoire4; + +public interface IPawn { + boolean isMoveValid(IPawn[][] board, Pawn.PawnMovement movement); + void move(Pawn.PawnMovement movement); + boolean isPusher(); + int getDirection(); + Player getPlayer(); + int getRow(); + int getCol(); + void setRow(int row); + void setCol(int col); +} diff --git a/src/main/java/laboratoire4/MiniMax.java b/src/main/java/laboratoire4/MiniMax.java index f535e5e..5506734 100644 --- a/src/main/java/laboratoire4/MiniMax.java +++ b/src/main/java/laboratoire4/MiniMax.java @@ -1,112 +1,141 @@ package laboratoire4; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Random; - public class MiniMax { - private static final int MAX_DEPTH = 2; - private static Random random = new Random(); + private static final int MAX_DEPTH = 1; public static MiniMaxResult miniMax(PusherBoard board) { - return miniMax(board, true, 0, 0); + MovingBoard game = new MovingBoard(board.getBoard(), board.getPlayer()); + return miniMax(game, true, 0, Integer.MIN_VALUE, Integer.MAX_VALUE); } - private static MiniMaxResult miniMax(PusherBoard board, boolean max, int depth, int alphaBeta) { - int limScore = max ? Integer.MIN_VALUE : Integer.MAX_VALUE; - List pawns = max ? - board.getMaxPawns() : - board.getMinPawns(); -// Collections.shuffle(pawns); + private static MiniMaxResult miniMax(MovingBoard game, boolean max, int depth, int alpha, int beta) { + depth += 1; - List results = new ArrayList<>(); + if (max) { + return max(game, depth, alpha, beta); + } - for (Pawn pawn : pawns) { - int originalRow = pawn.getRow(); - int originalCol = pawn.getCol(); + return min(game, depth, alpha, beta); + } + private static MiniMaxResult max(MovingBoard game, int depth, int alpha, int beta) { + int alphaT = Integer.MIN_VALUE; + MovingPawn lastPawn = null; + Pawn.PawnMovement lastMovement = null; + + for (MovingPawn pawn : game.getMaxPawns()) { for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) { - if (pawn.isMoveValid(board, movement)) { - pawn.move(movement); - - int nextAlphaBeta = max ? Math.max(limScore, alphaBeta) : Math.min(limScore, alphaBeta); - - int score = evaluate(pawn, board, max); - if (depth < MAX_DEPTH) { - score = miniMax(board, !max, depth + 1, nextAlphaBeta).getScore(); - } - - if ((max && score > limScore) || - (!max && score < limScore)) { - limScore = score; - } - - MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement); - - //elagage alphaBeta -// if ((max && limScore > alphaBeta) || -// (!max && limScore < alphaBeta)) { -// pawn.setRow(originalRow); -// pawn.setCol(originalCol); -// -// return result; -// } - - results.add(result); + if (!pawn.isMoveValid(game.getBoard(), movement)) { + continue; } - pawn.setRow(originalRow); - pawn.setCol(originalCol); + lastPawn = pawn; + lastMovement = movement; + + game.move(pawn, movement); + + int score = evaluate(pawn, game, depth); + if (depth < MAX_DEPTH) { + score = miniMax(game, false, depth, Math.max(alpha, alphaT), beta).getScore(); + } + + game.revertMove(); + + if (score > alphaT) { + alphaT = score; + } + + if (alphaT >= beta) { + return new MiniMaxResult(alphaT, pawn.getRow(), pawn.getCol(), movement); + } } } - // Choisir aléatoirement - int index = random.nextInt(results.size()); - return results.get(index); + if (lastPawn == null) { + return new MiniMaxResult(alphaT, 0, 0, null); + } + return new MiniMaxResult(alphaT, lastPawn.getRow(), lastPawn.getCol(), lastMovement); } - private static int evaluate(Pawn pawn, PusherBoard board, boolean max) { - int score = didWin(pawn, max) + didCapture(pawn, board, max) + isPushed(pawn); + private static MiniMaxResult min(MovingBoard game, int depth, int alpha, int beta) { + int betaT = Integer.MAX_VALUE; + MovingPawn lastPawn = null; + Pawn.PawnMovement lastMovement = null; - int homeRow = pawn.getPlayer() == Player.RED ? 0 : 7; - if (pawn.getRow() != homeRow) { - score += 50; + for (MovingPawn pawn : game.getMinPawns()) { + for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) { + if (!pawn.isMoveValid(game.getBoard(), movement)) { + continue; + } + + lastPawn = pawn; + lastMovement = movement; + + game.move(pawn, movement); + + int score = evaluate(pawn, game, depth); + if (depth < MAX_DEPTH) { + score = miniMax(game, true, depth, alpha, Math.min(betaT, beta)).getScore(); + } + + game.revertMove(); + + if (score < betaT) { + betaT = score; + } + + if (betaT <= beta) { + return new MiniMaxResult(betaT, pawn.getRow(), pawn.getCol(), movement); + } + } } - int direction = max ? 1 : -1; - return score * direction; + if (lastPawn == null) { + return new MiniMaxResult(betaT, 0, 0, null); + } + return new MiniMaxResult(betaT, lastPawn.getRow(), lastPawn.getCol(), lastMovement); } - private static int isPushed(Pawn pawn) { - if (pawn instanceof Pushed) { - return 5; + private static int evaluate(IPawn pawn, MovingBoard game, int depth) { + int score = didWin(pawn) + didCapture(pawn, game); + + score += pawn.isPusher() ? 0 : 5; + + int row = pawn.getRow(); + int col = pawn.getCol(); + int nextNextRow = row + pawn.getDirection() * 2; + + // Attire nos pusher vers nos pushed + if (pawn.isPusher() && nextNextRow >= 0 && nextNextRow <= 7) { + for (Pawn.PawnMovement move : Pawn.PawnMovement.values()) { + int nextCol = col + move.getMove(); + if (nextCol < 0 || nextCol > 7) { + continue; + } + + IPawn nearPawn = game.getBoard()[nextNextRow][nextCol]; + if (nearPawn != null && !nearPawn.isPusher() && nearPawn.getPlayer() == pawn.getPlayer()) { + score += 5; + } + } + } + + return score * pawn.getDirection(); + } + + private static int didWin(IPawn pawn) { + if (pawn.getRow() == pawn.getPlayer().getGoal()) { + return 100; } return 0; } - private static int didWin(Pawn pawn, boolean max) { - int goal = pawn.getPlayer() == Player.RED ? 7 : 0; + private static int didCapture(IPawn pawn, MovingBoard game) { + IPawn capturedPawn = game.getBoard()[pawn.getRow()][pawn.getCol()]; - if (pawn.getRow() == goal) { - return max ? 1000 : 100000; - } - - return 0; - } - - private static int didCapture(Pawn pawn, PusherBoard board, boolean max) { - Pawn capturedPawn = board.getBoard()[pawn.getRow()][pawn.getCol()]; - - if (capturedPawn != null) { - if (capturedPawn.getPlayer() != pawn.getPlayer()) { - if (max) { - return capturedPawn instanceof Pusher ? 100 : 50; - } else { - return capturedPawn instanceof Pusher ? 10000 : 5000; - } - } + if (capturedPawn != null && capturedPawn.getPlayer() != game.getPlayer()) { + return 500; } return 0; @@ -114,12 +143,14 @@ public class MiniMax { static class MiniMaxResult { private final int score; - private final Pawn pawn; + private final int row; + private final int col; private final Pawn.PawnMovement movement; - public MiniMaxResult(int score, Pawn pawn, Pawn.PawnMovement movement) { + public MiniMaxResult(int score, int row, int col, Pawn.PawnMovement movement) { this.score = score; - this.pawn = pawn; + this.row = row; + this.col = col; this.movement = movement; } @@ -127,8 +158,12 @@ public class MiniMax { return score; } - public Pawn getPawn() { - return pawn; + public int getRow() { + return row; + } + + public int getCol() { + return col; } public Pawn.PawnMovement getMovement() { @@ -139,7 +174,8 @@ public class MiniMax { public String toString() { return "MiniMaxResult{" + "score=" + score + - ", pawn=" + pawn + + ", col=" + col + + ", row=" + row + ", movement=" + movement + '}'; } diff --git a/src/main/java/laboratoire4/MovingBoard.java b/src/main/java/laboratoire4/MovingBoard.java new file mode 100644 index 0000000..4f267ef --- /dev/null +++ b/src/main/java/laboratoire4/MovingBoard.java @@ -0,0 +1,86 @@ +package laboratoire4; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Stack; +import java.util.function.Predicate; + +public class MovingBoard { + private final MovingPawn[][] board; + private final Player player; + + private final Stack removedPawns = new Stack<>(); + private final Stack previousMovedPawns = new Stack<>(); + + public MovingBoard(Pawn[][] board, Player player) { + this.board = asMovingPawns(board); + this.player = player; + } + + public void move(MovingPawn pawn, Pawn.PawnMovement movement) { + int toRow = pawn.getRow() + pawn.getDirection(); + int toCol = pawn.getCol() + movement.getMove(); + MovingPawn capturedPawn = board[toRow][toCol]; + + previousMovedPawns.push(pawn); + removedPawns.push(capturedPawn); + + board[pawn.getRow()][pawn.getCol()] = null; + board[toRow][toCol] = pawn; + pawn.move(movement); + } + + public void revertMove() { + MovingPawn pawn = previousMovedPawns.pop(); + MovingPawn capturedPawn = removedPawns.pop(); + + board[pawn.getRow()][pawn.getCol()] = capturedPawn; + + pawn.revertMove(); + board[pawn.getRow()][pawn.getCol()] = pawn; + } + + public MovingPawn[][] getBoard() { + return board; + } + + public Player getPlayer() { + return player; + } + + public Collection getMaxPawns() { + return getPawns(p -> p.getPlayer() == player); + } + + public Collection getMinPawns() { + return getPawns(p -> p.getPlayer() != player); + } + + private Collection getPawns(Predicate predicate) { + List pawns = new ArrayList<>(); + for (MovingPawn[] row : board) { + for (MovingPawn pawn : row) { + if (pawn != null && predicate.test(pawn)) { + pawns.add(pawn); + } + } + } + return pawns; + } + + public static MovingPawn[][] asMovingPawns(Pawn[][] board) { + MovingPawn[][] to = new MovingPawn[board.length][board.length]; + for (int row = 0; row < board.length; row++) { + for (int col = 0; col < board.length; col++) { + if (board[row][col] == null) { + continue; + } + + MovingPawn pawn = MovingPawn.from(board[row][col]); + to[row][col] = pawn; + } + } + return to; + } +} diff --git a/src/main/java/laboratoire4/MovingPawn.java b/src/main/java/laboratoire4/MovingPawn.java new file mode 100644 index 0000000..3d35162 --- /dev/null +++ b/src/main/java/laboratoire4/MovingPawn.java @@ -0,0 +1,14 @@ +package laboratoire4; + +public interface MovingPawn extends IPawn { + void revertMove(); + + static MovingPawn from(Pawn pawn) { + if (pawn instanceof Pusher) { + return new MovingPusher(pawn.getPlayer(), pawn.getRow(), pawn.getCol()); + } + + return new MovingPushed(pawn.getPlayer(), pawn.getRow(), pawn.getCol()); + } +} + diff --git a/src/main/java/laboratoire4/MovingPushed.java b/src/main/java/laboratoire4/MovingPushed.java new file mode 100644 index 0000000..b018334 --- /dev/null +++ b/src/main/java/laboratoire4/MovingPushed.java @@ -0,0 +1,25 @@ +package laboratoire4; + +import java.util.Stack; + +public class MovingPushed extends Pushed implements MovingPawn { + private final Stack previousMoves = new Stack<>(); + + public MovingPushed(Player player, int row, int col) { + super(player, row, col); + } + + @Override + public void move(PawnMovement movement) { + super.move(movement); + previousMoves.push(movement); + } + + @Override + public void revertMove() { + PawnMovement move = previousMoves.pop(); + setRow(row - getDirection()); + setCol(col - move.getMove()); + } + +} diff --git a/src/main/java/laboratoire4/MovingPusher.java b/src/main/java/laboratoire4/MovingPusher.java new file mode 100644 index 0000000..1a43458 --- /dev/null +++ b/src/main/java/laboratoire4/MovingPusher.java @@ -0,0 +1,24 @@ +package laboratoire4; + +import java.util.Stack; + +public class MovingPusher extends Pusher implements MovingPawn { + private final Stack previousMoves = new Stack<>(); + + public MovingPusher(Player player, int row, int col) { + super(player, row, col); + } + + @Override + public void move(PawnMovement movement) { + previousMoves.push(movement); + super.move(movement); + } + + @Override + public void revertMove() { + PawnMovement move = previousMoves.pop(); + setRow(row - getDirection()); + setCol(col - move.getMove()); + } +} diff --git a/src/main/java/laboratoire4/Pawn.java b/src/main/java/laboratoire4/Pawn.java index bd6b8d9..801b93a 100644 --- a/src/main/java/laboratoire4/Pawn.java +++ b/src/main/java/laboratoire4/Pawn.java @@ -1,20 +1,18 @@ package laboratoire4; -public abstract class Pawn { +public abstract class Pawn implements IPawn { protected final Player player; protected int row; protected int col; - protected int direction; public Pawn(Player player, int row, int col) { this.player = player; this.row = row; this.col = col; - this.direction = player == Player.RED ? 1 : -1; } public void move(PawnMovement movement) { - setRow(row + direction); + setRow(row + player.getDirection()); setCol(col + movement.move); } @@ -44,12 +42,10 @@ public abstract class Pawn { } public int getDirection() { - return direction; + return player.getDirection(); } - public boolean isMoveValid(PusherBoard game, PawnMovement movement) { - Pawn[][] board = game.getBoard(); - + public boolean isMoveValid(IPawn[][] board, PawnMovement movement) { int nextRow = row + getDirection(); if (nextRow < 0 || nextRow >= board.length) { return false; @@ -60,10 +56,10 @@ public abstract class Pawn { return false; } - return isMoveValid(board, movement); + return isMoveReallyValid(board, movement); } - protected abstract boolean isMoveValid(Pawn[][] board, PawnMovement movement); + protected abstract boolean isMoveReallyValid(IPawn[][] board, PawnMovement movement); enum PawnMovement { STRAIGHT(0), diff --git a/src/main/java/laboratoire4/Player.java b/src/main/java/laboratoire4/Player.java index a94952b..1c75464 100644 --- a/src/main/java/laboratoire4/Player.java +++ b/src/main/java/laboratoire4/Player.java @@ -1,6 +1,22 @@ package laboratoire4; public enum Player { - BLACK, - RED + BLACK(-1, 0), + RED(1, 7); + + private int direction; + private int goal; + + Player(int direction, int goal) { + this.direction = direction; + this.goal = goal; + } + + public int getDirection() { + return direction; + } + + public int getGoal() { + return goal; + } } diff --git a/src/main/java/laboratoire4/Pushed.java b/src/main/java/laboratoire4/Pushed.java index 8a69b3a..4398a2d 100644 --- a/src/main/java/laboratoire4/Pushed.java +++ b/src/main/java/laboratoire4/Pushed.java @@ -6,29 +6,31 @@ public class Pushed extends Pawn { } @Override - public boolean isMoveValid(Pawn[][] board, PawnMovement movement) { - Pawn pusher = null; - Pawn to = board[row + direction][col + movement.getMove()]; + public boolean isPusher() { + return false; + } + + @Override + public boolean isMoveReallyValid(IPawn[][] board, PawnMovement movement) { + int direction = getDirection(); + IPawn pusher = null; + IPawn to = board[row + direction][col + movement.getMove()]; if (col > 0 && movement == PawnMovement.RIGHT_DIAGONAL) { - pusher = board[row - direction][col - 1]; + pusher = board[row - direction][col - movement.getMove()]; } else if (col < board.length - 1 && movement == PawnMovement.LEFT_DIAGONAL) { - pusher = board[row - direction][col + 1]; + pusher = board[row - direction][col - movement.getMove()]; } else if (movement == PawnMovement.STRAIGHT) { pusher = board[row - direction][col]; } - boolean pusherValid = pusher instanceof Pusher && pusher.player == player; - boolean destinationValid = to == null || (movement != PawnMovement.STRAIGHT && to.player != this.player); + boolean pusherValid = pusher != null && pusher.isPusher() && pusher.getPlayer() == player; + boolean destinationValid = to == null || (movement != PawnMovement.STRAIGHT && to.getPlayer() != this.player); return pusherValid && destinationValid; } @Override public String toString() { - return "Pushed{" + - player + - ", " + col + - ", " + row + - "} "; + return String.format("Pushed{%s, %s}", player, getPosition()); } } diff --git a/src/main/java/laboratoire4/Pusher.java b/src/main/java/laboratoire4/Pusher.java index 01e4fbc..28d3b0d 100644 --- a/src/main/java/laboratoire4/Pusher.java +++ b/src/main/java/laboratoire4/Pusher.java @@ -6,22 +6,27 @@ public class Pusher extends Pawn { } @Override - public boolean isMoveValid(Pawn[][] board, PawnMovement movement) { - Pawn to = board[row + direction][col + movement.getMove()]; + public boolean isPusher() { + return true; + } + + @Override + public boolean isMoveReallyValid(IPawn[][] board, PawnMovement movement) { + IPawn to = board[getRow() + getDirection()][getCol() + movement.getMove()]; if (to == null) { return true; } - return to.player != this.player && movement != PawnMovement.STRAIGHT; + if (to.getPlayer() == player) { + return false; + } + + return movement != PawnMovement.STRAIGHT; } @Override public String toString() { - return "Pusher{" + - player + - ", " + col + - ", " + row + - "} "; + return String.format("Pusher{%s, %s}", player, getPosition()); } } diff --git a/src/main/java/laboratoire4/PusherBoard.java b/src/main/java/laboratoire4/PusherBoard.java index c2af373..636b603 100644 --- a/src/main/java/laboratoire4/PusherBoard.java +++ b/src/main/java/laboratoire4/PusherBoard.java @@ -1,15 +1,9 @@ package laboratoire4; -import java.util.ArrayList; -import java.util.List; - public class PusherBoard { private final Player player; private Pawn[][] board; - private final List maxPawns = new ArrayList<>(); - private final List minPawns = new ArrayList<>(); - public PusherBoard(Player player, String[] boardValues) { this.player = player; @@ -31,12 +25,6 @@ public class PusherBoard { pawn = new Pushed(pawnPlayer, row, col); } - if (pawnPlayer == player) { - maxPawns.add(pawn); - } else { - minPawns.add(pawn); - } - board[row][col] = pawn; } @@ -50,91 +38,66 @@ public class PusherBoard { public String runNextMove() { MiniMax.MiniMaxResult result = MiniMax.miniMax(this); - Pawn pawn = result.getPawn(); - String initialPosition = pawn.getPosition(); - + Pawn pawn = board[result.getRow()][result.getCol()]; System.out.println(result.getScore()); + String initialPosition = pawn.getPosition(); move(pawn, result.getMovement()); + String nextPosition = pawn.getPosition(); - - return initialPosition + "-" + pawn.getPosition(); + return initialPosition + "-" + nextPosition; } public void move(String move) { - //FORMAT ex : D2-D3 String[] split = move.trim().split(" - "); - move(split[0], split[1]); + String from = split[0]; + String to = split[1]; + + int fromCol = (int) from.charAt(0) - 65; + int fromRow = Integer.parseInt(String.valueOf(from.charAt(1))) - 1; + int toCol = (int) to.charAt(0) - 65; + + Pawn.PawnMovement movement = Pawn.PawnMovement.from(toCol - fromCol); + move(fromRow, fromCol, movement); } - private void move(Pawn pawn, Pawn.PawnMovement movement) { - move(pawn.getCol(), pawn.getRow(), pawn.getCol() + movement.getMove(), pawn.getRow() + pawn.getDirection()); - } - - public void move(String from, String to) { - //FORMAT ex : from {D2}, to {D3} - int from_col = (int) from.charAt(0) - 65; - int from_row = Integer.parseInt(String.valueOf(from.charAt(1))) - 1; - int to_col = (int) to.charAt(0) - 65; - int to_row = Integer.parseInt(String.valueOf(to.charAt(1))) - 1; - - move(from_col, from_row, to_col, to_row); - } - - public void move(int from_col, int from_row, int to_col, int to_row) { - if (!isValid(from_col, from_row, to_col)) { + public void move(int row, int col, Pawn.PawnMovement movement) { + Pawn pawn = board[row][col]; + if (pawn == null) { return; } - Pawn pawn = board[from_row][from_col]; - Pawn destPawn = board[to_row][to_col]; - - if (destPawn != null) { - if (destPawn.getPlayer() == player) { - maxPawns.remove(destPawn); - } else { - minPawns.remove(destPawn); - } - } - - board[from_row][from_col] = null; - board[to_row][to_col] = pawn; - - pawn.setRow(to_row); - pawn.setCol(to_col); + move(pawn, movement); } + private void move(Pawn pawn, Pawn.PawnMovement movement) { +// if (!pawn.isMoveValid(board, movement)) { +// return; +// } - private boolean isValid(int from_col, int from_row, int to_col) { - Pawn pawn = getBoard()[from_row][from_col]; - Pawn.PawnMovement move = Pawn.PawnMovement.from(to_col - from_col); + int toRow = pawn.getRow() + pawn.getDirection(); + int toCol = pawn.getCol() + movement.getMove(); - if (pawn == null) { - return false; - } + board[pawn.getRow()][pawn.getCol()] = null; + board[toRow][toCol] = pawn; + pawn.move(movement); + } - return pawn.isMoveValid(this, move); + public Player getPlayer() { + return player; } public Pawn[][] getBoard() { return board; } - public List getMaxPawns() { - return maxPawns; - } - - public List getMinPawns() { - return minPawns; - } - - public void printBoard() { + public static void printBoard(Pawn[][] board) { for (int i = 7; i >= 0; i--) { - for (int j = 0; j < this.board.length; j++) { - if (this.board[i][j] != null) { - System.out.print(this.board[i][j] + " | "); + for (int j = 0; j < board.length; j++) { + if (board[i][j] != null) { + System.out.print(board[i][j] + " | "); } else { - System.out.print(" | "); + System.out.print(" | "); } } System.out.println();