Giter Site home page Giter Site logo

distributed_battleship's Introduction

Distributed Battleship

Summary

A battleship game orchestrates many players. Each player is a node on the erlang network and can interact with the commander with the following messsages:

  • Once the Game Commander is started players will have some time to start their player and connect.
  • All players will be sent a notification that the game is started with the ocean size and the number of ship parts they can allocated.
  • Each player will be able to add a set of ship parts to make up a number of ships.
  • The commander will inform the players its time to take a turn and the players can submit their guesses as to where their opponents have placed their ships.
  • Play containues until there is one player remaining with surviving ships.

Setup

Imaging a room full of eager Elixir programmers wanting to pit their battleships against their opponents. The game progresses like this.

Introduction

First get over all the firewall issues which are bound to hurt. The game uses erlang clustering which uses the following ports.

  • 4369 for primary connections.
  • 9000 - 9100 for allocation to each player.

Play

  • Commander starts a new game.
  • Players connect.
  • The private UI shows the board as ships are added.
  • The public UI shows players names, a count of the ships they are adding and an empty rippling ocean.
  • Commence firing.

Rule Differences

The rules for normal 2 player battleships work well but some changes are needed to scale to more people.

  • When you add ships to a shared ocean, there may already be a ship there. This gives players another way to learn where opponents ships are. Is this fare? Of coarse it is. Track collusions and plan your bombing runs.
  • If you strike an opponents ship who should know about it? You should, of coarse, but if all players also know it then you can organize into attack groups against a single foe, which is better.
  • If you miss should your opponents know? Every guess improves the next players guess, unless there are no turns, just a free flow of bombs launched by your best AI. Then seeing your opponents misses will slow you down, and make the game better.

Scaling

The game scales as more players register. The ratios are:

  • Ocean size: number of players * 10 square
  • Number of ship parts per player: ocean size * 0.75

Running

There are a set of scripts that help the distributed game start. First start the server.

bin/start_battleships.sh

Then connect up the user interface to see the game progress.

bin/ui [--private]

Player API

When the commander starts you will get its IP address. You will need this to connect to the erlang network:

commander_ip=1.1.1.1

To start your beam you will need to add the erlang networking configutation for this cluster:

player_name=rose_petal
iex --erl '-kernel inet_dist_listen_min 9000' --erl '-kernel inet_dist_listen_max 9100' --name $player_name@$commander_ip -r lib/my_player_code.ex -e "MyPlayer.start('$player_name')"

Network connection first, this adds your beam into the same erlang network as the commander.

Node.connect(:"commander@#{commander_ip}")

How to send message to the commanders services.

pid = :global.whereis_name(:players)
GenServer.call(pid, {:register, player_name})

Messages that you will send during the game.

| Name         | Registry |  Format                                                                                                     |
|--------------|------------------------------------------------------------------------------------------------------------------------|
| Register     | :players | {:register, player_name}                                                                                    |
| Add ships    | :ocean   | {:add_ship, %{player: player_name, from: %{from_x: from_x, from_y: from_y}, to: %{to_x: to_x, to_y: to_y}}} |
| Take a turn  | :turns   | {:take, player_name, position = %{x: x, y: y}}                                                              |

Messages that you will receive during the game.

| Name        | Format                                                             |
|-------------|--------------------------------------------------------------------|
| Congrats    | {"congratulations", ocean_size, max_ship_parts}                    |
| Turn Result | {playe_name, position, :hit/:miss}                                 |
| Winner      | {:game_over, [{winner: player_name}]}                              |

Game Commander Design

Game phases:

  • Await players

Players connect and send in their names.

  • Start game

Players are told its time to start with the ocean size, and the number of ship parts they can use.

  • Player ships

Players send in ship location choises.

  • Player turns

Each player can take a turn to blow up an opponent.

  • Player feedback

Everyone gets feedback on who won the game.

Program Flow

The game is driven by a sequence of ticks and a predictable period. Every X ticks events can occur. A game is split into phases represented by states. Ticks initiate a step based on the phase.

distributed_battleship's People

Contributors

esumerfd avatar zkayser avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

distributed_battleship's Issues

Refactor all ship positions to a struct

Currently I am just passing startx starty endx and endy around and this is cumbersome when dealing with multiple ships and the tuples prevent easy x or y access with out explicit elem calls.

As struct will be simpler

Players need to be able to thrown bombs

Players can add ships now but next they need to thrown a bomb and get feedback on the result.

The Ocean would need to search through the ships to find out if one was hit an mark it as blown up.

Acceptance Criteria:

  • player message specifies x,y target
  • player response is hit or miss.
  • Update ocean ship state
  • all players receive hit or miss.

Allow players to crash and reconnect

We could use the register to store players pid's perhaps?

At least allow the registered players hash to be updated when a player reconnects.

Should a reconnection be possible mid-game?

Finish phase

Once there is only one player with ship parts left then the game is over and the finish phase is entered. This results in each player receiving the statistics describing what happened during the game. For example,

  • You WON/LOST
  • Hit X ships with Y bumbs
  • Missed X times
  • Deployed X ships with Y parts.
  • Winner was NAME.

UI Protocol for Ocean

We dont know how to send data to a UI yet. Any UI should be able to pull all data or just a deltas so that it can manage the rendering of the information.

I was thinking we could build a simple command line to pull this info and write out a character graphics rendering to start with.

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.