Skip to content

Commit 7f22289

Browse files
committed
Knight
1 parent 0d2cd6e commit 7f22289

File tree

4 files changed

+106
-30
lines changed

4 files changed

+106
-30
lines changed

src/application/UI.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ public static void printMatch(ChessMatch chessMatch, List<ChessPiece> captured)
5858
printCapturedPiece(captured);
5959
System.out.println();
6060
System.out.println("Turn: " + chessMatch.getTurn());
61-
if(!chessMatch.getCheckMate()) {
62-
System.out.println("Waiting player: " + chessMatch.getCurrentPlayer());
63-
if(chessMatch.getCheck()) {
64-
System.out.println("CHECK!");
61+
if (!chessMatch.getCheckMate()) {
62+
System.out.println("Waiting player: " + chessMatch.getCurrentPlayer());
63+
if (chessMatch.getCheck()) {
64+
System.out.println("CHECK!");
65+
}
6566
}
66-
}
6767
else {
6868
System.out.println("CHECKMATE!");
69-
System.out.println("Winner: " + chessMatch.getCurrentPlayer());
69+
System.out.println("Winner: " + chessMatch.getCurrentPlayer());
70+
7071
}
7172
}
7273

src/chess/ChessMatch.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import boardgame.Position;
1010
import chess.pieces.Bishop;
1111
import chess.pieces.King;
12+
import chess.pieces.Knight;
1213
import chess.pieces.Pawn;
1314
import chess.pieces.Rook;
1415

@@ -81,7 +82,7 @@ public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition t
8182
checkMate = true;
8283
}
8384
else {
84-
nextTurn();
85+
nextTurn();
8586
}
8687

8788
return (ChessPiece)capturedPiece;
@@ -101,12 +102,11 @@ private Piece makeMove(Position source, Position target) {
101102
}
102103

