Skip to content

kiranashraf/GamePlay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GamePlay Project

This Java project demonstrates a simple communication system between two players using message channels. It implements the SOLID principles and provides two execution modes: same-process and separate-process communication.

Overview

The project simulates a game where two players exchange messages. One player (initiator) sends an initial message, and each player responds by appending their name and a counter to the received message. The exchange continues until the initiator has sent and received 10 messages each.

Key Features

  • Message Channel Abstraction: Uses an interface for different communication implementations (in-memory queues for same-process, TCP sockets for separate processes).
  • SOLID Principles: Designed with Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion in mind.
  • Two Execution Modes:
    • Same-process: Players communicate via shared blocking queues.
    • Separate-process: Players run in different JVMs and communicate over TCP sockets.

Project Structure

  • src/main/java/com/gameplay/: Main source code
    • Main.java: Entry point for same-process execution.
    • InitiatorMain.java: Entry point for initiator in separate-process mode.
    • ResponderMain.java: Entry point for responder in separate-process mode.
    • communication/: Communication abstractions
      • MessageChannel.java: Interface for sending/receiving messages.
      • impl/: Implementations
        • QueueMessageChannel.java: In-memory queue implementation.
        • SocketMessageChannel.java: TCP socket implementation.
    • controller/GameController.java: Orchestrates the game loop in same-process mode.
    • domain/Player.java: Represents a player with messaging capabilities.
    • model/Message.java: Simple message data holder.
  • target/classes/: Compiled classes.

Requirements

  • Java 17 or higher (for records and modern features).
  • No external dependencies.

Quick Start

For a quick start guide, see QUICKSTART.md

Building the Project

Using javac (Recommended)

cd /Users/kiran/JavaWorkspace/GamePlay
find src/main/java -name "*.java" | xargs javac -d target/classes

Using Maven (if available)

mvn compile

Running the Project

You can run the project using shell scripts (recommended) or manual Java commands.

Using Shell Scripts (Easiest)

Shell scripts are located in ./shellScripts/ directory

Same-Process Mode:

./shellScripts/start.sh same-process

Separate-Process Mode:

./shellScripts/start.sh separate-process

For more script options and detailed usage, see SCRIPTS.md

Manual Execution

Same-Process Mode

In this mode, both players run in the same JVM using shared queues.

java -cp target/classes com.gameplay.Main

This will start the game, perform 10 exchanges, and print the message flow to the console.

Separate-Process Mode

In this mode, each player runs in a separate JVM, communicating over TCP sockets. The ResponderMain must be started first to listen for incoming connections.

Terminal 1 - Start the Responder:

cd /Users/kiran/JavaWorkspace/GamePlay
java -cp target/classes com.gameplay.ResponderMain

Terminal 2 - Start the Initiator (after responder is listening):

cd /Users/kiran/JavaWorkspace/GamePlay
java -cp target/classes com.gameplay.InitiatorMain

Or run responder in background:

java -cp target/classes com.gameplay.ResponderMain &
sleep 2  # Wait for responder to start listening
java -cp target/classes com.gameplay.InitiatorMain

The initiator sends an initial "start" message, and both players exchange messages until the initiator has sent and received 9 responses (10 total interactions). The game ends with a "STOP" message sent by the initiator.

Example Output

Same-Process Mode

Player2 received: start
Player1 received: start -> Player2(1)
Player2 received: start -> Player2(1) -> Player1(1)
Player1 received: start -> Player2(1) -> Player1(1) -> Player2(2)
Player2 received: start -> Player2(1) -> Player1(1) -> Player2(2) -> Player1(2)
...
Game finished.

Separate-Process Mode

ResponderMain Output:

Player2 received: start
Player2 received: start -> Player2(1) -> Player1(1)
Player2 received: start -> Player2(1) -> Player1(1) -> Player2(2) -> Player1(2)
Player2 received: start -> Player2(1) -> Player1(1) -> Player2(2) -> Player1(2) -> Player2(3) -> Player1(3)
...
Player2 received: start -> Player2(1) -> Player1(1) -> ... -> Player1(9)
Responder finished.

InitiatorMain Output:

Player1 received: start -> Player2(1)
Player1 received: start -> Player2(1) -> Player1(1) -> Player2(2)
Player1 received: start -> Player2(1) -> Player1(1) -> Player2(2) -> Player1(2) -> Player2(3)
...
Player1 received: start -> Player2(1) -> Player1(1) -> ... -> Player2(9)
Initiator finished.

Notes

  • Ports: The separate-process mode uses hardcoded ports (1234 and 1235). Ensure these are available.
  • Connection Handling: The SocketMessageChannel automatically retries failed connections for up to 30 seconds, making it robust to startup order variations.
  • Blocking I/O: The project uses blocking I/O for simplicity, so it will wait indefinitely for messages.
  • Message Serialization: Messages are serialized using Java's ObjectInputStream/ObjectOutputStream for network transmission.
  • Production Use: For production use, consider adding:
    • Timeout mechanisms
    • Comprehensive error handling and logging
    • Configurable ports
    • Graceful shutdown handlers

About

This Java project demonstrates a simple communication system between two players using message channels. It implements the SOLID principles and provides two execution modes: same-process and separate-process communication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors