forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceValidation.java
More file actions
93 lines (76 loc) · 3.77 KB
/
PieceValidation.java
File metadata and controls
93 lines (76 loc) · 3.77 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
public class PieceValidation {
// Helper method to check if a piece's path is clear (for pieces that move in straight lines)
public static boolean isPathClear(char[][] board, int startRow, int startCol, int endRow, int endCol) {
int rowStep = Integer.compare(endRow, startRow);
int colStep = Integer.compare(endCol, startCol);
int currentRow = startRow + rowStep;
int currentCol = startCol + colStep;
while (currentRow != endRow || currentCol != endCol) {
if (board[currentRow][currentCol] != ' ') {
return false;
}
currentRow += rowStep;
currentCol += colStep;
}
return true;
}
public static boolean validatePawnMove(char[][] board, char piece, int startRow, int startCol, int endRow, int endCol) {
boolean isWhite = Character.isUpperCase(piece);
int direction = isWhite ? -1 : 1; // White pawns move up (-1), black pawns move down (+1)
int startRank = isWhite ? 6 : 1; // Starting rank for pawns
// Normal one-square move
if (startCol == endCol && endRow == startRow + direction && board[endRow][endCol] == ' ') {
return true;
}
// Initial two-square move
if (startRow == startRank && startCol == endCol &&
endRow == startRow + 2 * direction &&
board[endRow][endCol] == ' ' &&
board[startRow + direction][startCol] == ' ') {
return true;
}
// Capture move (diagonal)
if (Math.abs(endCol - startCol) == 1 && endRow == startRow + direction) {
char targetPiece = board[endRow][endCol];
if (targetPiece != ' ' && Character.isUpperCase(targetPiece) != isWhite) {
return true;
}
}
return false;
}
public static boolean validateRookMove(char[][] board, int startRow, int startCol, int endRow, int endCol) {
// Rook moves horizontally or vertically
if (startRow != endRow && startCol != endCol) {
return false;
}
return isPathClear(board, startRow, startCol, endRow, endCol);
}
public static boolean validateKnightMove(char[][] board, int startRow, int startCol, int endRow, int endCol) {
// Knight moves in L-shape: 2 squares in one direction and 1 square perpendicular
int rowDiff = Math.abs(endRow - startRow);
int colDiff = Math.abs(endCol - startCol);
return (rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2);
}
public static boolean validateBishopMove(char[][] board, int startRow, int startCol, int endRow, int endCol) {
// Bishop moves diagonally
if (Math.abs(endRow - startRow) != Math.abs(endCol - startCol)) {
return false;
}
return isPathClear(board, startRow, startCol, endRow, endCol);
}
public static boolean validateQueenMove(char[][] board, int startRow, int startCol, int endRow, int endCol) {
// Queen combines rook and bishop movements
if (startRow == endRow || startCol == endCol) {
return validateRookMove(board, startRow, startCol, endRow, endCol);
} else if (Math.abs(endRow - startRow) == Math.abs(endCol - startCol)) {
return validateBishopMove(board, startRow, startCol, endRow, endCol);
}
return false;
}
public static boolean validateKingMove(char[][] board, int startRow, int startCol, int endRow, int endCol) {
// King moves one square in any direction
int rowDiff = Math.abs(endRow - startRow);
int colDiff = Math.abs(endCol - startCol);
return rowDiff <= 1 && colDiff <= 1;
}
}