103104
private void undoMove(Position source, Position target, Piece capturedPiece) {
104-
ChessPiece p = (ChessPiece)board.removePiece(target);
105-
p.decreaseMoveCount();
105+
Piece p = board.removePiece(target);
106106
board.placePiece(p, source);
107-
108-
if(CapturedPieces != null) {
109-
board.placePiece(capturedPiece, source);
107+
108+
if (capturedPiece != null) {
109+
board.placePiece(capturedPiece, target);
110110
CapturedPieces.remove(capturedPiece);
111111
piecesOnTheBoard.add(capturedPiece);
112112
}
@@ -139,54 +139,53 @@ private void nextTurn() {
139139
private Color opponent(Color color) {
140140
return (color == Color.WHITE) ? Color.BLACK : Color.WHITE;
141141
}
142-
143-
private ChessPiece King(Color color) {
142+
143+
private ChessPiece king(Color color) {
144144
List<Piece> list = piecesOnTheBoard.stream().filter(x -> ((ChessPiece)x).getColor() == color).collect(Collectors.toList());
145-
for(Piece p : list) {
146-
if(p instanceof King) {
145+
for (Piece p : list) {
146+
if (p instanceof King) {
147147
return (ChessPiece)p;
148148
}
149-
150-
}throw new IllegalStateException("There is no " + color + " king on the board");
151-
149+
}
150+
throw new IllegalStateException("There is no " + color + " king on the board");
152151
}
153-
152+
154153
private boolean testCheck(Color color) {
155-
Position kingPosition = King(color).getChessPosition().toPosition();
154+
Position kingPosition = king(color).getChessPosition().toPosition();
156155
List<Piece> opponentPieces = piecesOnTheBoard.stream().filter(x -> ((ChessPiece)x).getColor() == opponent(color)).collect(Collectors.toList());
157156
for (Piece p : opponentPieces) {
158157
boolean[][] mat = p.possibleMoves();
159-
if(mat[kingPosition.getRow()][kingPosition.getColumn()]) {
158+
if (mat[kingPosition.getRow()][kingPosition.getColumn()]) {
160159
return true;
161160
}
162161
}
163162
return false;
164163
}
165164

166165
private boolean testCheckMate(Color color) {
167-
if(!testCheck(color)) {
166+
if (!testCheck(color)) {
168167
return false;
169168
}
170169
List<Piece> list = piecesOnTheBoard.stream().filter(x -> ((ChessPiece)x).getColor() == color).collect(Collectors.toList());
171-
for(Piece p : list) {
170+
for (Piece p : list) {
172171
boolean[][] mat = p.possibleMoves();
173-
for(int i=0; i<board.getRows(); i++) {
174-
for(int j=0; j<board.getColumns(); j++) {
175-
if(mat[i][j]) {
172+
for (int i=0; i<board.getRows(); i++) {
173+
for (int j=0; j<board.getColumns(); j++) {
174+
if (mat[i][j]) {
176175
Position source = ((ChessPiece)p).getChessPosition().toPosition();
177176
Position target = new Position(i, j);
178177
Piece capturedPiece = makeMove(source, target);
179178
boolean testCheck = testCheck(color);
180179
undoMove(source, target, capturedPiece);
181-
if(!testCheck) {
180+
if (!testCheck) {
182181
return false;
183182
}
184183
}
185184
}
186185
}
187186
}
188187
return true;
189-
}
188+
}
190189

191190
private void placeNewPiece(char column, int row, ChessPiece piece) {
192191
board.placePiece(piece, new ChessPosition(column, row).toPosition());
@@ -195,9 +194,11 @@ private void placeNewPiece(char column, int row, ChessPiece piece) {
195194

196195
private void initialSetup() {
197196
placeNewPiece('a', 1, new Rook(board, Color.WHITE));
197+
placeNewPiece('b', 1, new Knight(board, Color.WHITE));
198198
placeNewPiece('c', 1, new Bishop(board, Color.WHITE));
199199
placeNewPiece('e', 1, new King(board, Color.WHITE));
200200
placeNewPiece('f', 1, new Bishop(board, Color.WHITE));
201+
placeNewPiece('g', 1, new Knight(board, Color.WHITE));
201202
placeNewPiece('h', 1, new Rook(board, Color.WHITE));
202203
placeNewPiece('a', 2, new Pawn(board, Color.WHITE));
203204
placeNewPiece('b', 2, new Pawn(board, Color.WHITE));
@@ -210,9 +211,11 @@ private void initialSetup() {
210211

211212

212213
placeNewPiece('a', 8, new Rook(board, Color.BLACK));
214+
placeNewPiece('b', 8, new Knight(board, Color.BLACK));
213215
placeNewPiece('c', 8, new Bishop(board, Color.BLACK));
214216
placeNewPiece('e', 8, new King(board, Color.BLACK));
215217
placeNewPiece('f', 8, new Bishop(board, Color.BLACK));
218+
placeNewPiece('g', 8, new Knight(board, Color.BLACK));
216219
placeNewPiece('h', 8, new Rook(board, Color.BLACK));
217220
placeNewPiece('a', 7, new Pawn(board, Color.BLACK));
218221
placeNewPiece('b', 7, new Pawn(board, Color.BLACK));

src/chess/ChessPosition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected Position toPosition() {
2929
}
3030

3131
protected static ChessPosition fromPosition(Position position) {
32-
return new ChessPosition((char)('a' + position.getColumn()),8 - position.getRow());
32+
return new ChessPosition((char)('a' + position.getColumn()), 8 - position.getRow());
3333
}
3434

3535
@Override

src/chess/pieces/Knight.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package chess.pieces;
2+
3+
import boardgame.Board;
4+
import boardgame.Position;
5+
import chess.ChessPiece;
6+
import chess.Color;
7+
8+
public class Knight extends ChessPiece {
9+
10+
public Knight(Board board, Color color) {
11+
super(board, color);
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return "N";
17+
}
18+
19+
private boolean canMove(Position position) {
20+
ChessPiece p = (ChessPiece)getBoard().piece(position);
21+
return p == null || p.getColor() != getColor();
22+
}
23+
24+
@Override
25+
public boolean[][] possibleMoves() {
26+
boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()];
27+
28+
Position p = new Position(0, 0);
29+
30+
p.setValue(position.getRow() - 1, position.getColumn() - 2);
31+
if (getBoard().positionExists(p) && canMove(p)) {
32+
mat[p.getRow()][p.getColumn()] = true;
33+
}
34+
35+
p.setValue(position.getRow() - 2, position.getColumn() - 1);
36+
if (getBoard().positionExists(p) && canMove(p)) {
37+
mat[p.getRow()][p.getColumn()] = true;
38+
}
39+
40+
p.setValue(position.getRow() - 2, position.getColumn() + 1);
41+
if (getBoard().positionExists(p) && canMove(p)) {
42+
mat[p.getRow()][p.getColumn()] = true;
43+
}
44+
45+
p.setValue(position.getRow() - 1, position.getColumn() + 2);
46+
if (getBoard().positionExists(p) && canMove(p)) {
47+
mat[p.getRow()][p.getColumn()] = true;
48+
}
49+
50+
p.setValue(position.getRow() + 1, position.getColumn() + 2);
51+
if (getBoard().positionExists(p) && canMove(p)) {
52+
mat[p.getRow()][p.getColumn()] = true;
53+
}
54+
55+
p.setValue(position.getRow() + 2, position.getColumn() + 1);
56+
if (getBoard().positionExists(p) && canMove(p)) {
57+
mat[p.getRow()][p.getColumn()] = true;
58+
}
59+
60+
p.setValue(position.getRow() + 2, position.getColumn() - 1);
61+
if (getBoard().positionExists(p) && canMove(p)) {
62+
mat[p.getRow()][p.getColumn()] = true;
63+
}
64+
65+
p.setValue(position.getRow() + 1, position.getColumn() - 2);
66+
if (getBoard().positionExists(p) && canMove(p)) {
67+
mat[p.getRow()][p.getColumn()] = true;
68+
}
69+
70+
return mat;
71+
}
72+
}

0 commit comments

Comments
 (0)