Browse Source

Added moveFigure method to ChessBoard with test

Nick Stolbov 3 years ago
parent
commit
eea1fbe1bf
  1. 23
      src/main/java/Game/ChessObj/ChessBoard.java
  2. 47
      src/test/java/Game/ChessObj/ChessBoardTest.java

23
src/main/java/Game/ChessObj/ChessBoard.java

@ -4,6 +4,13 @@ import java.util.ArrayList;
public class ChessBoard { public class ChessBoard {
public enum MoveFeedback {
ENEMYBEATEN,
MOVE,
OUTSIDEOFBOARD,
INVALID
}
private ChessFigure[][] board; private ChessFigure[][] board;
public ChessBoard() { public ChessBoard() {
@ -142,6 +149,22 @@ public class ChessBoard {
return false; 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) { private int getStepWidth(int delta) {
if (delta == 0) if (delta == 0)
return 0; return 0;

47
src/test/java/Game/ChessObj/ChessBoardTest.java

@ -161,4 +161,51 @@ class ChessBoardTest {
assertTrue(chessBoard.validateMove(4, 7, 6, 5)); assertTrue(chessBoard.validateMove(4, 7, 6, 5));
assertFalse(chessBoard.validateMove(4, 7, 7, 4)); 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());
}
} }
Loading…
Cancel
Save