|
1 | 1 | # Sokoban Game - Java Implementation |
2 | 2 |
|
3 | | -[](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/io/package-summary.html) [](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/java/awt/package-summary.html) [](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/package-summary.html) [](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/javax/imageio/package-summary.html) [](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/javax/swing/package-summary.html) [](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/package-summary.html) [](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/package-summary.html) |
4 | | - |
5 | | - |
6 | 3 | [](https://opensource.org/licenses/MIT-0)  |
7 | 4 |
|
8 | 5 | ## Table of Contents |
9 | 6 |
|
10 | 7 | - [Introduction](#Introduction) |
11 | | -- [Design Choices and Features](#Design) |
12 | | -- [Validation Points](#Validation) |
13 | 8 | - [Installation](#Installation) |
14 | 9 | - [Build Instructions](#Build-Instructions) |
15 | 10 | - [Game Controls](#Game-Controls) |
|
19 | 14 |
|
20 | 15 | ## Introduction |
21 | 16 |
|
22 | | -Sokoban is a classic puzzle game where the player must push boxes onto designated spots in order to complete the level. The player controls a character that can move up, down, left, and right and push boxes. The game is won when all boxes are on the designated spots. |
23 | | - |
24 | | -## Design Choices and Features |
25 | | - |
26 | | -During the early stages of development, the following design choices and features were considered: |
| 17 | +[Sokoban](https://en.wikipedia.org/wiki/Sokoban) is a classic puzzle game where the player must push boxes onto designated spots in order to complete the level. The player controls a character that can move up, down, left, and right and push boxes. The game is won when all boxes are on the designated spots. |
27 | 18 |
|
28 | | -- The game should have a simple, easy-to-use interface that allows the player to quickly understand how to play the game. |
29 | | -- The game should have multiple levels of increasing difficulty to keep the player engaged. |
30 | | -- The game should keep track of the player's score and time to add an element of competitiveness to the game. |
31 | | -- The game should have sound effects and music to enhance the player's experience. |
32 | | -- The game should have the ability to save and load game progress. |
| 19 | +This project is a fork of a [nice Sokoban implementation](https://github.com/Gimligili/Sokoban.Java), |
| 20 | +enhanced with modern development practices. |
| 21 | +The original implementation has been updated with the following key improvements: |
33 | 22 |
|
34 | | -In order to meet these requirements, the following features were implemented: |
| 23 | +- **Build System**: Migrated to Maven for dependency management and build automation |
| 24 | +- **Testing**: Added unit tests using JUnit 5, AssertJ, and Mockito |
| 25 | +- **Code Quality**: Integrated static analysis tools including: |
| 26 | + - Error Prone for catching common programming mistakes |
| 27 | + - NullAway for compile-time null safety checking |
| 28 | + - Maven Compiler Plugin with strict compilation flags |
| 29 | +- **Modern Java**: Upgraded to use Java 24 features |
| 30 | +- **Dependencies**: Added useful libraries like Guava and StreamEx for improved functionality |
35 | 31 |
|
36 | | -- A graphical user interface (GUI) was designed to display the game board, the player's score and time, and the menu options. |
37 | | -- A set of levels with increasing difficulty was created. |
38 | | -- A scoring system was implemented based on the number of moves the player makes and the time taken to complete the level. |
39 | | -- Sound effects and background music were added to enhance the player's experience. |
40 | | -- The game state can be saved and loaded using the Java Serialization API. |
| 32 | +## Screenshots |
41 | 33 |
|
42 | | -## Validation Points |
| 34 | +### Gameplay |
| 35 | +<div style="text-align: center"> |
| 36 | +<img src="img/Screenshot_Level_1.png" alt="Gameplay Screenshot" style="max-width: 80%;"> |
| 37 | +<div><em>Level 1 - The main game screen showing the player, boxes, and target locations</em></div> |
| 38 | +</div> |
43 | 39 |
|
44 | | -During the testing phase, the following validation points were checked to ensure the game is working correctly: |
45 | | - |
46 | | -- The game interface is displayed correctly on different screen resolutions. |
47 | | -- The player's movements and box movements are correctly registered and displayed on the game board. |
48 | | -- The game levels are of increasing difficulty and can be completed by the player. |
49 | | -- The scoring system correctly calculates the player's score based on the number of moves and time taken to complete the level. |
50 | | -- The sound effects and background music are correctly played. |
51 | | -- The game state is correctly saved and loaded using the Java Serialization API. |
| 40 | +### Level Editor |
| 41 | +<div style="text-align: center"> |
| 42 | +<img src="img/Screenshot_Level_Editor.png" alt="Level Editor Screenshot" style="max-width: 80%;"> |
| 43 | +<div><em>The built-in level editor for creating and modifying game levels</em></div> |
| 44 | +</div> |
52 | 45 |
|
53 | 46 | ## Installation |
54 | 47 |
|
55 | 48 | To play the Sokoban game, you need to have Java installed on your computer. Once you have Java installed, you can download the game files from the repository and compile them using a Java compiler. |
56 | 49 |
|
57 | 50 | ## Build Instructions |
58 | | -If you want to build yourself the .jar file from the release, you need to have Java Development Kit installed on your computer. I used version JDK 20. Then, you can either reuse the .class files that I included each time needed, or you can run the command : |
59 | 51 |
|
60 | | -`javac *.java ihm\*.java logic\*.java` |
| 52 | +### Prerequisites |
| 53 | +- Java Development Kit (JDK) 24 or later |
| 54 | + - Example installation method using [SDKMAN!](https://sdkman.io/): |
| 55 | + ```bash |
| 56 | + # Install SDKMAN! (if not already installed) |
| 57 | + curl -s "https://get.sdkman.io" | bash |
| 58 | + source "$HOME/.sdkman/bin/sdkman-init.sh" |
| 59 | + |
| 60 | + # Install Java 24 |
| 61 | + sdk install java 24-tem |
| 62 | + sdk use java 24-tem |
| 63 | + ``` |
| 64 | +- Apache Maven 3.6.0 or later (can also be installed via SDKMAN! with `sdk install maven`) |
61 | 65 |
|
62 | | -from the ./src folder (referenced from the root folder of the project). Then you need to go to the root of the project and run : |
| 66 | +### Building the Project |
63 | 67 |
|
64 | | -`jar cvfm <MY FILE>.jar MANIFEST.MF src\*.class src\ihm\*.class src\logic\*.class img font levels` |
| 68 | +1. **Clone the repository** (if you haven't already): |
| 69 | + ```bash |
| 70 | + git clone <repository-url> |
| 71 | + cd Sokoban.Java |
| 72 | + ``` |
65 | 73 |
|
66 | | -To execute your .jar file, you can navigate with the console to its location and run : |
| 74 | +2. **Build the project** using Maven: |
| 75 | + ```bash |
| 76 | + mvn clean package |
| 77 | + ``` |
| 78 | + This will: |
| 79 | + - Compile the source code |
| 80 | + - Run the tests |
| 81 | + - Package the application into a JAR file in the `target` directory |
67 | 82 |
|
68 | | -`java -jar <MY FILE>.jar` |
| 83 | +### Running the Game |
69 | 84 |
|
70 | | -or simply find the .jar file with the file exlorer, and run it. You only have to check that the file is run with the Java(TM) Platform SE binary, or else it will not run correctly. |
| 85 | +After building, you can run the game using either of these methods: |
71 | 86 |
|
72 | | -## Game Controls |
| 87 | +1. **Using Maven**: |
| 88 | + ```bash |
| 89 | + mvn exec:java -Dexec.mainClass="main_game" |
| 90 | + ``` |
73 | 91 |
|
74 | | -The Sokoban game is controlled using the arrow keys on the keyboard. The character moves one square at a time in the direction of the arrow key pressed. If there is a box in the way, the character will push the box in the direction of the arrow key. The game is won when all the boxes have been moved to their designated storage locations. |
| 92 | +2. **Using the generated JAR**: |
| 93 | + ```bash |
| 94 | + java -jar target/sokoban-1.0-SNAPSHOT.jar |
| 95 | + ``` |
| 96 | +
|
| 97 | + > Note: The JAR file will be named based on the `artifactId` and `version` defined in `pom.xml` |
75 | 98 |
|
76 | | -## Game Classes |
| 99 | +### Development |
77 | 100 |
|
78 | | -The Sokoban game is implemented using several Java classes. These classes include: |
| 101 | +- **Compile the project**: `mvn compile` |
| 102 | +- **Run tests**: `mvn test` |
| 103 | +- **Create a distributable JAR**: `mvn package` |
| 104 | +- **Clean build**: `mvn clean install` |
79 | 105 |
|
80 | | -- Game: The main class that runs the game. |
81 | | -- Level: Represents a single level of the game, including the maze layout, box and storage locations, and character starting position. |
82 | | -- Player: Represents the player character in the game. |
83 | | -- Box: Represents the boxes that the player pushes around the maze. |
84 | | -- Storage: Represents the storage locations for the boxes. |
| 106 | +### IDE Support |
| 107 | +
|
| 108 | +You can import the project into any Java IDE that supports Maven. The project contains the necessary Maven configuration (`pom.xml`) for easy setup in IDEs like IntelliJ IDEA, Eclipse, or VS Code with the Java extension pack. |
| 109 | +
|
| 110 | +## Game Controls |
| 111 | +
|
| 112 | +The Sokoban game is controlled using the arrow keys on the keyboard. The character moves one square at a time in the direction of the arrow key pressed. If there is a box in the way, the character will push the box in the direction of the arrow key. The game is won when all the boxes have been moved to their designated storage locations. |
| 113 | +
|
| 114 | +## Game Architecture |
| 115 | +
|
| 116 | +The Sokoban game is implemented using a Model-View-Controller (MVC) architecture with the following key components: |
| 117 | +
|
| 118 | +### Core Game Logic (`logic` package) |
| 119 | +- [`Case.java`](src/main/java/logic/Case.java): Represents a single cell in the game grid, containing information about its contents (wall, floor, target) and any movable objects (player, box). |
| 120 | +- [`Controller.java`](src/main/java/logic/Controller.java): Handles game state management, move validation, and game progression. |
| 121 | +- [`Direction.java`](src/main/java/logic/Direction.java): Enumerates possible movement directions (UP, DOWN, LEFT, RIGHT). |
| 122 | +- [`GameAction.java`](src/main/java/logic/GameAction.java): Defines possible game actions and their outcomes. |
| 123 | +- [`TileType.java`](src/main/java/logic/TileType.java): Enumerates different types of tiles (WALL, FLOOR, TARGET, etc.). |
| 124 | +- [`Warehouse.java`](src/main/java/logic/Warehouse.java): Represents the game level, including the grid layout and game state. |
| 125 | +- [`Worker.java`](src/main/java/logic/Worker.java): Represents the player character with movement capabilities. |
| 126 | +
|
| 127 | +### User Interface (`ihm` package) |
| 128 | +- [`Editor.java`](src/main/java/ihm/Editor.java): The main level editor interface for creating and modifying game levels. |
| 129 | +- [`HomeWindow.java`](src/main/java/ihm/HomeWindow.java): The main menu screen with options to play, edit, or exit. |
| 130 | +- [`LevelEditorSetup.java`](src/main/java/ihm/LevelEditorSetup.java): Dialog for setting up new levels in the editor. |
| 131 | +- [`LevelSelection.java`](src/main/java/ihm/LevelSelection.java): Screen for selecting which level to play or edit. |
| 132 | +- [`SokobanPanel.java`](src/main/java/ihm/SokobanPanel.java): The main game panel that renders the current game state. |
| 133 | +- [`SokobanWindow.java`](src/main/java/ihm/SokobanWindow.java): The main application window that contains the game panel. |
| 134 | +- `*Handler.java`: Various event handlers for user interactions. |
| 135 | +
|
| 136 | +### Game Flow |
| 137 | +1. The game starts with `SokobanWindow` showing the main menu. |
| 138 | +2. Players can select a level or enter the level editor. |
| 139 | +3. During gameplay, `Controller` processes moves and updates the `Warehouse` state. |
| 140 | +4. The `SokobanPanel` renders the current game state based on the `Warehouse` model. |
| 141 | +5. The level editor allows creating and modifying levels with a visual interface. |
85 | 142 |
|
86 | 143 | ## Conclusion |
87 | 144 |
|
88 | 145 | The Sokoban game is a challenging and enjoyable puzzle game that requires strategy and planning to solve. With its multiple levels, undo feature, and sound effects, it provides hours of entertainment for players of all ages. Thank you for considering this game for your entertainment needs. |
89 | 146 |
|
90 | | -This README file was created with the help of ChatGPT, an AI language model trained by OpenAI. The content was based on the original report written by myself for a Software Engineering lab. Thank you ChatGPT for your contribution! |
91 | | - |
92 | 147 | ## License |
93 | 148 |
|
94 | 149 | This project is licensed under the terms of the MIT License with Attribution. See [LICENSE](LICENSE) for more information. |
0 commit comments