battle_boats 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dadd59a7577daa0ee72910ae66b905d4064eb074
4
- data.tar.gz: 190263c08aec175df8720e7711d4bf9f842ee9c0
3
+ metadata.gz: 574f4f3e02b7c5cef9b75a07705a746a0c054b2a
4
+ data.tar.gz: 930c4d8fe11f4251f423217a29cfe138b74535a1
5
5
  SHA512:
6
- metadata.gz: 45d24201004142d06df0644891af60f4e38706fdd45b4d991265a9bdc05632beb1302549e7e023c5e4bea87e844dbb8bd937cff0cd96d3c65e6d9a97bbe6d6d4
7
- data.tar.gz: 7b04df2c019d2b82162faa68d4d51270226ebb9e823aad9bbaf94e98574cb7d8ffe2e8313dd4c0068c99978deb882dc6de055f126a2e9ec50ed3e7a6f839a646
6
+ metadata.gz: ff190ee5734297fa9e2de51622790f60faf30340efd252e300353797917b92839bf58f5b8ec88dacd5db19988590bd73dad66c2404a94e3cca2b94ef60fca47c
7
+ data.tar.gz: 38015890c606111c547c2e8d68add8eb856c72169184e72f9a7d361f721546a061988ead35707aa3fa96d72aad16c28e5443b337f07a59936cf032f302cb35e4
data/.rubocop.yml CHANGED
@@ -210,9 +210,9 @@ Style/LineEndConcatenation:
210
210
  Enabled: false
211
211
 
212
212
  Metrics/LineLength:
213
- Description: 'Limit lines to 80 characters.'
213
+ Description: 'Limit lines to 100 characters.'
214
214
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
215
- Max: 80
215
+ Max: 100
216
216
  Exclude:
217
217
  - "spec/**/*"
218
218
 
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.0.10
2
+ ### New Features
3
+ - `battle_boats "dev"` will play in developer mode
4
+ - Randomly placed ships will be visible
5
+
1
6
  # 0.0.9
2
7
  ### New Features
3
8
  - Displays "won" message once all the ship are sunk
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battle_boats (0.0.9)
4
+ battle_boats (0.0.10)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/bin/battle_boats CHANGED
@@ -2,26 +2,17 @@
2
2
 
3
3
  require 'battle_boats/engine'
4
4
  require 'battle_boats/board'
5
- require 'battle_boats/fleet'
6
- require 'battle_boats/coordinate'
5
+ require 'battle_boats/dev_console_ui'
7
6
 
8
7
  board = BattleBoats::Board.new
8
+ board.place_ships_randomly
9
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
10
+ if ARGV[0] == "dev"
11
+ interface = BattleBoats::DevConsoleUI.new
12
+ else
13
+ interface = BattleBoats::ConsoleUI.new
22
14
  end
23
15
 
24
-
25
- engine = BattleBoats::Engine.new(board: board)
16
+ engine = BattleBoats::Engine.new(board: board, interface: interface)
26
17
  engine.start
27
18
 
@@ -3,39 +3,41 @@ require "battle_boats/fleet"
3
3
 
4
4
  module BattleBoats
5
5
  class Board
6
- attr_reader :play_area, :status_report, :error_messages
6
+ attr_reader :fleet, :play_area, :status_report
7
7
 
8
- def initialize
8
+ def initialize(fleet: BattleBoats::Fleet.new)
9
+ @fleet = fleet
9
10
  @status_report = ""
10
- @error_messages = []
11
- @play_area = []
12
- 10.times do
13
- row = []
14
- 10.times do
15
- row << BattleBoats::Cell.new
11
+ @play_area = create_play_area
12
+ end
13
+
14
+ def place_ships_randomly
15
+ @fleet.ships.each do |ship|
16
+ coin_flip = ["heads", "tails"].sample
17
+ if coin_flip == "heads"
18
+ until place_ship_horizontally(coordinate: get_random_coordinate, ship: ship)
19
+ end
20
+ else
21
+ until place_ship_vertically(coordinate: get_random_coordinate, ship: ship)
22
+ end
16
23
  end
