-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoardTest.java
More file actions
113 lines (87 loc) · 4.21 KB
/
ChessBoardTest.java
File metadata and controls
113 lines (87 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.domain;
import com.backend.domain.Chessboard;
import com.backend.models.ChessPiece;
import com.backend.models.ChessPieceType;
import com.backend.models.Color;
import com.backend.models.Position;
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ChessBoardTest {
@Test
public void TestTwoMoves() {
Chessboard chessboard = new Chessboard();
Position source = new Position(1, 0);
Position target = new Position(2, 0);
// 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);
Assertions.assertEquals(ChessPieceType.Empty, resultA.type());
Assertions.assertEquals(ChessPieceType.Pawn, resultB.type());
Assertions.assertEquals(ChessPieceType.Empty, result.type());
// Black
source = new Position(6, 0);
target = new Position(5, 0);
result = chessboard.movePiece(source, target, Color.Black);
resultA = chessboard.getBoardPosition(source.row, source.col);
resultB = chessboard.getBoardPosition(target.row, target.col);
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();
Position source = new Position(0, 0);
Position target = new Position(1, 0);
// 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);
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();
Position source = new Position(2, 0);
Position target = new Position(1, 0);
// 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);
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();
Position source = new Position(2, 0);
Position target = new Position(3, 0);
// 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);
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
// add pieces movement rules
// display board on the ui
}