battle_boats 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d417f15deb5ea35be4829d14c0af66e5fd347e8
4
- data.tar.gz: 75522bcf8e4e69dad64495f76962cc1b0178dccd
3
+ metadata.gz: 0fc91ef81269dd27748b5d9d56ce2ca769e97eac
4
+ data.tar.gz: 191033ffb7cda58c5f6e9ae5a630434f1f3ae511
5
5
  SHA512:
6
- metadata.gz: a217b9891b88d5ea077dce6ef3787852bbcb6d68add6410709ddde5f8ac6d9ba44eadfbec3425616e9e0a1ee5809e1a41f4c306fe34c1414b3208c4a978ec10b
7
- data.tar.gz: 53109099edc9b6228995faba7a6a7e85a6fcc7d47a704164babb9616fb319c77c4ec078c880f114792e90101f16a3b151a7c0c98d61216c7ed7e760ba6422713
6
+ metadata.gz: 496aacce57286fbc9a644c889eb58a7ca49d67d3880194a10ec88014b81dd9126ecf09ee1c22e69b3d046b647b6050de9504f2ae59dc9e1698977413b7b6bd13
7
+ data.tar.gz: a2eab93f0dd9c5b4e4212767d71e7b568fd97ced611c16446bbc655817c7fdb7cdd2731b35a019457eea73f21bf09651de8becfae3efc2dbc3bfa6177b68a327
data/.codeclimate.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  version: "2"
2
2
  plugins:
3
3
  rubocop:
4
- enable: true
4
+ enabled: true
5
5
  exclude_patterns:
6
6
  - 'spec/*'
7
7
  - 'Gemfile'
data/.rubocop.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  AllCops:
2
2
  Exclude:
3
- - 'spec/**/**'
4
3
  - 'Gemfile'
5
4
  - 'Rakefile'
6
5
  - '*.gemspec'
@@ -214,6 +213,8 @@ Metrics/LineLength:
214
213
  Description: 'Limit lines to 80 characters.'
215
214
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
216
215
  Max: 80
216
+ Exclude:
217
+ - "spec/**/*"
217
218
 
218
219
  Metrics/MethodLength:
219
220
  Description: 'Avoid methods longer than 10 lines of code.'
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.0.5
2
+ ### New Features
3
+ - Starting the game will display a board of randomly placed enemy ships
4
+ - Hitting a ship will display a "You hit my `<<ship>>`" message
5
+ - Hitting a ship will display the initial of the ship you've hit on the board
6
+
1
7
  # 0.0.4
2
8
  ### New Features