17
- @play_area << row
18
24
  end
19
25
  end
20
26
 
21
27
  def strike_position(coordinate:)
22
- validate_position(coordinate: coordinate)
23
- if @error_messages.empty?
24
- cell = cell_at(coordinate: coordinate)
28
+ cell = cell_at(coordinate: coordinate)
29
+ if cell.hit?
30
+ @status_report = "That position has already been hit"
31
+ false
32
+ else
25
33
  cell.strike
26
34
  @status_report = cell.status_report
27
35
  true
28
- else
29
- false
30
36
  end
31
37
  end
32
38
 
33
39
  def game_over?
34
- if BattleBoats::FLEET.all?(&:sunk?)
35
- true
36
- else
37
- false
38
- end
40
+ fleet.ships.all?(&:sunk?)
39
41
  end
40
42
 
41
43
  def cell_at(coordinate:)
@@ -62,23 +64,18 @@ module BattleBoats
62
64
 
63
65
  private
64
66
 
65
- def validate_position(coordinate:)
66
- @error_messages.clear
67
- if !position_available?(coordinate: coordinate)
68
- @error_messages << "That position has already been hit"
67
+ def create_play_area
68
+ Array.new(10) do
69
+ row = []
70
+ 10.times do
71
+ row << BattleBoats::Cell.new
72
+ end
73
+ row
69
74
  end
70
75
  end
71
76
 
72
77
  def within_range?(coordinate:)
73
- if coordinate.row.to_s =~ /^[0-9]$/ && coordinate.column.to_s =~ /^[0-9]$/
74
- true
75
- else
76
- false
77
- end
78
- end
79
-
80
- def position_available?(coordinate:)
81
- !cell_at(coordinate: coordinate).hit?
78
+ coordinate.row.between?(0, 9) && coordinate.column.between?(0, 9)
82
79
  end
83
80
 
84
81
  def occupy_cells(cells:, ship:)
@@ -94,5 +91,9 @@ module BattleBoats
94
91
  def cells_are_occupiable(cells:)
95
92
  cells.none?(&:nil?) && cells.none?(&:occupied?)
96
93
  end
94
+
95
+ def get_random_coordinate
96
+ BattleBoats::Coordinate.random(row: 0..9, column: 0..9)
97
+ end
97
98
  end
98
99
  end
@@ -24,9 +24,9 @@ module BattleBoats
24
24
 
25
25
  def to_s
26
26
  if hit?
27
- occupant.symbol
27
+ occupant.to_ansi
28
28
  else
29
- ".".blue
29
+ to_ansi
30
30
  end
31
31
  end
32
32
 
@@ -42,5 +42,11 @@ module BattleBoats
42
42
  def occupied?
43
43
  !occupant.empty?
44
44
  end
45
+
46
+ private
47
+
48
+ def to_ansi
49
+ "~".blue
50
+ end
45
51
  end
46
52
  end
@@ -29,10 +29,6 @@ module BattleBoats
29
29
  output.puts status_report
30
30
  end
31
31
 
32
- def display_errors(errors)
33
- output.puts errors
34
- end
35
-
36
32
  def win
37
33
  output.puts "You've won the game!"
38
34
  end
@@ -1,5 +1,13 @@
1
1
  module BattleBoats
2
2
  class Coordinate
3
+ class << self
4
+ def random(row:, column:)
5
+ row = rand(row)
6
+ column = rand(column)
7
+ new(row: row, column: column)
8
+ end
9
+ end
10
+
3
11
  attr_reader :row, :column
4
12
 
5
13
  def initialize(row: nil, column: nil)
