From eea1fbe1bf8b8774c2dc3c231501c0d9e06fc4c4 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Mon, 31 Jan 2022 11:40:45 +0100 Subject: [PATCH] Added moveFigure method to ChessBoard with test --- src/main/java/Game/ChessObj/ChessBoard.java | 41 ++++++++++++---- .../java/Game/ChessObj/ChessBoardTest.java | 47 +++++++++++++++++++ 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index f46138c..f5dd704 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -4,6 +4,13 @@ import java.util.ArrayList; public class ChessBoard { + public enum MoveFeedback { + ENEMYBEATEN, + MOVE, + OUTSIDEOFBOARD, + INVALID + } + private ChessFigure[][] board; public ChessBoard() { @@ -88,14 +95,14 @@ public class ChessBoard { return false; ChessFigure figure = board[sourceY][sourceX]; //Destination has same Team - if(board[destY][destX] != null) - if(board[destY][destX].getTeam() == figure.getTeam()) + if (board[destY][destX] != null) + if (board[destY][destX].getTeam() == figure.getTeam()) return false; int deltaX = destX - sourceX; int deltaY = sourceY - destY; //Pawn special case - if(figure.getType() == ChessFigure.Type.PAWN){ - if(board[destY][destX] != null) { + if (figure.getType() == ChessFigure.Type.PAWN) { + if (board[destY][destX] != null) { if (board[destY][destX].getTeam() == figure.getTeam()) return false; if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) @@ -103,13 +110,13 @@ public class ChessBoard { if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) return true; } - if(figure.isRelativeMoveValid(deltaX, deltaY)) + if (figure.isRelativeMoveValid(deltaX, deltaY)) return true; } - if(figure.isRelativeMoveValid(deltaX, deltaY)){ - if(figure.getType() == ChessFigure.Type.KNIGHT) + if (figure.isRelativeMoveValid(deltaX, deltaY)) { + if (figure.getType() == ChessFigure.Type.KNIGHT) return true; - if(validateCleanPath(sourceX, sourceY, destX, destY)) + if (validateCleanPath(sourceX, sourceY, destX, destY)) return true; } @@ -126,7 +133,7 @@ public class ChessBoard { sourceY += stepY; if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) return false; - if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) + if (Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) return true; while (!(sourceX == destX && sourceY == destY)) { if (board[sourceY][sourceX] != null) @@ -142,6 +149,22 @@ public class ChessBoard { return false; } + public MoveFeedback moveFigure(int sourceX, int sourceY, final int destX, final int destY) { + if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) + return MoveFeedback.OUTSIDEOFBOARD; + if (validateMove(sourceX, sourceY, destX, destY)) { + MoveFeedback feedback = MoveFeedback.INVALID; + if (board[destY][destX] == null) + feedback = MoveFeedback.MOVE; + else + feedback = MoveFeedback.ENEMYBEATEN; + board[destY][destX] = board[sourceY][sourceX]; + board[sourceY][sourceX] = null; + return feedback; + } + return MoveFeedback.INVALID; + } + private int getStepWidth(int delta) { if (delta == 0) return 0; diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 489f0df..4793902 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -161,4 +161,51 @@ class ChessBoardTest { assertTrue(chessBoard.validateMove(4, 7, 6, 5)); assertFalse(chessBoard.validateMove(4, 7, 7, 4)); } + + /* + 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 + ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ + 0 │ │ │ │ │ │ │ │ │ 0 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 1 │ │ │ │ T │ │ │ │ K │ 1 │ │ │ │ │ │ │ │ K │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 2 │ │ │ │ │ │ │ │ │ 2 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 3 │ │ │ │ │ │ Z │ │ │ 3 │ │ │ │ │ │ Z │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ --> ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 4 │ │ │ │ │ │ │ │ │ 4 │ │ │ │ │ B │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 5 │ │ │ |Q| │ │ │ │ │ │ 5 │ │ │ T │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 6 │ │ │ │ │ │ │ B │ │ 6 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 7 │ │ │ │ │ │ │ │ │ 7 │ │ │ │ │ │ │ │ │ + └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ + */ + @Test + void moveFigure() { + ChessFigure[][] tempBoard = new ChessFigure[8][8]; + ChessFigure[][] resultBoard = new ChessFigure[8][8]; + + tempBoard[1][3] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE); + tempBoard[1][7] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + tempBoard[6][6] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + tempBoard[3][5] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + tempBoard[5][2] = new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK); + + resultBoard[5][2] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE); + resultBoard[1][7] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + resultBoard[4][4] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + resultBoard[3][5] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + + chessBoard.setChessBoard(tempBoard); + + assertEquals(chessBoard.moveFigure(3, 1, 2, 1), ChessBoard.MoveFeedback.MOVE); + assertEquals(chessBoard.moveFigure(2, 1, 2, 5), ChessBoard.MoveFeedback.ENEMYBEATEN); + assertEquals(chessBoard.moveFigure(6, 6, 4, 4), ChessBoard.MoveFeedback.MOVE); + assertEquals(chessBoard.moveFigure(7, 1, 8, 1), ChessBoard.MoveFeedback.OUTSIDEOFBOARD); + assertEquals(chessBoard.moveFigure(5, 3, 4, 2), ChessBoard.MoveFeedback.INVALID); + + assertArrayEquals(resultBoard, chessBoard.getBoard()); + } } \ No newline at end of file