Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions backend/src/main/java/com/backend/domain/Chessboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,17 @@ private void addKingTour(Position from, int[] orientation, List<Position> list,
}

private void evaluateCompleteTour(Position from, int[] orientation, List<Position> list, Color move) {
int[] start = new int[]{from.row, from.col};
int row = from.row + orientation[0];
int col = from.col + orientation[1];

while (isValidPosition(start) && board[start[0]][start[1]].type() == ChessPieceType.Empty) {
start[0] += orientation[0];
start[1] += orientation[1];
list.add(new Position(start[0], start[1]));
while (isValidPosition(row, col) && board[row][col].type() == ChessPieceType.Empty) {
list.add(new Position(row, col));
row += orientation[0];
col += orientation[1];
}

if (isValidPosition(start) && board[start[0]][start[1]].color() != move) {
list.add(new Position(start[0], start[1]));
if (isValidPosition(row, col) && board[row][col].color() != move) {
list.add(new Position(row, col));
}
}

Expand Down
66 changes: 33 additions & 33 deletions backend/src/test/java/com/domain/ChessBoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.backend.models.ChessPieceType;
import com.backend.models.Color;
import com.backend.models.Position;
import org.testng.Assert;
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ChessBoardTest {
Expand All @@ -14,21 +15,17 @@ public class ChessBoardTest {
public void TestTwoMoves() {
Chessboard chessboard = new Chessboard();

System.out.println(chessboard.getBoardPosition(1 , 0));

Position source = new Position(1, 0);
Position target = new Position(2, 0);

chessboard.printBoard();

// White
ChessPiece result = chessboard.movePiece(source, target, Color.White);
ChessPiece resultA = chessboard.getBoardPosition(source.row, source.col);
ChessPiece resultB = chessboard.getBoardPosition(target.row, target.col);

Assert.assertEquals(resultA.type(), ChessPieceType.Empty);
Assert.assertEquals(resultB.type(), ChessPieceType.Pawn);
Assert.assertEquals(result.type(), ChessPieceType.Empty);
Assertions.assertEquals(ChessPieceType.Empty, resultA.type());
Assertions.assertEquals(ChessPieceType.Pawn, resultB.type());
Assertions.assertEquals(ChessPieceType.Empty, result.type());

// Black
source = new Position(6, 0);
Expand All @@ -37,73 +34,76 @@ public void TestTwoMoves() {
resultA = chessboard.getBoardPosition(source.row, source.col);
resultB = chessboard.getBoardPosition(target.row, target.col);

Assert.assertEquals(resultA.type(), ChessPieceType.Empty);
Assert.assertEquals(resultB.type(), ChessPieceType.Pawn);
Assert.assertEquals(result.type(), ChessPieceType.Empty);
chessboard.printBoard();
Assertions.assertEquals(ChessPieceType.Empty, resultA.type());
Assertions.assertEquals(ChessPieceType.Pawn, resultB.type());
Assertions.assertEquals(ChessPieceType.Empty, result.type());
}

@Test
public void TestOccupiedSameColor() {
Chessboard chessboard = new Chessboard();

System.out.println(chessboard.getBoardPosition(1 , 0));

Position source = new Position(0, 0);
Position target = new Position(1, 0);

chessboard.printBoard();

// White
ChessPiece result = chessboard.movePiece(source, target, Color.White);
ChessPiece resultA = chessboard.getBoardPosition(source.row, source.col);
ChessPiece resultB = chessboard.getBoardPosition(target.row, target.col);

Assert.assertEquals(resultA.type(), ChessPieceType.Rock);
Assert.assertEquals(resultB.type(), ChessPieceType.Pawn);
Assert.assertEquals(result.type(), ChessPieceType.Empty);
Assertions.assertEquals(ChessPieceType.Rock, resultA.type());
Assertions.assertEquals(ChessPieceType.Pawn, resultB.type());
Assertions.assertEquals(ChessPieceType.Invalid, result.type());
}

@Test
public void TestEmptySource() {
Chessboard chessboard = new Chessboard();

System.out.println(chessboard.getBoardPosition(1 , 0));

Position source = new Position(2, 0);
Position target = new Position(1, 0);

chessboard.printBoard();

// White
ChessPiece result = chessboard.movePiece(source, target, Color.White);
ChessPiece resultA = chessboard.getBoardPosition(source.row, source.col);
ChessPiece resultB = chessboard.getBoardPosition(target.row, target.col);

Assert.assertEquals(resultA.type(), ChessPieceType.Empty);
Assert.assertEquals(resultB.type(), ChessPieceType.Pawn);
Assert.assertEquals(result.type(), ChessPieceType.Empty);
Assertions.assertEquals(ChessPieceType.Empty, resultA.type());
Assertions.assertEquals(ChessPieceType.Pawn, resultB.type());
Assertions.assertEquals(ChessPieceType.Invalid, result.type());
}

@Test
public void TestEmptySourceEmptyTarget() {
Chessboard chessboard = new Chessboard();

System.out.println(chessboard.getBoardPosition(1 , 0));

Position source = new Position(2, 0);
Position target = new Position(3, 0);

chessboard.printBoard();

// White
ChessPiece result = chessboard.movePiece(source, target, Color.White);
ChessPiece resultA = chessboard.getBoardPosition(source.row, source.col);
ChessPiece resultB = chessboard.getBoardPosition(target.row, target.col);

Assert.assertEquals(resultA.type(), ChessPieceType.Empty);
Assert.assertEquals(resultB.type(), ChessPieceType.Empty);
Assert.assertEquals(result.type(), ChessPieceType.Empty);
Assertions.assertEquals(ChessPieceType.Empty, resultA.type());
Assertions.assertEquals(ChessPieceType.Empty, resultB.type());
Assertions.assertEquals(ChessPieceType.Invalid, result.type());
}

@Test
public void TestRookValidMovesAfterClearingPath() {
Chessboard chessboard = new Chessboard();

// Move pawn from column a to clear rook's vertical path
chessboard.movePiece(new Position(1, 0), new Position(3, 0), Color.White);

Position[] validMoves = chessboard.getValidMoves(new Position(0, 0));

Assertions.assertEquals(2, validMoves.length);
Assertions.assertTrue(Arrays.stream(validMoves)
.anyMatch(p -> p.row == 1 && p.col == 0));
Assertions.assertTrue(Arrays.stream(validMoves)
.anyMatch(p -> p.row == 2 && p.col == 0));
}

// TODO: check other methods, set white/black pieces
Expand Down