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.
- 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
- 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
- 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
ExecutorServiceandScheduledExecutorService - File I/O for persistent game state
- CSS Styling for modern UI
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- Java 17 or higher
- JavaFX SDK configured
- In IntelliJ, run the
Server.javafile
- In IntelliJ, run the
Application.javafile 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
- Enter a unique player name
- Click plots to select them
- Use Plant/Harvest/Steal buttons
- Visit friends by entering their name
plant <row> <col>harvest <row> <col>steal <targetPlayer> <row> <col>visit <friendName>backresync
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>
- 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
- Each player's
Gameobject is synchronized during critical operations - Stealing uses victim-first locking to prevent deadlocks
- Atomic yield deduction ensures no over-stealing
- All farm operations are thread-safe with synchronized blocks
- Real-time state synchronization via
view_updatebroadcasts - Persistent storage with automatic crash recovery
- Graceful disconnect detection
- Automatic reconnection with state resynchronization
- UI remains responsive during network outages
- Farm states persisted to disk every operation
- Growth timers resume after server restart
- Clients automatically reconnect and resync
- Server detects disconnections and releases resources
- Farm state preserved for returning players
- Visitors automatically returned to their farms
- StealBot for Concurrency Testing
- 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
- 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
- Check server logs for thread activity and lock acquisition
- Monitor client console for network errors
- Verify farm files in
farms/directory for persistence
- Concurrency Stress Test Procedure: For the concurrency stress test demo, please refer to concurrency_test_procedure.md.
- Failure Recovery Demonstration: For the failure recovery demonstration, please refer to failure_recovery_demo.md.
If you have any questions or need further assistance, feel free to contact me [email protected] :)