Battleship Interview Question Answer

Q: Write a very simple game of battleships. You should:

# 1. Implement a Battleships class

# 2. Implement the method Battleships#hit?(cell_name)

# A submarine has length 1, a destroyer has length 3, and a battleship has # length 5.

require 'rspec'

describe Battleships do

before :all do @ships = { submarine: { position: 'A3', orientation: 'South'

}, destroyer: { position: 'C1', orientation: 'East'

}, battleship: { position: 'G6', orientation: 'West'



@game = @ships end

it 'should hit top of submarine with A3' do

@game.hit?('A3').should be true end

it 'should hit tail of destroyer with E1' do

@game.hit?('E1').should be true end

it 'should hit middle of battleship with G4' do

@game.hit?('G4').should be true end

it 'should hit nothing with G7' do @game.hit?('G7').should be false end




Contact Form