3
9
  - Error messages for bad input
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battle_boats (0.0.4)
4
+ battle_boats (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Battle Boats [![Build Status](https://travis-ci.org/Thomascountz/battle_boats.svg?branch=master)](https://travis-ci.org/Thomascountz/battle_boats) [![Maintainability](https://api.codeclimate.com/v1/badges/f9fb2b053e6bad40b13e/maintainability)](https://codeclimate.com/github/Thomascountz/battle_boats/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/f9fb2b053e6bad40b13e/test_coverage)](https://codeclimate.com/github/Thomascountz/battle_boats/test_coverage)
1
+ # Battle Boats [![Gem Version](https://badge.fury.io/rb/battle_boats.svg)](https://badge.fury.io/rb/battle_boats) [![Build Status](https://travis-ci.org/Thomascountz/battle_boats.svg?branch=master)](https://travis-ci.org/Thomascountz/battle_boats) [![Maintainability](https://api.codeclimate.com/v1/badges/f9fb2b053e6bad40b13e/maintainability)](https://codeclimate.com/github/Thomascountz/battle_boats/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/f9fb2b053e6bad40b13e/test_coverage)](https://codeclimate.com/github/Thomascountz/battle_boats/test_coverage)
2
2
 
3
3
 
4
4
  Battle Boats is a ruby library implementation of [Battleship](https://en.wikipedia.org/wiki/Battleship_%28game%29)!
data/bin/battle_boats CHANGED
@@ -1,4 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'battle_boats/engine'
4
- BattleBoats::Engine.new.start
4
+ require 'battle_boats/board'
5
+ require 'battle_boats/fleet'
6
+ require 'battle_boats/coordinate'
7
+
8
+ board = BattleBoats::Board.new
9
+
10
+ BattleBoats::FLEET.each do |ship|
11
+ coin_toss = [1, 2].sample
12
+ random_coordinate = BattleBoats::Coordinate.new(row: rand(0..9), column: rand(0..9))
13
+ if coin_toss == 1
14
+ until board.place_ship_horizontally(coordinate: random_coordinate, ship: ship)
15
+ random_coordinate = BattleBoats::Coordinate.new(row: rand(0..9), column: rand(0..9))
16
+ end
17
+ else
18
+ until board.place_ship_vertically(coordinate: random_coordinate, ship: ship)
19
+ random_coordinate = BattleBoats::Coordinate.new(row: rand(0..9), column: rand(0..9))
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ engine = BattleBoats::Engine.new(board: board)
26
+ engine.start
27
+
@@ -1,3 +1,5 @@
1
+ require "battle_boats/cell"
2
+
1
3
  module BattleBoats
2
4
  class Board
3
5
  attr_reader :play_area, :status_report, :error_messages
@@ -9,17 +11,18 @@ module BattleBoats
9
11
  10.times do
10
12
  row = []
11
13
  10.times do
12
- row << nil
14
+ row << BattleBoats::Cell.new
13
15
  end
14
16
  @play_area << row
15
17
  end
16
18
  end
17
19
 
18
- def strike_position(row:, column:)
19
- validate_position(row: row, column: column)
20
+ def strike_position(coordinate:)
21
+ validate_position(coordinate: coordinate)
20
22
  if @error_messages.empty?
21
- @play_area[row.to_i][column.to_i] = "X"
22
- @status_report = "Miss!"
23
+ cell = cell_at(coordinate: coordinate)
24
+ cell.strike
25
+ @status_report = cell.status_report
23
26
  true
24
27
  else
25
28
  false
@@ -30,24 +33,46 @@ module BattleBoats
30
33
  false
31
34
  end
32
35
 
36
+ def cell_at(coordinate:)
37
+ if within_range?(coordinate.row) && within_range?(coordinate.column)
38
+ @play_area[coordinate.row.to_i][coordinate.column.to_i]
39
+ end
40
+ end
41
+
42
+ def place_ship_horizontally(coordinate:, ship:)
43
+ cells_to_occupy = Array.new(ship.length) do |offset|
44
+ cell_at(coordinate: coordinate.right(offset: offset))
45
+ end
46
+
47
+ occupy_cells(cells: cells_to_occupy, ship: ship)
48
+ end
49
+
50
+ def place_ship_vertically(coordinate:, ship:)
51
+ cells_to_occupy = Array.new(ship.length) do |offset|
52
+ cell_at(coordinate: coordinate.up(offset: offset))
53
+ end
54
+
55
+ occupy_cells(cells: cells_to_occupy, ship: ship)
56
+ end
57
+
33
58
  private
34
59
 
35
- def validate_position(row:, column:)
60
+ def validate_position(coordinate:)
36
61
  @error_messages.clear
37
- if !between_zero_and_nine?(row)
62
+ if !within_range?(coordinate.row)
38
63
  @error_messages << "The selected row is invalid"
39
64
  end
40
- if !between_zero_and_nine?(column)
65
+ if !within_range?(coordinate.column)
41
66
  @error_messages << "The selected column is invalid"
42
67
  end
43
68
  if @error_messages.empty?
44
- if !position_available?(row: row, column: column)
69
+ if !position_available?(coordinate: coordinate)
45
70
  @error_messages << "That position has already been hit"
46
71
  end
47
72
  end
48
73
  end
49
74
 
50
- def between_zero_and_nine?(input)
75
+ def within_range?(input)
51
76
  if input.to_s =~ /^[0-9]$/
52
77
  true
53
78
  else
@@ -55,8 +80,22 @@ module BattleBoats
55
80
  end
56
81
  end
57
82
 
58
- def position_available?(row:, column:)
59
- @play_area[row.to_i][column.to_i] != "X"
83
+ def position_available?(coordinate:)
84
+ !cell_at(coordinate: coordinate).hit?
85
+ end
86
+
87
+ def occupy_cells(cells:, ship:)
88
+ if cells_are_occupiable(cells)
89
+ cells.each do |cell|
90
+ cell.occupant = ship
91
+ end
92
+ else
93
+ false
94
+ end
95
+ end
96
+
97
+ def cells_are_occupiable(cells)
98
+ cells.none?(&:nil?) && cells.none?(&:occupied?)
60
99
  end
61
100
  end
62
101
  end
@@ -0,0 +1,42 @@
1
+ require_relative "null_ship"
2
+
3
+ module BattleBoats
4
+ class Cell
5
+ attr_accessor :occupant
6
+
7
+ def initialize
8
+ @hit = false
9
+ @occupant = BattleBoats::NullShip.new
10
+ end
11
+
12
+ def hit?
13
+ @hit
14
+ end
15
+
16
+ def strike
17
+ if !hit?
18
+ @hit = true
19
+ end
20
+ end
21
+
22
+ def to_s
23
+ if hit?
24
+ occupant.symbol
25
+ else
26
+ "."
27
+ end
28
+ end
29
+
30
+ def status_report
31
+ if hit?
32
+ "You hit my #{occupant.name}!"
33
+ else
34
+ "All Clear!"
35
+ end
36
+ end
37
+
38
+ def occupied?
39
+ !occupant.empty?
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,5 @@
1
+ require_relative "coordinate"
2
+
1
3
  module BattleBoats
2
4
  class ConsoleUI
3
5
  def initialize(output: $stdout, input: $stdin)
@@ -13,14 +15,10 @@ module BattleBoats
13
15
  output.puts format_board(board)
14
16
  end
15
17
 
16
- def get_row
17
- output.puts "Target row:"
18
- input.gets.chomp
19
- end
20
-
21
- def get_column
22
- output.puts "Target column:"
23
- input.gets.chomp
18
+ def get_coordinate
19
+ row = get_row
20
+ column = get_column
21
+ BattleBoats::Coordinate.new(row: row, column: column)
24
22
  end
25
23
 
26
24
  def display_status_report(status_report)
@@ -46,11 +44,7 @@ module BattleBoats
46
44
  board_string << " #{row_number} "
47
45
  board_string << pipe
48
46
  row.each do |cell|
49
- board_string << if cell
50
- " #{cell} "
51
- else
52
- " . "
53
- end
47
+ board_string << " #{cell} "
54
48
  board_string << pipe
55
49
  end
56
50
  board_string << newline
@@ -75,5 +69,15 @@ module BattleBoats
75
69
  def pipe
76
70
  "|"
77
71
  end
72
+
73
+ def get_row
74
+ output.puts "Target row:"
75
+ input.gets.chomp
76
+ end
77
+
78
+ def get_column
79
+ output.puts "Target column:"
80
+ input.gets.chomp
81
+ end
78
82
  end
79
83
  end
@@ -0,0 +1,18 @@
1
+ module BattleBoats
2
+ class Coordinate
3
+ attr_reader :row, :column
4
+
5
+ def initialize(row: nil, column: nil)
6
+ @row = row
7
+ @column = column
8
+ end
9
+
10
+ def up(offset: 1)
11
+ self.class.new(row: row - offset, column: column)
12
+ end
13
+
14
+ def right(offset: 1)
15
+ self.class.new(row: row, column: column + offset)
16
+ end
17
+ end
18
+ end
@@ -13,12 +13,10 @@ module BattleBoats
13
13
  interface.greet
14
14
  until board.game_over?
15
15
  interface.display_board(board)
16
- row = interface.get_row
17
- column = interface.get_column
18
- until board.strike_position(row: row, column: column)
16
+ coordinate = interface.get_coordinate
17
+ until board.strike_position(coordinate: coordinate)
19
18
  interface.display_errors(board.error_messages)
20
- row = interface.get_row
21
- column = interface.get_column
19
+ coordinate = interface.get_coordinate
22
20
  end
23
21
  interface.display_status_report(board.status_report)
24
22
  end
@@ -0,0 +1,11 @@
1
+ require_relative "ship"
2
+
3
+ module BattleBoats
4
+ FLEET = [
5
+ BattleBoats::Ship.new(name: "Carrier", length: 5, symbol: "C"),
6
+ BattleBoats::Ship.new(name: "Battleship", length: 4, symbol: "B"),
7
+ BattleBoats::Ship.new(name: "Cruiser", length: 3, symbol: "R"),
8
+ BattleBoats::Ship.new(name: "Submarine", length: 3, symbol: "S"),
9
+ BattleBoats::Ship.new(name: "Destroyer", length: 2, symbol: "D"),
10
+ ].freeze
11
+ end
@@ -0,0 +1,15 @@
1
+ module BattleBoats
2
+ class NullShip
3
+ attr_reader :name, :length, :symbol
4
+
5
+ def initialize
6
+ @name = "nothing"
7
+ @length = 1
8
+ @symbol = "X"
9
+ end
10
+
11
+ def empty?
12
+ true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module BattleBoats
2
+ class Ship
3
+ attr_reader :name, :length, :symbol
4
+
5
+ def initialize(name:, length:, symbol: "O")
6
+ @name = name
7
+ @length = length
8
+ @symbol = symbol
9
+ end
10
+
11
+ def empty?
12
+ false
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module BattleBoats
2
- VERSION = "0.0.4".freeze
2
+ VERSION = "0.0.5".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battle_boats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Countz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-14 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,8 +107,13 @@ files:
107
107
  - bin/setup
108
108
  - lib/battle_boats.rb
109
109
  - lib/battle_boats/board.rb
110
+ - lib/battle_boats/cell.rb
110
111
  - lib/battle_boats/console_ui.rb
112
+ - lib/battle_boats/coordinate.rb
111
113
  - lib/battle_boats/engine.rb
114
+ - lib/battle_boats/fleet.rb
115
+ - lib/battle_boats/null_ship.rb
116
+ - lib/battle_boats/ship.rb
112
117
  - lib/battle_boats/version.rb
113
118
  homepage: https://www.github.com/thomascountz/battle_boats
114
119
  licenses: