Skip to content

Commit d3ff9e0

Browse files
committed
some improvements from Jean-Francois
1 parent b42fd0a commit d3ff9e0

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

CCSPiJ/src/chapter8/Board.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface Board<Move> {
2828
boolean isWin();
2929

3030
default boolean isDraw() {
31-
return (!isWin() && (getLegalMoves().size() == 0));
31+
return !isWin() && getLegalMoves().isEmpty();
3232
}
3333

3434
double evaluate(Piece player);

CCSPiJ/src/chapter8/C4Piece.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public enum C4Piece implements Piece {
2121

2222
@Override
2323
public C4Piece opposite() {
24-
if (this == C4Piece.B) {
24+
switch (this) {
25+
case B:
2526
return C4Piece.R;
26-
} else if (this == C4Piece.R) {
27+
case R:
2728
return C4Piece.B;
28-
} else {
29+
default: // E, empty
2930
return C4Piece.E;
3031
}
3132
}

CCSPiJ/src/chapter8/TTTBoard.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public List<Integer> getLegalMoves() {
6565
@Override
6666
public boolean isWin() {
6767
// three row, three column, and then two diagonal checks
68-
return position[0] == position[1] && position[0] == position[2] && position[0] != TTTPiece.E ||
69-
position[3] == position[4] && position[3] == position[5] && position[3] != TTTPiece.E ||
70-
position[6] == position[7] && position[6] == position[8] && position[6] != TTTPiece.E ||
71-
position[0] == position[3] && position[0] == position[6] && position[0] != TTTPiece.E ||
72-
position[1] == position[4] && position[1] == position[7] && position[1] != TTTPiece.E ||
73-
position[2] == position[5] && position[2] == position[8] && position[2] != TTTPiece.E ||
74-
position[0] == position[4] && position[0] == position[8] && position[0] != TTTPiece.E ||
75-
position[2] == position[4] && position[2] == position[6] && position[2] != TTTPiece.E;
68+
return checkPos(0, 1, 2) || checkPos(3, 4, 5) || checkPos(6, 7, 8)
69+
|| checkPos(0, 3, 6) || checkPos(1, 4, 7) || checkPos(2, 5, 8)
70+
|| checkPos(0, 4, 8) || checkPos(2, 4, 6);
71+
}
72+
73+
private boolean checkPos(int p0, int p1, int p2) {
74+
return position[p0] == position[p1] && position[p0] == position[p2]
75+
&& position[p0] != TTTPiece.E;
7676
}
7777

7878
@Override

CCSPiJ/src/chapter8/TTTPiece.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public enum TTTPiece implements Piece {
2121

2222
@Override
2323
public TTTPiece opposite() {
24-
if (this == TTTPiece.X) {
24+
switch (this) {
25+
case X:
2526
return TTTPiece.O;
26-
} else if (this == TTTPiece.O) {
27+
case O:
2728
return TTTPiece.X;
28-
} else {
29+
default: // E, empty
2930
return TTTPiece.E;
3031
}
3132
}

0 commit comments

Comments
 (0)