Last active
May 13, 2016 22:03
-
-
Save mariusbutuc/f9340f97b05aa173b2d757abb11d3a6a to your computer and use it in GitHub Desktop.
Kata #6: Battleship https://en.wikipedia.org/wiki/Battleship_(game)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
class Battleship | |
DEFAULT_ROWS_COUNT = 10 | |
DEFAULT_COLUMNS_COUNT = 10 | |
VERTICAL = 'ns'.freeze | |
HORIZONTAL = 'ew'.freeze | |
HIT = '*'.freeze | |
MISS = 'x'.freeze | |
class Ship | |
attr_reader :player, :row, :column, :length, :orientation | |
def initialize(player:, row:, column:, length:, orientation:) | |
@player = player | |
@row = row | |
@column = column | |
@length = length | |
@orientation = orientation | |
# http://www.midwestconnection.org/glshpng/glossary.htm | |
def hull | |
orientation == Battleship::VERTICAL ? populate_vertically : populate_vertically | |
end | |
end | |
end | |
attr_reader :rows, :columns, :ships | |
def initialize(rows: DEFAULT_ROWS_COUNT, columns: DEFAULT_COLUMNS_COUNT) | |
@rows = rows | |
@columns = columns | |
@ships = [] | |
end | |
def add_ship(player:, row:, column:, length:, orientation:) | |
ship = Ship.new(player: player, row: row, column: column, length: length, orientation: orientation) | |
ships << ship | |
ship | |
end | |
def ships_for(player:) | |
ships.select { |ship| ship.player == player } | |
end | |
def attack(player:, row:, column:) | |
# | |
end | |
# board_status | |
def board(player:, row:, column:) | |
ships_for(player: player).select { |ship| ship.row == row && ship.column == column } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'minitest' | |
gem 'pry-byebug' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './battleship.rb' | |
require 'minitest/autorun' | |
require 'pry' | |
describe Battleship do | |
def battleship | |
@battleship ||= Battleship.new(**board_size) | |
end | |
def board_size | |
@board_size ||= { rows: 10, columns: 10 } | |
end | |
describe 'initialization' do | |
it "sets the board's rows" do | |
assert_equal board_size[:rows], battleship.rows | |
end | |
it "sets the board's columns" do | |
assert_equal board_size[:columns], battleship.columns | |
end | |
end | |
describe '#add_ship' do | |
it 'adds a ship at the given coordinates' do | |
battleship.add_ship(player: 1, row: 3, column: 4, length: 5, orientation: Battleship::VERTICAL) | |
refute_empty battleship.ships_for(player: 1) | |
battleship.ships_for(player: 1).each do |ship| | |
assert_equal 1, ship.player | |
assert_equal 3, ship.row | |
assert_equal 4, ship.column | |
assert_equal 5, ship.length | |
assert_equal Battleship::VERTICAL, ship.orientation | |
end | |
end | |
end | |
describe '#attack' do | |
it 'records a miss' do | |
row = 5 | |
column = 5 | |
result = battleship.attack(player: 1, row: row, column: column) | |
assert_equal Battleship::MISS, result | |
assert_equal Battleship::MISS, battleship.board(player: 1, row: row, column: column) | |
end | |
it 'records a hit' do | |
row = 5 | |
column = 5 | |
ship = battleship.add_ship(player: 1, row: row - 1, column: column, length: 5, orientation: Battleship::VERTICAL) | |
result = battleship.attack(player: 1, row: row, column: column) | |
assert_equal Battleship::HIT, result | |
assert_equal Battleship::HIT, battleship.board(player: 1, row: row, column: column) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment