The Chess
class represents a chess game with a graphical user interface (GUI) using Java Swing. It handles the initialization of the chessboard, the placement of pieces, and the logic for moving pieces.
JButton[][] boardSquares
: A 2D array representing the chessboard squares.GameState state
: An instance of theGameState
class to manage the state of the game.Validator val
: An instance of theValidator
class to validate moves.JFrame jFrame
: The main frame for the chess game.static int x1, y1
: Coordinates for the first selected square.
public Chess()
: Initializes a new instance of theChess
class.
-
public void start(boolean isWhite) throws IOException
:- Initializes the chessboard and places the pieces.
- Sets up the GUI components and displays the main frame.
- Parameters:
isWhite
: A boolean indicating whether the player is playing as white.
-
private void initializeBoard(boolean isWhite)
:- Sets up the initial positions of the pieces on the board.
- Parameters:
isWhite
: A boolean indicating whether the player is playing as white.
-
private void setupPieces(int pawn, int pawnOpp, int rook, int knight, int bishop, int queen, int king, int rookOpp, int knightOpp, int bishopOpp, int queenOpp, int kingOpp)
:- Places the pieces on the board based on the provided piece codes.
- Parameters:
pawn
,pawnOpp
,rook
,knight
,bishop
,queen
,king
,rookOpp
,knightOpp
,bishopOpp
,queenOpp
,kingOpp
: Integer codes representing different pieces.
-
public void check(int x, int y)
:- Handles the logic for selecting and moving pieces on the board.
- Parameters:
x
,y
: Coordinates of the selected square.
public static void main(String[] args) {
Chess game = new Chess();
try {
game.start(true); // Start the game with the player as white
} catch (IOException e) {
e.printStackTrace();
}
}