diff --git a/README.md b/README.md new file mode 100644 index 0000000..c1bedf7 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Chess Game Application + +This project is a Java-based chess game application that provides a graphical user interface (GUI) for playing chess. The application allows users to select their color (black or white) and start a game of chess against an opponent. + +## Project Structure + +- `App.java`: The entry point of the application, which initializes the GUI for selecting the player's color and starting the game. +- `Chess.java`: Manages the chess game, including the initialization of the chessboard, placement of pieces, and the logic for moving pieces. +- `GameState.java`: Manages the state of the chess game, including the positions of pieces on the board. +- `Validator.java`: Validates the moves of chess pieces based on the rules of chess. + +## Documentation + +For detailed documentation on each class, refer to the following files: + +- [App.md](docs/App.md): Documentation for `App.java` +- [Chess.md](docs/Chess.md): Documentation for `Chess.java` +- [GameState.md](docs/GameState.md): Documentation for `GameState.java` +- [Validator.md](docs/Validator.md): Documentation for `Validator.java` + +## Getting Started + +To run the application, execute the `main` method in `App.java`. This will launch the GUI, allowing you to select your color and start the game. + +## Requirements + +- Java Development Kit (JDK) +- Maven (for managing dependencies) + +## How to Play + +1. Run the application. +2. Select your color (black or white) in the GUI. +3. Click "Start" to begin the game. +4. Play chess by clicking on the pieces and then clicking on the space you want them to move (according to the rules of chess). + +Enjoy playing chess! diff --git a/docs/App.md b/docs/App.md new file mode 100644 index 0000000..d9a31b7 --- /dev/null +++ b/docs/App.md @@ -0,0 +1,22 @@ +### Documentation for `App.java` + +#### Class: `App` + +The `App` class serves as the entry point for the chess game application, providing a graphical user interface (GUI) for selecting the player's color and starting the game. + +#### Fields: +- `public static boolean whiteTurn`: A static boolean indicating whether it is white's turn. + +#### Methods: + +- `public static void main(String[] args)`: + - The main method initializes the GUI for the chess game menu, allowing the user to select their color and start the game. + - Parameters: + - `args`: An array of command-line arguments for the application. + +#### Usage Example: +```java +public static void main(String[] args) { + App.main(new String[]{}); +} +``` diff --git a/docs/Chess.md b/docs/Chess.md new file mode 100644 index 0000000..b600478 --- /dev/null +++ b/docs/Chess.md @@ -0,0 +1,50 @@ +### Documentation for `Chess.java` + +#### Class: `Chess` + +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. + +#### Fields: +- `JButton[][] boardSquares`: A 2D array representing the chessboard squares. +- `GameState state`: An instance of the `GameState` class to manage the state of the game. +- `Validator val`: An instance of the `Validator` class to validate moves. +- `JFrame jFrame`: The main frame for the chess game. +- `static int x1, y1`: Coordinates for the first selected square. + +#### Constructor: +- `public Chess()`: Initializes a new instance of the `Chess` class. + +#### Methods: + +- `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. + +#### Usage Example: +```java +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(); + } +} +``` diff --git a/docs/GameState.md b/docs/GameState.md new file mode 100644 index 0000000..97c473a --- /dev/null +++ b/docs/GameState.md @@ -0,0 +1,43 @@ +### Documentation for `GameState.java` + +#### Class: `GameState` + +The `GameState` class manages the state of the chess game, including the positions of pieces on the board. + +#### Fields: +- `public static HashMap coordinatePlane`: A static `HashMap` representing the board, where the key is a coordinate (x * 10 + y) and the value is the piece code. + +#### Constructor: +- `public GameState()`: Initializes a new instance of the `GameState` class. + +#### Methods: + +- `public void putInMap(int xy, int piece)`: + - Adds a piece to the board at the specified coordinate. + - Parameters: + - `xy`: An integer representing the coordinate (x * 10 + y). + - `piece`: An integer code representing the piece. + +- `public void updateMap(Integer xy, Integer piece)`: + - Updates the piece at the specified coordinate. + - Parameters: + - `xy`: An integer representing the coordinate (x * 10 + y). + - `piece`: An integer code representing the piece. + +- `public int checkPiece(int x, int y)`: + - Retrieves the piece code at the specified coordinates. + - Parameters: + - `x`: The x-coordinate on the board. + - `y`: The y-coordinate on the board. + - Returns: + - An integer code representing the piece at the specified coordinates. + +#### Usage Example: +```java +public static void main(String[] args) { + GameState state = new GameState(); + state.putInMap(11, 1); // Place a white pawn at coordinate (1, 1) + int piece = state.checkPiece(1, 1); // Check the piece at coordinate (1, 1) + System.out.println("Piece at (1, 1): " + piece); +} +``` diff --git a/docs/Validator.md b/docs/Validator.md new file mode 100644 index 0000000..cfd723a --- /dev/null +++ b/docs/Validator.md @@ -0,0 +1,31 @@ +### Documentation for `Validator.java` + +#### Class: `Validator` + +The `Validator` class is responsible for validating the moves of chess pieces based on the rules of chess. + +#### Constructor: +- `public Validator()`: Initializes a new instance of the `Validator` class. + +#### Methods: + +- `public boolean validation(int type, int color, int x1, int x2, int y1, int y2)`: + - Validates whether a move is legal based on the type and color of the piece and the coordinates of the move. + - Parameters: + - `type`: An integer representing the type of the piece (1 = Pawn, 2 = Rook, 3 = Knight, 4 = Bishop, 5 = Queen, 6 = King). + - `color`: An integer representing the color of the piece (0 = White, 1 = Black). + - `x1`: The x-coordinate of the starting position. + - `x2`: The x-coordinate of the ending position. + - `y1`: The y-coordinate of the starting position. + - `y2`: The y-coordinate of the ending position. + - Returns: + - A boolean indicating whether the move is valid. + +#### Usage Example: +```java +public static void main(String[] args) { + Validator validator = new Validator(); + boolean isValid = validator.validation(1, 0, 2, 2, 7, 5); // Validate a move for a white pawn + System.out.println("Is the move valid? " + isValid); +} +``` diff --git a/src/main/java/com/redplover/app/App.java b/src/main/java/com/redplover/app/App.java index 5cae093..da2b15d 100644 --- a/src/main/java/com/redplover/app/App.java +++ b/src/main/java/com/redplover/app/App.java @@ -2,8 +2,6 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.io.IOException; class App { @@ -89,9 +87,4 @@ public static void main(String[] args) { jFrame.dispose(); }); } - - public static void close(int x) { - System.out.println(x); - System.exit(0); - } }