battle_boats 0.0.10 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/bin/battle_boats +1 -8
- data/lib/battle_boats.rb +10 -7
- data/lib/battle_boats/board.rb +30 -36
- data/lib/battle_boats/board_formatter.rb +77 -0
- data/lib/battle_boats/cell.rb +0 -16
- data/lib/battle_boats/console_ui.rb +35 -53
- data/lib/battle_boats/null_ship.rb +0 -7
- data/lib/battle_boats/ship.rb +0 -7
- data/lib/battle_boats/version.rb +1 -1
- metadata +3 -3
- data/lib/battle_boats/dev_console_ui.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c234594bcdb65723e19dd4386b5fc3fff1a12e11
|
4
|
+
data.tar.gz: 831459a8cbe3467c52984597f7242bb9048ea008
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a809442d9468226c1bbe15ce3463a2c4c59336a2105c2c00dc70851c952cfda7fe36acd897fcf859b76d6d65349b21a0dc08f5cfca917f06728a7b1fb114493
|
7
|
+
data.tar.gz: ef473db7cb3250ea218cc03c4f33cd25655816d8c47d4595d96dd3a36c9f0575e44ef241506ba91ac3a976cf6c5df6a4a24e5360f9c35fad046b166f5ea3b92d
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/bin/battle_boats
CHANGED
@@ -2,17 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'battle_boats/engine'
|
4
4
|
require 'battle_boats/board'
|
5
|
-
require 'battle_boats/dev_console_ui'
|
6
5
|
|
7
6
|
board = BattleBoats::Board.new
|
8
7
|
board.place_ships_randomly
|
9
8
|
|
10
|
-
|
11
|
-
interface = BattleBoats::DevConsoleUI.new
|
12
|
-
else
|
13
|
-
interface = BattleBoats::ConsoleUI.new
|
14
|
-
end
|
15
|
-
|
16
|
-
engine = BattleBoats::Engine.new(board: board, interface: interface)
|
9
|
+
engine = BattleBoats::Engine.new(board: board)
|
17
10
|
engine.start
|
18
11
|
|
data/lib/battle_boats.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
require "battle_boats/version"
|
2
|
-
require "battle_boats/engine"
|
3
|
-
require "battle_boats/console_ui"
|
4
1
|
require "battle_boats/board"
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
require "battle_boats/board_formatter"
|
3
|
+
require "battle_boats/cell"
|
4
|
+
require "battle_boats/colorize"
|
5
|
+
require "battle_boats/console_ui"
|
6
|
+
require "battle_boats/coordinate"
|
7
|
+
require "battle_boats/engine"
|
8
|
+
require "battle_boats/fleet"
|
9
|
+
require "battle_boats/null_ship"
|
10
|
+
require "battle_boats/ship"
|
11
|
+
require "battle_boats/version"
|
data/lib/battle_boats/board.rb
CHANGED
@@ -13,13 +13,33 @@ module BattleBoats
|
|
13
13
|
|
14
14
|
def place_ships_randomly
|
15
15
|
@fleet.ships.each do |ship|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
until ship_deployed?(ship: ship)
|
17
|
+
coordinate = get_random_coordinate
|
18
|
+
orientation = %i[horizontal vertical].sample
|
19
|
+
attempt_to_deploy_ship(ship: ship, coordinate: coordinate, orientation: orientation)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def ship_deployed?(ship:)
|
25
|
+
play_area.flatten.map(&:occupant).include?(ship)
|
26
|
+
end
|
27
|
+
|
28
|
+
def attempt_to_deploy_ship(ship:, coordinate:, orientation:)
|
29
|
+
cells = Array.new(ship.length) do |offset|
|
30
|
+
if orientation == :horizontal
|
31
|
+
next_coordinate = coordinate.right(offset: offset)
|
32
|
+
elsif orientation == :vertical
|
33
|
+
next_coordinate = coordinate.up(offset: offset)
|
20
34
|
else
|
21
|
-
|
22
|
-
|
35
|
+
return nil
|
36
|
+
end
|
37
|
+
cell_at(coordinate: next_coordinate)
|
38
|
+
end
|
39
|
+
|
40
|
+
if cells_occupiable?(cells: cells)
|
41
|
+
cells.each do |cell|
|
42
|
+
cell.occupant = ship
|
23
43
|
end
|
24
44
|
end
|
25
45
|
end
|
@@ -46,22 +66,6 @@ module BattleBoats
|
|
46
66
|
end
|
47
67
|
end
|
48
68
|
|
49
|
-
def place_ship_horizontally(coordinate:, ship:)
|
50
|
-
cells_to_occupy = Array.new(ship.length) do |offset|
|
51
|
-
cell_at(coordinate: coordinate.right(offset: offset))
|
52
|
-
end
|
53
|
-
|
54
|
-
occupy_cells(cells: cells_to_occupy, ship: ship)
|
55
|
-
end
|
56
|
-
|
57
|
-
def place_ship_vertically(coordinate:, ship:)
|
58
|
-
cells_to_occupy = Array.new(ship.length) do |offset|
|
59
|
-
cell_at(coordinate: coordinate.up(offset: offset))
|
60
|
-
end
|
61
|
-
|
62
|
-
occupy_cells(cells: cells_to_occupy, ship: ship)
|
63
|
-
end
|
64
|
-
|
65
69
|
private
|
66
70
|
|
67
71
|
def create_play_area
|
@@ -74,22 +78,12 @@ module BattleBoats
|
|
74
78
|
end
|
75
79
|
end
|
76
80
|
|
77
|
-
def
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
def occupy_cells(cells:, ship:)
|
82
|
-
if cells_are_occupiable(cells: cells)
|
83
|
-
cells.each do |cell|
|
84
|
-
cell.occupant = ship
|
85
|
-
end
|
86
|
-
else
|
87
|
-
false
|
88
|
-
end
|
81
|
+
def cells_occupiable?(cells:)
|
82
|
+
cells.none?(&:nil?) && cells.none?(&:occupied?)
|
89
83
|
end
|
90
84
|
|
91
|
-
def
|
92
|
-
|
85
|
+
def within_range?(coordinate:)
|
86
|
+
coordinate.row.between?(0, 9) && coordinate.column.between?(0, 9)
|
93
87
|
end
|
94
88
|
|
95
89
|
def get_random_coordinate
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative "colorize"
|
2
|
+
|
3
|
+
module BattleBoats
|
4
|
+
class BoardFormatter
|
5
|
+
using Colorize
|
6
|
+
def format_board(board, hide_ships: true)
|
7
|
+
board_string = horizontal_line
|
8
|
+
board_string << newline
|
9
|
+
board_string << column_header
|
10
|
+
board_string << horizontal_line
|
11
|
+
board_string << newline
|
12
|
+
board.play_area.each_with_index do |row, row_number|
|
13
|
+
board_string << pipe
|
14
|
+
board_string << " #{row_labels[row_number]} "
|
15
|
+
board_string << pipe
|
16
|
+
row.each do |cell|
|
17
|
+
board_string << if cell.occupied? && cell.hit?
|
18
|
+
" #{cell.occupant.symbol.red} "
|
19
|
+
elsif cell.occupied? && !hide_ships
|
20
|
+
" #{cell.occupant.symbol.yellow} "
|
21
|
+
elsif cell.hit?
|
22
|
+
" #{'X'.yellow} "
|
23
|
+
else
|
24
|
+
" #{'~'.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
|
+
|
35
|
+
def valid_coordinate_input?(input)
|
36
|
+
input =~ /^[A-J][0-9]$/i
|
37
|
+
end
|
38
|
+
|
39
|
+
def input_to_coordinate(input)
|
40
|
+
input_row = input[0]
|
41
|
+
input_column = input[1]
|
42
|
+
row = row_label_to_row_number(input_row)
|
43
|
+
column = column_label_to_column_number(input_column)
|
44
|
+
BattleBoats::Coordinate.new(row: row, column: column)
|
45
|
+
end
|
46
|
+
|
47
|
+
def row_label_to_row_number(row_label)
|
48
|
+
row_labels.index(row_label.upcase)
|
49
|
+
end
|
50
|
+
|
51
|
+
def column_label_to_column_number(column_label)
|
52
|
+
column_label.to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def column_header
|
58
|
+
"| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
def row_labels
|
62
|
+
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
|
63
|
+
end
|
64
|
+
|
65
|
+
def newline
|
66
|
+
"\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
def horizontal_line
|
70
|
+
"-" * 67
|
71
|
+
end
|
72
|
+
|
73
|
+
def pipe
|
74
|
+
"|"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/battle_boats/cell.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require_relative "null_ship"
|
2
|
-
require_relative "colorize"
|
3
2
|
|
4
3
|
module BattleBoats
|
5
|
-
using Colorize
|
6
4
|
class Cell
|
7
5
|
attr_accessor :occupant
|
8
6
|
|
@@ -22,14 +20,6 @@ module BattleBoats
|
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
|
-
def to_s
|
26
|
-
if hit?
|
27
|
-
occupant.to_ansi
|
28
|
-
else
|
29
|
-
to_ansi
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
23
|
def status_report
|
34
24
|
occupant_name = occupant.name
|
35
25
|
if occupant.sunk?
|
@@ -42,11 +32,5 @@ module BattleBoats
|
|
42
32
|
def occupied?
|
43
33
|
!occupant.empty?
|
44
34
|
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def to_ansi
|
49
|
-
"~".blue
|
50
|
-
end
|
51
35
|
end
|
52
36
|
end
|
@@ -1,10 +1,14 @@
|
|
1
1
|
require_relative "coordinate"
|
2
|
+
require_relative "board_formatter"
|
2
3
|
|
3
4
|
module BattleBoats
|
4
5
|
class ConsoleUI
|
5
|
-
def initialize(output: $stdout,
|
6
|
+
def initialize(output: $stdout,
|
7
|
+
input: $stdin,
|
8
|
+
board_formatter: BattleBoats::BoardFormatter.new)
|
6
9
|
@output = output
|
7
10
|
@input = input
|
11
|
+
@board_formatter = board_formatter
|
8
12
|
end
|
9
13
|
|
10
14
|
def greet
|
@@ -12,17 +16,36 @@ module BattleBoats
|
|
12
16
|
end
|
13
17
|
|
14
18
|
def display_board(board)
|
15
|
-
output.puts format_board(board)
|
19
|
+
output.puts board_formatter.format_board(board, hide_ships: true)
|
20
|
+
end
|
21
|
+
|
22
|
+
def display_ally_board(board)
|
23
|
+
output.puts board_formatter.format_board(board, hide_ships: false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def display_ship_data(ship:)
|
27
|
+
output.puts "SHIP: #{ship.name} ALIAS: #{ship.symbol}"
|
28
|
+
output.puts "LENGTH: #{ship.length}"
|
16
29
|
end
|
17
30
|
|
18
31
|
def get_coordinate
|
19
32
|
output.puts "Target coordinate: "
|
20
33
|
user_input = input.gets.chomp
|
21
|
-
until
|
34
|
+
until board_formatter.valid_coordinate_input?(user_input)
|
22
35
|
output.puts "Coordinate invalid."
|
23
36
|
user_input = input.gets.chomp
|
24
37
|
end
|
25
|
-
input_to_coordinate(user_input)
|
38
|
+
board_formatter.input_to_coordinate(user_input)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_orientation
|
42
|
+
output.puts "Orientation [hV]:"
|
43
|
+
user_input = input.gets.chomp
|
44
|
+
until valid_orientation_input?(user_input)
|
45
|
+
output.puts "Orientation invalid."
|
46
|
+
user_input = input.gets.chomp
|
47
|
+
end
|
48
|
+
input_to_orientation(user_input)
|
26
49
|
end
|
27
50
|
|
28
51
|
def display_status_report(status_report)
|
@@ -35,59 +58,18 @@ module BattleBoats
|
|
35
58
|
|
36
59
|
private
|
37
60
|
|
38
|
-
attr_reader :output, :input
|
39
|
-
|
40
|
-
def valid_input?(coordinate)
|
41
|
-
coordinate =~ /^[A-J][0-9]$/i
|
42
|
-
end
|
61
|
+
attr_reader :output, :input, :board_formatter
|
43
62
|
|
44
|
-
def
|
45
|
-
|
46
|
-
input_column = input[1]
|
47
|
-
row = row_labels.index(input_row.upcase)
|
48
|
-
column = input_column.to_i
|
49
|
-
BattleBoats::Coordinate.new(row: row, column: column)
|
63
|
+
def valid_orientation_input?(orientation)
|
64
|
+
orientation =~ /^[h{1}|v{1}]$/i
|
50
65
|
end
|
51
66
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
board_string << newline
|
58
|
-
board.play_area.each_with_index do |row, row_number|
|
59
|
-
board_string << pipe
|
60
|
-
board_string << " #{row_labels[row_number]} "
|
61
|
-
board_string << pipe
|
62
|
-
row.each do |cell|
|
63
|
-
board_string << " #{cell} "
|
64
|
-
board_string << pipe
|
65
|
-
end
|
66
|
-
board_string << newline
|
67
|
-
board_string << horizontal_line
|
68
|
-
board_string << newline
|
67
|
+
def input_to_orientation(input)
|
68
|
+
if input =~ /^[h{1}]$/i
|
69
|
+
:horizontal
|
70
|
+
elsif input =~ /^[v{1}]$/i
|
71
|
+
:vertical
|
69
72
|
end
|
70
|
-
board_string
|
71
|
-
end
|
72
|
-
|
73
|
-
def column_label
|
74
|
-
"| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n"
|
75
|
-
end
|
76
|
-
|
77
|
-
def row_labels
|
78
|
-
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
|
79
|
-
end
|
80
|
-
|
81
|
-
def newline
|
82
|
-
"\n"
|
83
|
-
end
|
84
|
-
|
85
|
-
def horizontal_line
|
86
|
-
"-" * 67
|
87
|
-
end
|
88
|
-
|
89
|
-
def pipe
|
90
|
-
"|"
|
91
73
|
end
|
92
74
|
end
|
93
75
|
end
|
@@ -1,7 +1,4 @@
|
|
1
|
-
require_relative "colorize"
|
2
|
-
|
3
1
|
module BattleBoats
|
4
|
-
using Colorize
|
5
2
|
class NullShip
|
6
3
|
attr_reader :name, :length, :symbol
|
7
4
|
|
@@ -15,10 +12,6 @@ module BattleBoats
|
|
15
12
|
true
|
16
13
|
end
|
17
14
|
|
18
|
-
def to_ansi
|
19
|
-
@symbol.yellow
|
20
|
-
end
|
21
|
-
|
22
15
|
def hit; end
|
23
16
|
|
24
17
|
def sunk?; end
|
data/lib/battle_boats/ship.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
require_relative "colorize"
|
2
|
-
|
3
1
|
module BattleBoats
|
4
|
-
using Colorize
|
5
2
|
class Ship
|
6
3
|
attr_reader :name, :length, :symbol
|
7
4
|
|
@@ -16,10 +13,6 @@ module BattleBoats
|
|
16
13
|
false
|
17
14
|
end
|
18
15
|
|
19
|
-
def to_ansi
|
20
|
-
@symbol.red
|
21
|
-
end
|
22
|
-
|
23
16
|
def hit
|
24
17
|
@hits += 1
|
25
18
|
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.20
|
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-
|
11
|
+
date: 2018-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,11 +107,11 @@ files:
|
|
107
107
|
- bin/setup
|
108
108
|
- lib/battle_boats.rb
|
109
109
|
- lib/battle_boats/board.rb
|
110
|
+
- lib/battle_boats/board_formatter.rb
|
110
111
|
- lib/battle_boats/cell.rb
|
111
112
|
- lib/battle_boats/colorize.rb
|
112
113
|
- lib/battle_boats/console_ui.rb
|
113
114
|
- lib/battle_boats/coordinate.rb
|
114
|
-
- lib/battle_boats/dev_console_ui.rb
|
115
115
|
- lib/battle_boats/engine.rb
|
116
116
|
- lib/battle_boats/fleet.rb
|
117
117
|
- lib/battle_boats/null_ship.rb
|
@@ -1,35 +0,0 @@
|
|
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
|