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 +4 -4
- data/.rubocop.yml +2 -2
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/bin/battle_boats +7 -16
- data/lib/battle_boats/board.rb +33 -32
- data/lib/battle_boats/cell.rb +8 -2
- data/lib/battle_boats/console_ui.rb +0 -4
- data/lib/battle_boats/coordinate.rb +8 -0
- data/lib/battle_boats/dev_console_ui.rb +35 -0
- data/lib/battle_boats/engine.rb +1 -1
- data/lib/battle_boats/fleet.rb +13 -7
- data/lib/battle_boats/null_ship.rb +5 -1
- data/lib/battle_boats/ship.rb +4 -4
- data/lib/battle_boats/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 574f4f3e02b7c5cef9b75a07705a746a0c054b2a
|
4
|
+
data.tar.gz: 930c4d8fe11f4251f423217a29cfe138b74535a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
213
|
+
Description: 'Limit lines to 100 characters.'
|
214
214
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
|
215
|
-
Max:
|
215
|
+
Max: 100
|
216
216
|
Exclude:
|
217
217
|
- "spec/**/*"
|
218
218
|
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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/
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
|
data/lib/battle_boats/board.rb
CHANGED
@@ -3,39 +3,41 @@ require "battle_boats/fleet"
|
|
3
3
|
|
4
4
|
module BattleBoats
|
5
5
|
class Board
|
6
|
-
attr_reader :
|
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
|
-
@
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
23
|
-
if
|
24
|
-
|
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
|
-
|
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
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
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
|
data/lib/battle_boats/cell.rb
CHANGED
@@ -24,9 +24,9 @@ module BattleBoats
|
|
24
24
|
|
25
25
|
def to_s
|
26
26
|
if hit?
|
27
|
-
occupant.
|
27
|
+
occupant.to_ansi
|
28
28
|
else
|
29
|
-
|
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
|
@@ -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
|
data/lib/battle_boats/engine.rb
CHANGED
@@ -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.
|
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)
|
data/lib/battle_boats/fleet.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
require_relative "ship"
|
2
2
|
|
3
3
|
module BattleBoats
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
data/lib/battle_boats/ship.rb
CHANGED
@@ -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
|
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
|
20
|
-
@
|
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
|
-
|
28
|
+
@hits == length
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/lib/battle_boats/version.rb
CHANGED
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
|
+
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-
|
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
|