-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.java
More file actions
157 lines (130 loc) · 5.88 KB
/
ChessBoard.java
File metadata and controls
157 lines (130 loc) · 5.88 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.ArrayList;
public class ChessBoard {
private final int SIZE = 8; // Standard ches
private Position whiteKingPosition;
private Position blackKingPosition;
private ThreatState threatState = new ThreatState();
private ChessPiece[][] board = new ChessPiece[8][8];
public ChessBoard() {
initializeBoard();
}
void initializeBoard() {
//clear board
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
board[row][col] = null; // Set each square to null
}
}
for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn("Pawn", "Black");
board[6][i] = new Pawn("Pawn", "White");
}
// Initialize Rooks
board[0][0] = new Rook("Rook", "Black");
board[0][7] = new Rook("Rook", "Black");
board[7][0] = new Rook("Rook", "White");
board[7][7] = new Rook("Rook", "White");
// Initialize Knights
board[0][1] = new Knight("Knight", "Black");
board[0][6] = new Knight("Knight", "Black");
board[7][1] = new Knight("Knight", "White");
board[7][6] = new Knight("Knight", "White");
// Initialize Bishops
board[0][2] = new Bishop("Bishop", "Black");
board[0][5] = new Bishop("Bishop", "Black");
board[7][2] = new Bishop("Bishop", "White");
board[7][5] = new Bishop("Bishop", "White");
// Initialize Queens
board[0][3] = new Queen("Queen", "Black");
board[7][3] = new Queen("Queen", "White");
// Initialize Kings
board[0][4] = new King("King", "Black");
board[7][4] = new King("King", "White");
blackKingPosition = new Position(0, 4); // Store black king's position
whiteKingPosition = new Position(7, 4); // Store white king's position
}
public void setPiece(int sR, int sC, ChessPiece piece){
if (sR >= 0 && sR < SIZE && sC >= 0 && sC < SIZE) {
board[sR][sC] = piece;
} else {
System.out.println("Position out of bounds");
}
}
public void movePiece(int startRow, int startCol, int endRow, int endCol) {
ChessPiece pieceToMove = board[startRow][startCol]; // Get the piece to move
//Keep track of king positions
if (pieceToMove.getType(pieceToMove).equals("King")){
setKingPosition(pieceToMove.color, endRow,endCol);
}
board[startRow][startCol] = null; // Remove the piece from the starting position
board[endRow][endCol] = pieceToMove; // Place the piece at the target position
}
public ChessPiece getPiece(int row, int col) {
return board[row][col];
}
public ChessPiece[][] getBoard(){
return board;
}
public void setKingPosition(String color, int row, int col) {
System.out.println("setting king position " + color);
if ("White".equals(color)) {
whiteKingPosition = new Position(row, col);
} else if ("Black".equals(color)) {
blackKingPosition = new Position(row, col);
}
System.out.println(whiteKingPosition);
System.out.println(blackKingPosition);
}
public Position getKingPosition(String color) {
return "White".equals(color) ? whiteKingPosition : blackKingPosition;
}
public void updateThreatState(){
}
public String toBoardCoordinate(int row, int col) {
char colLetter = (char) ('A' + col);
int rowNumber = SIZE - row; // Assuming your board rows start from 0 at the bottom
return "" + colLetter + rowNumber;
}
public ThreatState getThreatState() {
return threatState;
}
/*
public ThreatState checkThreatState(ArrayList<Move> legalMovesPiece, ChessBoard chessBoard){
int Kingx = chessBoard.getKingPosition("White").getCol();
int Kingy = chessBoard.getKingPosition("White").getRow();
String kingPos = toBoardCoordinate(Kingy,Kingx);
System.out.println(kingPos);
System.out.println("King X: " + Kingx + " King Y: " + Kingy);
for (Move potentialMove : legalMovesPiece) {
System.out.println("Evaluating move " + potentialMove);
//System.out.println(getThreatState().isUnderThreat(potentialMove.toThreatState(potentialMove)));
if (getThreatState().isUnderThreat(potentialMove.toThreatState(potentialMove))){
if (potentialMove.getNewY() == Kingy && potentialMove.getNewX() == Kingx) {
System.out.println("White King is in Check!");
}
System.out.println("Warning Piece in King Threat Zone!");
getThreatState().setKingMoveable(potentialMove.toThreatState(potentialMove),false);
}
}
return null;
}
*/
public void simulateMove(int startRow, int startCol, int endRow, int endCol) {
System.out.println("Simulating SR: " + startRow + " SC " + startCol);
System.out.println();
ChessPiece movingPiece = this.board[startRow][startCol];
if (movingPiece.getType(movingPiece).equals("King")){
setKingPosition(movingPiece.color, endRow,endCol);
}
this.board[startRow][startCol] = null; // Remove the piece from the starting square
this.board[endRow][endCol] = movingPiece; // Place it on the destination square
}
// Method to undo a simulated move
public void undoSimulatedMove(int startRow, int startCol, int endRow, int endCol, ChessPiece originalPiece, ChessPiece capturedPiece) {
this.board[startRow][startCol] = originalPiece; // Restore the original piece to its starting square
this.board[endRow][endCol] = capturedPiece; // Restore the captured piece, if any, to the destination square
if (originalPiece.getType(originalPiece).equals("King")){
setKingPosition(originalPiece.color, startRow,startCol);
}
}
}