Skip to content

Instantly share code, notes, and snippets.

@kyleries
Created November 3, 2016 19:41
Show Gist options
  • Save kyleries/cd30cb4b9db28351ff59cba2d38f4614 to your computer and use it in GitHub Desktop.
Save kyleries/cd30cb4b9db28351ff59cba2d38f4614 to your computer and use it in GitHub Desktop.
# Battleship Coding Challenge
# Usage: rspec battleship.rb
#
# Example Board
#
#
# 0 1 2 3 4 5 6 7 8 9
# A
# B N
# C ^
# D W< >E
# E v
# F S
# G
# H
# I
# J
class Battleship
end
describe Battleship do
let(:carrier) do
{ type: :carrier, position: 'A6', direction: :south, length: 5 }
end
let(:battleship) do
{ type: :battleship, position: 'D9', direction: :south, length: 4 }
end
let(:submarine) do
{ type: :submarine, position: 'I7', direction: :north, length: 3 }
end
let(:game) { Battleship.new([carrier, battleship, submarine]) }
subject { game }
context 'hits' do
it 'the beginning edge of the Submarine' do
turn = subject.fire!('I7') # Hits the Submarine
expect(turn).to be true
end
it 'the middle of the Battleship' do
turn = subject.fire!('G9') # Hits the Battleship
expect(turn).to be true
end
it 'the ending edge of the Carrier' do
turn = subject.fire!('E6') # Hits the Carrier
expect(turn).to be true
end
it 'returns type of ship when sunk' do
turn = subject.fire!('I7') # Hits the Submarine
expect(turn).to be true
turn = subject.fire!('H7') # Hits the Submarine
expect(turn).to be true
turn = subject.fire!('G7') # Hits the Submarine
expect(turn).to be :submarine
end
end
context 'misses' do
it 'and hits the middle of the ocean' do
turn = subject.fire!('E3')
expect(turn).to be false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment