Skip to content

Jouwy/QQ-Farm

Repository files navigation

QQ Farm - Multiplayer Farming Game

Project Overview

QQ Farm is a simplified multiplayer farming game implemented using Java Socket Programming, Multithreading, and JavaFX. Each player owns a 4×4 farm grid where they can plant crops, harvest them, and steal from other players' farms. The project focuses on concurrent programming, thread safety, and synchronization among multiple clients connected to a common server.

Features

Core Gameplay

  • Planting: Spend 5 coins to plant crops that mature in 10 seconds
  • Harvesting: Collect 12 coins when crops are ripe
  • Stealing: Steal up to 25% of yield from other players' ripe crops (only when they're away)
  • Farm Visiting: Visit friends' farms to view their progress and potentially steal

Technical Features

  • Multi-threaded Server: Handles multiple concurrent clients
  • Real-time Synchronization: All clients see updates instantly
  • Crash Recovery: Server persists farm states and recovers after crashes
  • Disk Persistence: Farm states are saved to disk, enabling recovery across server restarts, going beyond basic in-memory management.
  • Thread-safe Operations: Atomic plant/harvest/steal operations prevent race conditions
  • Graceful Error Handling: Network failures handled without UI crashes

Environment and Technology Stack

  • java JDK: openjdk-22 (Oracle OpenJDK 22.0.2)
  • javafx-fxml: 22.0.1
  • javafx-controls: 22.0.1
  • maven: 3.8.5
  • Socket Programming for client-server communication
  • Multithreading with ExecutorService and ScheduledExecutorService
  • File I/O for persistent game state
  • CSS Styling for modern UI

Project Structure

src/main/java/org/example/demo/
├── Application.java # JavaFX application entry point
├── Controller.java # GUI controller and network handler
├── Game.java # Farm state management
├── Server.java # Multi-threaded game server
├── StealBot.java # Concurrency testing bot
src/main/resources/org/example/demo/
├── board.fxml # GUI layout definition
└── styles.css # UI styling

How to Run

Prerequisites

  • Java 17 or higher
  • JavaFX SDK configured

Step 1: Start the Server

  • In IntelliJ, run the Server.java file

Step 2: Launch Clients

  • In IntelliJ, run the Application.java file for each client
  • Make sure to allow multiple instances:
    • Go to Run > Modify Run Configurations...
    • Select the configuration for Application.java
    • Click Modify options > Allow multiple instances

Step 3: Play!

  • Enter a unique player name
  • Click plots to select them
  • Use Plant/Harvest/Steal buttons
  • Visit friends by entering their name

Client-Server Protocol

  • plant <row> <col>
  • harvest <row> <col>
  • steal <targetPlayer> <row> <col>
  • visit <friendName>
  • back
  • resync

Server Responses

  • planted <row> <col> <newCoins>
  • harvested <row> <col> <amountEarned> <newCoins>
  • stole <amount> <newCoins>
  • stolen <row> <col> <amount>
  • mature <row> <col>
  • visit <friendName> <coins> <farmState>
  • view_update <owner> <row> <col> <state> <yield>

Concurrency & Synchronization

Threading Model

  • Client Handler Threads: Fixed thread pool (10 threads) for network I/O
  • Growth Scheduler: Separate thread pool for crop maturation timers
  • JavaFX Thread: Dedicated UI thread with Platform.runLater() for updates

Synchronization Strategy

  • Each player's Game object is synchronized during critical operations
  • Stealing uses victim-first locking to prevent deadlocks
  • Atomic yield deduction ensures no over-stealing

Data Consistency

  • All farm operations are thread-safe with synchronized blocks
  • Real-time state synchronization via view_update broadcasts
  • Persistent storage with automatic crash recovery

Error Handling & Recovery

Network Failures

  • Graceful disconnect detection
  • Automatic reconnection with state resynchronization
  • UI remains responsive during network outages

Server Crashes

  • Farm states persisted to disk every operation
  • Growth timers resume after server restart
  • Clients automatically reconnect and resync

Client Crashes

  • Server detects disconnections and releases resources
  • Farm state preserved for returning players
  • Visitors automatically returned to their farms

Testing Tools

  • StealBot for Concurrency Testing

Extension Possibilities

  • Multiple crop types with different costs/growth times
  • Friend lists and gift systems
  • Crop market with dynamic pricing
  • Mobile client using the same protocol
  • Database backend for scalability
  • Anti-theft items and protection systems

Troubleshooting

Common Issues

  • JavaFX not found: Ensure JavaFX SDK path is correct
  • Port in use: Kill existing Java processes or change port
  • Connection refused: Verify server is running
  • Name already taken: Choose unique player names

Log Analysis

  • Check server logs for thread activity and lock acquisition
  • Monitor client console for network errors
  • Verify farm files in farms/ directory for persistence

For Detailed Procedures:


Contact Information

If you have any questions or need further assistance, feel free to contact me [email protected] :)

About

Multiplayer farming game with Java Socket Programming & JavaFX. Plant crops, harvest coins, and steal from friends' farms in real-time. Features thread-safe server with crash recovery and disk persistence.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors