📚 Technical Documentation: View Javadoc Online
A Java-based multiplayer implementation of the classic Battleship game. This project features a robust Client-Server architecture using Java Sockets for real-time communication and Java Swing for a responsive graphical user interface.
- Real-time Multiplayer: Play against another player over a network using TCP Sockets.
- Intuitive UI: Fully functional graphical interface built with Java Swing.
- Automated Placement Logic: Smart validation for ship placement, preventing overlaps or out-of-bounds errors.
- Turn-based Synchronization: Server-side logic manages player turns and game state synchronization.
- Modern Java Features: Utilizes Java Records for clean data modeling.
src/com/alessiocascini/battleship/
├── client/
│ ├── event/ # ActionListeners & UI Handlers
│ ├── model/ # Data Records (Ship, Cell, ShipInfo)
│ └── ui/ # Swing Interfaces (PlacementUI, GameUI)
└── server/
└── Server.java # Game logic & Socket management
- Language: Java (JDK 17+).
- Networking: Java Sockets (TCP) for reliable data transmission.
- Concurrency: Multithreading to handle simultaneous client connections.
- GUI Framework: Java Swing / AWT.
The server is client-agnostic and uses Java Object Serialization over TCP. Any client can connect if it follows this sequence:
- Identification: Send
boolean(isFirstPlayer). Receiveboolean(isYourTurn). - First Turn Sync: (Only for Player 2) Receive
int[](opponent move) andint[][](result). - Game Loop:
- Send
int[](your move coordinates{row, col}). - Receive
int[][](the move result). - Receive
int[](opponent's move) andint[][](opponent's result).
- Send
- Java Development Kit (JDK) 17 or higher.
- Start the Server: Run
Server.java. - Start the Clients: Run
PlacementUI.javatwice (once for each player). - Setup: Place all ships on the grid and click Confirm.
- Play: Once both players are connected, the game starts automatically.
Note on Networking: By default, the client connects to
localhost. To play across different networks, ensure the server's IP is correctly set in the client code and that port5000is open on the host's router.
| Status | Color | Description |
|---|---|---|
| 🌊 Water | Blue | Your shot missed the target. |
| 🔥 Hit | Red | You successfully hit an opponent's ship. |
| 💀 Sunk | Black | The entire ship has been destroyed. |
| 🚢 Ship | Gray | Your own ship's position. |
- UI/UX Overhaul: Replacing Swing components with custom graphics and animations.
- Network Optimization: Exploring UDP for faster state updates.
- Code Refactoring: Moving the
modelrecords into a shared library to be used by both Client and Server. - Media: Adding gameplay videos and screenshots once assets are implemented.
You can import the game models into your own Java project via GitHub Packages.
<dependencies>
<dependency>
<groupId>com.alessiocascini</groupId>
<artifactId>battleship-multiplayer</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>This project is licensed under the MIT License. See the LICENSE file for more details.