@@ -0,0 +1,35 @@
1
+ require_relative "console_ui"
2
+ require_relative "coordinate"
3
+ require_relative "colorize"
4
+
5
+ module BattleBoats
6
+ class DevConsoleUI < ConsoleUI
7
+ using Colorize
8
+ def format_board(board)
9
+ board_string = horizontal_line
10
+ board_string << newline
11
+ board_string << column_label
12
+ board_string << horizontal_line
13
+ board_string << newline
14
+ board.play_area.each_with_index do |row, row_number|
15
+ board_string << pipe
16
+ board_string << " #{row_labels[row_number]} "
17
+ board_string << pipe
18
+ row.each do |cell|
19
+ board_string << if cell.hit?
20
+ " #{cell.occupant.symbol.red} "
21
+ elsif cell.occupied?
22
+ " #{cell.occupant.symbol.yellow} "
23
+ else
24
+ " #{cell.occupant.symbol.blue} "
25
+ end
26
+ board_string << pipe
27
+ end
28
+ board_string << newline
29
+ board_string << horizontal_line
30
+ board_string << newline
31
+ end
32
+ board_string
33
+ end
34
+ end
35
+ end
@@ -15,7 +15,7 @@ module BattleBoats
15
15
  interface.display_board(board)
16
16
  coordinate = interface.get_coordinate
17
17
  until board.strike_position(coordinate: coordinate)
18
- interface.display_errors(board.error_messages)
18
+ interface.display_status_report(board.status_report)
19
19
  coordinate = interface.get_coordinate
20
20
  end
21
21
  interface.display_status_report(board.status_report)
@@ -1,11 +1,17 @@
1
1
  require_relative "ship"
2
2
 
3
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
4
+ class Fleet
5
+ attr_reader :ships
6
+
7
+ def initialize
8
+ @ships = [
9
+ BattleBoats::Ship.new(name: "Carrier", length: 5, symbol: "C"),
10
+ BattleBoats::Ship.new(name: "Battleship", length: 4, symbol: "B"),
11
+ BattleBoats::Ship.new(name: "Cruiser", length: 3, symbol: "R"),
12
+ BattleBoats::Ship.new(name: "Submarine", length: 3, symbol: "S"),
13
+ BattleBoats::Ship.new(name: "Destroyer", length: 2, symbol: "D"),
14
+ ]
15
+ end
16
+ end
11
17
  end
@@ -8,13 +8,17 @@ module BattleBoats
8
8
  def initialize
9
9
  @name = "nothing"
10
10
  @length = 1
11
- @symbol = "X".yellow
11
+ @symbol = "X"
12
12
  end
13
13
 
14
14
  def empty?
15
15
  true
16
16
  end
17
17
 
18
+ def to_ansi
19
+ @symbol.yellow
20
+ end
21
+
18
22
  def hit; end
19
23
 
20
24
  def sunk?; end
@@ -8,7 +8,7 @@ module BattleBoats
8
8
  def initialize(name:, length:, symbol: "O")
9
9
  @name = name
10
10
  @length = length
11
- @symbol = symbol.red
11
+ @symbol = symbol
12
12
  @hits = 0
13
13
  end
14
14
 
@@ -16,8 +16,8 @@ module BattleBoats
16
16
  false
17
17
  end
18
18
 
19
- def hit_count
20
- @hits
19
+ def to_ansi
20
+ @symbol.red
21
21
  end
22
22
 
23
23
  def hit
@@ -25,7 +25,7 @@ module BattleBoats
25
25
  end
26
26
 
27
27
  def sunk?
28
- hit_count == length
28
+ @hits == length
29
29
  end
30
30
  end
31
31
  end
@@ -1,3 +1,3 @@
1
1
  module BattleBoats
2
- VERSION = "0.0.9".freeze
2
+ VERSION = "0.0.10".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.9
4
+ version: 0.0.10
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-23 00:00:00.000000000 Z
11
+ date: 2018-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,6 +111,7 @@ files:
111
111
  - lib/battle_boats/colorize.rb
112
112
  - lib/battle_boats/console_ui.rb
113
113
  - lib/battle_boats/coordinate.rb
114
+ - lib/battle_boats/dev_console_ui.rb
114
115
  - lib/battle_boats/engine.rb
115
116
  - lib/battle_boats/fleet.rb
116
117
  - lib/battle_boats/null_ship.rb