Skip to content

Commit cdc01d4

Browse files
committed
Sepcial move casteling
1 parent be8ac1d commit cdc01d4

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

src/chess/ChessMatch.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ private Piece makeMove(Position source, Position target) {
9999
piecesOnTheBoard.remove(capturedPiece);
100100
CapturedPieces.add(capturedPiece);
101101
}
102+
103+
//#specialmove castling kinside rook
104+
if(p instanceof King && target.getColumn() == source.getColumn() + 2) {
105+
Position sourceT = new Position(source.getRow(), source.getColumn() + 3);
106+
Position targetT = new Position(source.getRow(), source.getColumn() + 1);
107+
ChessPiece rook = (ChessPiece)board.removePiece(sourceT);
108+
board.placePiece(rook, targetT);
109+
rook.increaseMoveCount();
110+
}
111+
112+
//#specialmove castling kinside rook
113+
if(p instanceof King && target.getColumn() == source.getColumn() - 2) {
114+
Position sourceT = new Position(source.getRow(), source.getColumn() - 4);
115+
Position targetT = new Position(source.getRow(), source.getColumn() - 1);
116+
ChessPiece rook = (ChessPiece)board.removePiece(sourceT);
117+
board.placePiece(rook, targetT);
118+
rook.increaseMoveCount();
119+
}
120+
102121
return capturedPiece;
103122
}
104123

@@ -111,6 +130,25 @@ private void undoMove(Position source, Position target, Piece capturedPiece) {
111130
CapturedPieces.remove(capturedPiece);
112131
piecesOnTheBoard.add(capturedPiece);
113132
}
133+
134+
//#specialmove castling kinside rook
135+
if(p instanceof King && target.getColumn() == source.getColumn() + 2) {
136+
Position sourceT = new Position(source.getRow(), source.getColumn() + 3);
137+
Position targetT = new Position(source.getRow(), source.getColumn() + 1);
138+
ChessPiece rook = (ChessPiece)board.removePiece(targetT);
139+
board.placePiece(rook, sourceT);
140+
rook.decreaseMoveCount();
141+
}
142+
143+
//#specialmove castling kinside rook
144+
if(p instanceof King && target.getColumn() == source.getColumn() - 2) {
145+
Position sourceT = new Position(source.getRow(), source.getColumn() - 4);
146+
Position targetT = new Position(source.getRow(), source.getColumn() - 1);
147+
ChessPiece rook = (ChessPiece)board.removePiece(targetT);
148+
board.placePiece(rook, sourceT);
149+
rook.decreaseMoveCount();
150+
}
151+
114152
}
115153

116154

@@ -198,7 +236,7 @@ private void initialSetup() {
198236
placeNewPiece('b', 1, new Knight(board, Color.WHITE));
199237
placeNewPiece('c', 1, new Bishop(board, Color.WHITE));
200238
placeNewPiece('d', 1, new Queen(board, Color.WHITE));
201-
placeNewPiece('e', 1, new King(board, Color.WHITE));
239+
placeNewPiece('e', 1, new King(board, Color.WHITE, this));
202240
placeNewPiece('f', 1, new Bishop(board, Color.WHITE));
203241
placeNewPiece('g', 1, new Knight(board, Color.WHITE));
204242
placeNewPiece('h', 1, new Rook(board, Color.WHITE));
@@ -216,7 +254,7 @@ private void initialSetup() {
216254
placeNewPiece('b', 8, new Knight(board, Color.BLACK));
217255
placeNewPiece('c', 8, new Bishop(board, Color.BLACK));
218256
placeNewPiece('d', 8, new Queen(board, Color.BLACK));
219-
placeNewPiece('e', 8, new King(board, Color.BLACK));
257+
placeNewPiece('e', 8, new King(board, Color.BLACK, this));
220258
placeNewPiece('f', 8, new Bishop(board, Color.BLACK));
221259
placeNewPiece('g', 8, new Knight(board, Color.BLACK));
222260
placeNewPiece('h', 8, new Rook(board, Color.BLACK));

src/chess/pieces/King.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
import boardgame.Board;
44
import boardgame.Position;
5+
import chess.ChessMatch;
56
import chess.ChessPiece;
67
import chess.Color;
78

89
public class King extends ChessPiece {
910

10-
public King(Board board, Color color) {
11+
private ChessMatch chessMatch;
12+
13+
public King(Board board, Color color, ChessMatch chessMatch) {
1114
super(board, color);
15+
this.chessMatch = chessMatch;
16+
}
17+
18+
private boolean testRookCastling(Position position) {
19+
ChessPiece p = (ChessPiece) getBoard().piece(position);
20+
return p != null && p instanceof Rook && p.getColor() == getColor() && p.getMoveCount() == 0;
1221
}
1322

1423
@Override
@@ -75,6 +84,29 @@ public boolean[][] possibleMoves() {
7584
mat[p.getRow()][p.getColumn()] = true;
7685
}
7786

87+
// Specialmove castling
88+
if (getMoveCount() == 0 && !chessMatch.getCheck()) {
89+
// #specialmove castling kingside rook
90+
Position postT1 = new Position(position.getRow(), position.getColumn() + 3);
91+
if (testRookCastling(postT1)) {
92+
Position p1 = new Position(position.getRow(), position.getColumn() + 1);
93+
Position p2 = new Position(position.getRow(), position.getColumn() + 2);
94+
if (getBoard().piece(p1) == null && getBoard().piece(p2) == null) {
95+
mat[position.getRow()][position.getColumn() + 2] = true;
96+
}
97+
}
98+
// #specialmove castling kingside rook
99+
Position postT2 = new Position(position.getRow(), position.getColumn() - 4);
100+
if (testRookCastling(postT2)) {
101+
Position p1 = new Position(position.getRow(), position.getColumn() + 1);
102+
Position p2 = new Position(position.getRow(), position.getColumn() + 2);
103+
Position p3 = new Position(position.getRow(), position.getColumn() + 3);
104+
if (getBoard().piece(p1) == null && getBoard().piece(p2) == null && getBoard().piece(p3) == null) {
105+
mat[position.getRow()][position.getColumn() + 2] = true;
106+
}
107+
}
108+
109+
}
78110
return mat;
79111
}
80-
}
112+
}

0 commit comments

Comments
 (0)