Coder Social home page Coder Social logo

namawho / battleship-java Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 15 KB

Program that manages the logic for a multi-player battleship game in Java. This project only have a basic client that works using CLI, see README to get the GUI.

Java 100.00%

battleship-java's Introduction

Java Battleship

This mainly is the server of the application, for the client's GUI go to:

This project contains a very basic CLI client (Client.java)

How to run Server.java

Inside the 'server' folder, run the following command:

> javac *java

Then:

> cd ..
> java server.Server

(to test) Create your jar file using the following command:

jar -cvfe server.jar *

When launching the Server using CLI, some arguments can be added. Possible args are:

  • -p [portNumber] specifies the port where the server has to listen from
  • -l [gridLength] specifies the grid length
  • -p [portNumber] -l [gridLength] specifies both options
  • -l [gridLength] -p [portNumber]

How to run Client.java

###TO BE ADDED

When launching the Client using CLI, some arguments can be added. Possible args are:

  • [server address] (format: "127.0.0.1")
  • [portNumber] (from 1 to 65535)
  • [server address] [portNumber]


Communication specifications

Format

The protocol is based on a simple request-response form. The message format is as follows:

REQUEST

CMD <space> MSG

RESPONSE

{
 "cmd" : "...",
 "msg" : "..."
}

the msg key can contain an object, for example on the HIT response:

{
 "cmd" : "HIT",
 "msg" : 
   {
    "row" : "...",
    "col" : "..."
   }
}

where <space> is a simple space, CMD is the command, and MSG is the request/response body (the msg key is optional)


Requests

Create ship:

SET rrcclo
  • rr is the row coordinate of the ship (2 char length)
  • cc is the column coordinate (2 char length)
  • l is the lenght of the ship (1 char length)
  • o is the orientation of the ship (1 char, H Horizontal, V Vertical)

Fire ship:

FIRE rrcc
  • rr row coordinate (2 char length)
  • cc column coordinate (2 char length)

Delete ship:

DELETE rrcc
  • rr row coordinate (2 char length)
  • cc column coordinate (2 char length)

Delete all ships:

RESET

[response is GRID [...]]

Asks for grid infos:

GRID

This interrogates the server, which will respond with the corresponding infos (more in the responses)

Tells the server that the client has all the ships set and is ready to play:

READY

[ possible response are WAIT or PLAY or ERROR]


Responses

the documentation is structured like:

  • CMD
    • MSG

Entry point (player are setting ships)

  • WAIT wait for opponent (to finish positioning ships)
  • GRID response for the a GRID or RESET requests.
    • { length: l, ships: [...] }
      • length: size of the grid
      • ships: is the ships array (of integers) of the ships left to position.

Server.Game (both players are in the game)

  • PLAY game can begin (needs to be sent to all players)
  • HIT selected coordinates had a ship
    • { row: r, col: c }
  • MISS selected coordinates were empty
    • { row: r, col: c }
  • SUNK remote ship was fully hit, and then sunk. (the message contains the coordinates for the head of the ship + length and orientation)
    • { row: r, col: c, length: l, orientation: "..." }
  • TURN
    • msg: false opponent's turn
    • msg: true your turn

Universal (this are always valid)

  • LOST you lost the game
  • WON you won the game
  • ERROR
    • { cod: "...", msg: "..." }
      • cod: error status code
      • msg: error message
  • OK successful command

Error status codes

format: abb the first digit (a) is the category, while the other digits (bb) represent the error itself

POSITIONING SHIPS [category: 1]
  • 00 Invalid ship position
  • 01 Selected ship size not available
  • 02 Selected tile doesn't contain a ship
  • 03 READY command not valid, you still have ships remaining
  • 04 Invalid Orientation (Select H or V)
GENERAL [category: 9]
  • 00 Invalid message
  • 01 Selected coordinates invalid
  • 02 Connection error

battleship-java's People

Contributors

namawho avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.