RU
russoangelo/netfour
A Java implementation of the classic Connect Four game with a JavaFX graphical interface and client-server architecture. Built using Java sockets for network communication and JavaFX for the visual interface.
๐ฎ Connect 4 - Multiplayer Game
A Java implementation of the classic Connect Four game featuring a beautiful JavaFX interface and real-time multiplayer gameplay over network.
โจ Features
- ๐ฏ Real-time Multiplayer - Two players compete over a local network
- ๐จ Animated Interface - Smooth disc-dropping animations with JavaFX
- ๐ Turn Management - Automatic turn synchronization between players
- ๐ Win Detection - Automatic detection of wins, draws, and disconnections
- ๐ญ Player Colors - Red (X) vs Yellow (O) discs
- ๐ก TCP Sockets - Reliable client-server communication
๐ Quick Start
Prerequisites
- Java 21 or higher
- Gradle 8.0+
- JavaFX SDK
Installation
-
Clone the repository
git clone https://github.com/russoangelo/connect-4.git cd connect-4 -
Build the project
./gradlew build
Running the Game
Start the Server
./gradlew run --main-class=com.connectfour.connect4.ServerThe server will start on port 12345 and wait for two players.
Start Client 1
Open a new terminal:
./gradlew run --main-class=com.connectfour.connect4.ClientGUIStart Client 2
Open another terminal:
./gradlew run --main-class=com.connectfour.connect4.ClientGUI๐ฎ How to Play
- Start the server - It will wait for two players to connect
- Launch two clients - First player gets Red (X), second gets Yellow (O)
- Take turns - Click on a column number to drop your disc
- Win condition - Connect four discs horizontally, vertically, or diagonally
- Game ends - When someone wins, the board is full, or a player disconnects
๐๏ธ Project Structure
connect-4/
โโโ src/main/java/com/connectfour/connect4/
โ โโโ Server.java # Main server (port 12345)
โ โโโ ClientGUI.java # JavaFX client interface
โ โโโ ClientHandler.java # Handles client connections
โ โโโ Game.java # Game logic and win detection
โโโ build.gradle.kts
โโโ README.md
๐ง Technical Details
Architecture
- Server: Multi-threaded TCP server handling two simultaneous connections
- Client: JavaFX application with socket communication
- Protocol: Custom text-based protocol over TCP
- Game Logic: Synchronized game state shared between threads
Network Protocol
Server โ Client:
PLAYER_ID:X/O- Assigns player symbolBOARD_START- Begins board transmissionBOARD_END- Ends board transmissionYOUR_TURN- Notifies player's turnWAIT_TURN- Wait for opponentWIN/LOSE/DRAW- Game result
Client โ Server:
0-6- Column number for move
Key Classes
Game: Core game logic, board state, win detectionServer: Accepts connections and creates game instancesClientHandler: Manages individual client communicationClientGUI: JavaFX interface with animations
๐จ Customization
Change Server Port
Edit Server.java:
final int PORT = 12345; // Change to preferred portModify Board Size
Edit Game.java:
public static final int ROWS = 6;
public static final int COLS = 7;Animation Speed
Edit ClientGUI.java:
transition.setDuration(Duration.millis(500));๐ Known Issues
- Server must be restarted between games
- No reconnection support if client disconnects
- Requires manual IP configuration for non-localhost play
๐ License
This project is open source and available under the MIT License.
๐ค Author
Angelo Russo
- GitHub: @russoangelo
โญ Show Your Support
Give a โญ๏ธ if you like this project!