Implementation of a Battleship board, uses a sparse hash to store locations of boats.
bundler
for Dependency managementminitest
for tests. Install withbundle install
Run tests with bundle exec ruby test/test_boat.rb
. The board class remains untested.
Run ruby src/battleship.rb
to run through a sample game.
Here's the output:
Putting boat 0 at 3,1
Putting boat 0 at 3,2
Putting boat 0 at 3,3
Putting boat 0 at 3,4
Putting boat 1 at 8,2
Putting boat 1 at 9,2
_ _ _ _ _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ 1 1 _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
Attacking 0,0 ->
Miss
M _ _ _ _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ 1 1 _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
Attacking 3,1
Hit
M _ _ _ _ _ _ _ _ _ _
_ _ _ X0X _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ 1 1 _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ 0 _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _
Attacking 3,2 ->
Hit
Attacking 3,2 ->
Hit
Attacking 3,2 ->
Sunk
M _ _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