GitHunt
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.

Java
JavaFX
License

โœจ 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

  1. Clone the repository

    git clone https://github.com/russoangelo/connect-4.git
    cd connect-4
  2. Build the project

    ./gradlew build

Running the Game

Start the Server

./gradlew run --main-class=com.connectfour.connect4.Server

The 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.ClientGUI

Start Client 2

Open another terminal:

./gradlew run --main-class=com.connectfour.connect4.ClientGUI

๐ŸŽฎ How to Play

  1. Start the server - It will wait for two players to connect
  2. Launch two clients - First player gets Red (X), second gets Yellow (O)
  3. Take turns - Click on a column number to drop your disc
  4. Win condition - Connect four discs horizontally, vertically, or diagonally
  5. 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 symbol
  • BOARD_START - Begins board transmission
  • BOARD_END - Ends board transmission
  • YOUR_TURN - Notifies player's turn
  • WAIT_TURN - Wait for opponent
  • WIN/LOSE/DRAW - Game result

Client โ†’ Server:

  • 0-6 - Column number for move

Key Classes

  • Game: Core game logic, board state, win detection
  • Server: Accepts connections and creates game instances
  • ClientHandler: Manages individual client communication
  • ClientGUI: JavaFX interface with animations

๐ŸŽจ Customization

Change Server Port

Edit Server.java:

final int PORT = 12345; // Change to preferred port

Modify 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

โญ Show Your Support

Give a โญ๏ธ if you like this project!

russoangelo/netfour | GitHunt