Browse Source

Added moveFigure method to ChessBoard with test

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

41
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() {
@ -88,14 +95,14 @@ public class ChessBoard {
return false; return false;
ChessFigure figure = board[sourceY][sourceX]; ChessFigure figure = board[sourceY][sourceX];
//Destination has same Team //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; return false;
int deltaX = destX - sourceX; int deltaX = destX - sourceX;
int deltaY = sourceY - destY; int deltaY = sourceY - destY;
//Pawn special case //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()) if (board[destY][destX].getTeam() == figure.getTeam())
return false; return false;
if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) 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) if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1)
return true; return true;
} }
if(figure.isRelativeMoveValid(deltaX, deltaY))
if (figure.isRelativeMoveValid(deltaX, deltaY))
return true; 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; return true;
if(validateCleanPath(sourceX, sourceY, destX, destY))
if (validateCleanPath(sourceX, sourceY, destX, destY))
return true; return true;
} }
@ -126,7 +133,7 @@ public class ChessBoard {
sourceY += stepY; sourceY += stepY;
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY))
return false; return false;
if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1)
if (Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1)
return true; return true;
while (!(sourceX == destX && sourceY == destY)) { while (!(sourceX == destX && sourceY == destY)) {
if (board[sourceY][sourceX] != null) if (board[sourceY][sourceX] != null)
@ -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