-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecialMoves.java
More file actions
101 lines (83 loc) · 3.31 KB
/
SpecialMoves.java
File metadata and controls
101 lines (83 loc) · 3.31 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
import java.util.ArrayList;
import java.util.Random;
public class SpecialMoves {
public static void pawnPromotion(Pawn pawn, char c){
Square intermediate = pawn.getPosition();
PieceSet set = pawn.getColor() == PieceColor.WHITE ?
Board.getWhiteSet() : Board.getBlackSet();
set.capturePiece(pawn);
Piece piece;
switch (c) {
case 'r': piece = new Rook (intermediate, set.getColor());
break;
case 'k': piece = new Knight(intermediate, set.getColor());
break;
case 'b': piece = new Bishop(intermediate, set.getColor());
break;
default: piece = new Queen (intermediate, set.getColor());
}
set.addPiece(piece);
}
/**Moves the rook in order to perform a Kingside Castling*/
public static void smallCastling(){
int row = Game.getTurn() == PieceColor.WHITE ? 1 : 8;
Game.move( new Move( Board.translate(Letter.H, row), Board.translate(Letter.F, row) ));
}
/**Moves the rook in order to perform a Queenside Castling*/
public static void bigCastling() {
int row = Game.getTurn() == PieceColor.WHITE ? 1 : 8;
Game.move( new Move( Board.translate(Letter.A, row), Board.translate(Letter.D, row) ));
}
public static String outOfCheck() {
Piece auxKing = Game.getMySet().getAvailablePieces().get(0);
ArrayList<Square> captureFreeSquares = auxKing.getCaptureFreeSquares();
int opponentColor = auxKing.getColor() == PieceColor.WHITE ? PieceColor.BLACK : PieceColor.WHITE;
Square intermediate;
if(captureFreeSquares.size() != 0) {
// Move the king out of check.
Move move;
Random randGen = new Random();
int m = randGen.nextInt(captureFreeSquares.size());
move = new Move(auxKing.getPosition(), captureFreeSquares.get(m));
Game.moveToWinboard(move);
return move.toString() + "\n";
}
// Try to capture the piece that threatens the king.
Square s = Piece.canBeCaptured(auxKing, auxKing.getPosition(), opponentColor);
for(Piece p : Game.getMySet().getAvailablePieces()) {
if(PieceType.getType(p) == PieceType.KING)
continue;
for(Square sqr : p.getValidSquares())
if(sqr == s) {
Move move = new Move(p.getPosition(), s);
Game.moveToWinboard(move);
return move.toString() + "\n";
}
}
// Try to block the piece that threatens the king.
// A knight cannot be blocked.
if(PieceType.getType(s.getPiece()) == PieceType.KNIGHT)
return "resign\n";
int letterDirection = s.getLetter() - auxKing.getPosition().getLetter();
int numberDirection = s.getNumber() - auxKing.getPosition().getNumber();
letterDirection = letterDirection < 0 ? -1 : (letterDirection > 0 ? 1 : 0);
numberDirection = numberDirection < 0 ? -1 : (numberDirection > 0 ? 1 : 0);
intermediate = Board.translate(auxKing.getPosition().getLetter() + letterDirection,
auxKing.getPosition().getNumber() + numberDirection);
while(intermediate.compareTo(s) != 0) {
for(Piece p : Game.getMySet().getAvailablePieces()) {
if(PieceType.getType(p) == PieceType.KING)
continue;
for(Square sqr : p.getValidSquares())
if(sqr == intermediate) {
Move move = new Move(p.getPosition(), intermediate);
Game.moveToWinboard(move);
return move.toString() + "\n";
}
}
intermediate = Board.translate(intermediate.getLetter() + letterDirection,
intermediate.getNumber() + numberDirection);
}
return "resign\n";
}
}