Code Monkey home page Code Monkey logo

battleships-1's Introduction

Referenced from Makers Academy Battleships (private repo).

Battleships (or Battleship) is a game where each player has a board upon which they can place a number of ships. The boards are 10x10 two dimensional grid.

After each player has placed their ships on their own board they take turns to shoot on the opponents board. In each round, each player takes a turn to announce a target square in the opponent's grid which is to be shot at. The opponent announces whether or not the square is occupied by a ship, and if it is a hit they mark this on their own primary grid. The attacking player notes the hit or miss on their own tracking grid, in order to build up a picture of the opponent's fleet.

When all of one player's ships have been sunk the game finishes and the player who has ships remaining is the winner.

The code is intended to demonstrate a range of techniques:

  • Dependency inversion (e.g. Game with Player and Board ) over tighter dependencies (e.g. Board and Cell)
  • TDD and BDD
  • Factory methods (e.g. Ship.submarine)
  • Refactoring to create service objects (e.g. CoordinateHandler)
  • Implementing Enumerable (CoordinateHandler)
  • Emergent design. No part of the code architecture was decided on up front. All classes and refactoring emerged as a consequence of further tests being addded.

The game is fully playable in irb:

$ irb
> require './battleships'

Now create a new game using Game, Player and Board classes:

> game = Game.new Player, Board

Each player can place ships on their own board:

> game.player_1.place_ship Ship.battleship, :B4, :vertically

> game.player_2.place_ship Ship.carrier, :G6

Players can shoot at each other's boards:

> game.player_1.shoot :C2
 => :miss
> game.player_2.shoot :B4
 => :hit

Players can view their own boards:

> puts game.own_board_view game.player_1
   ABCDEFGHIJ
  ------------
 1|          |1
 2|          |2
 3|          |3
 4| *        |4
 5| B        |5
 6| B        |6
 7| B        |7
 8|          |8
 9|          |9
10|          |10
  ------------
   ABCDEFGHIJ
   
> puts game.own_board_view game.player_2
   ABCDEFGHIJ
  ------------
 1|          |1
 2|  -       |2
 3|          |3
 4|          |4
 5|          |5
 6|      CCC |6
 7|          |7
 8|          |8
 9|          |9
10|          |10
  ------------
   ABCDEFGHIJ

And their opponent's boards:

> puts game.opponent_board_view game.player_1
   ABCDEFGHIJ
  ------------
 1|          |1
 2|  -       |2
 3|          |3
 4|          |4
 5|          |5
 6|          |6
 7|          |7
 8|          |8
 9|          |9
10|          |10
  ------------
   ABCDEFGHIJ

> puts game.opponent_board_view game.player_1
   ABCDEFGHIJ
  ------------
 1|          |1
 2|          |2
 3|          |3
 4| *        |4
 5|          |5
 6|          |6
 7|          |7
 8|          |8
 9|          |9
10|          |10
  ------------
   ABCDEFGHIJ

Players can sink their opponent's ships:

game.player_2.shoot :B5
 => :hit
game.player_2.shoot :B6
 => :hit
game.player_2.shoot :B7
 => :sunk

And the game can be tested for a winner:

> game.has_winner?
 => true
> game.winner? game.player_1
 => false
> game.player_2.winner?
 => true

battleships-1's People

Contributors

silvabox avatar

Watchers

James Cloos avatar Anna Klimas 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.