Skip to content

asadbmirza/Multiplayer-Online-Asynchronous-Battleship

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Multiplayer Online Asynchronous Battleship (MOAB)

This project is the server-side implementation for a Multiplayer Online Asynchronous Battleship game. It is written in C and uses epoll for efficient handling of multiple concurrent client connections.


Features

  • 10x10 Game Board: The game is played on a 10x10 grid, with 0-based coordinates.
  • Asynchronous Gameplay: Players can connect and take turns at any time.
  • Dynamic Player Management: The server can handle players joining, registering their ships, bombing cells, and disconnecting at any point.
  • Ship Placement: Each player places a 5-cell ship, either a 5x1 (horizontal) or 1x5 (vertical) piece. Overlapping ships are permitted.
  • Bombing and Damage: Players can bomb any cell, damaging any and all ships that occupy that cell. The server broadcasts the results (hit or miss) to all registered players.
  • Win/Loss Conditions: A player loses when all 5 cells of their ship are damaged. The server then disconnects them and broadcasts their loss to all other players.

Server Usage

To run the server, you will need a C compiler (gcc) and a make utility as well as being in the /src directory.

  1. Compile the code:

    make

    This will create an executable named server.

  2. Run the server:

    ./server <port_number>

    Replace <port_number> with the desired port for the server to listen on. The server will attempt to bind to the specified port and will increment the port number if the address is already in use.


Message Syntax

The server communicates with clients using a specific message format. All messages end with a newline character (\n).

Client to Server Messages

  • Registration: REG <name> <x> <y> <d>\n
    • name: up to 20-byte string for the player's name.
    • x, y: integer coordinates of the ship's center.
    • d: character representing the ship's direction (- for horizontal, | for vertical).
  • Bomb Request: BOMB <x> <y>\n
    • x, y: integer coordinates of the cell to bomb.

Server to Client Messages

  • Registration Accepted: WELCOME\n
  • Invalid Message: INVALID\n (for syntax errors or invalid ship placement)
  • Name Taken: TAKEN\n (if the chosen name is already in use)
  • Join Broadcast: JOIN <name>\n (broadcast to all players when a new player successfully registers)
  • Damage Report: HIT <attacker> <x> <y> <victim>\n (broadcast when a ship is hit)
  • Miss Report: MISS <attacker> <x> <y>\n (broadcast when no ships are hit)
  • Game Over Broadcast: GG <player>\n (broadcast when a player loses or disconnects)

Project Structure

The project is organized into modular C files to improve readability and maintainability:

  • server.c / server.h: Manages the main server loop, network connections, epoll events, and high-level message handling.
  • user.c / user.h: Handles user-related data, including creation, registration, and managing linked lists of users.
  • board.c / board.h: Manages the game board state, including adding/removing users (ships) and handling bomb requests.
  • Makefile: A build script for compiling the source code and cleaning up the project.
  • test.sh: A basic shell script for testing certain disconnection scenarios.

Development and Testing Notes

  • Language and Libraries: The server is implemented in C and uses standard unix libraries for networking (sys/socket.h, netinet/in.h), I/O multiplexing (sys/epoll.h), and signal handling (signal.h).
  • Debugging: All debug and error messages are printed to stderr.
  • epoll: The server uses epoll with edge-triggered mode (EPOLLET) to handle I/O events efficiently for a large number of clients.
  • Signal Handling: The server uses a pipe to handle SIGINT and SIGTERM signals, allowing for a clean shutdown. It also ignores SIGPIPE to prevent crashes if a client disconnects unexpectedly.
  • Client Testing: You can manually test the server using the nc (netcat) utility, which can act as a simple client. For example, nc localhost <port_number>.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages