battleships 0.1.1 → 0.1.2

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: 7d52439b82c6283a6c19fb6e5840d113a973a87a
4
- data.tar.gz: eb5b4b0fc4387d8761fac470f91b2b26698556db
3
+ metadata.gz: e54bb56b0b005da4197bc3f710a557028f4c0ed1
4
+ data.tar.gz: 79c465f53c4dfa22f7015d039a634eb5b6de5c48
5
5
  SHA512:
6
- metadata.gz: 2aab4da6a19a37045c7f8e66d5790ebdfe738574e0c1f38b68a104abbd9985b0746e4988553f088eba2e53cbd76e520c637fd86878f9214c17734999a3ee72a5
7
- data.tar.gz: 354fc770a7337e188fdc92195b4462f8dadfc6a2dceddda2b59d803ebeea37749e1fb3ef4e5a6edc18a83bce4489b0b1c3849af1da8161c4196370e5e4123c70
6
+ metadata.gz: 2ca989c2bfb96e8ce5606ebae42720acd7b440d00c6fff47014d20e50780ec68a6f138a551d55f901b4fe5a4638b7bc0e95fbf4a6e2c0b93e43fa9d394ca3fae
7
+ data.tar.gz: 424686bdfc49d3819ad15023b6396cd132c14cf3cd3a635aa4c06f30c75704f213aff74275b75157eab568bb9f80dc6613b4dd2766acc9ec598254ea9d5914d5
@@ -1,5 +1,3 @@
1
- module Battleships; end
2
-
3
1
  require_relative './battleships/board'
4
2
  require_relative './battleships/ship'
5
3
  require_relative './battleships/player'
@@ -1,101 +1,103 @@
1
1
  require_relative 'cell'
2
2
  require_relative 'coordinate_handler'
3
3
 
4
- class Battleships::Board
5
- SIZE = 10
4
+ module Battleships
5
+ class Board
6
+ SIZE = 10
6
7
 
7
- attr_accessor :width
8
+ attr_accessor :width
8
9
 
9
- def initialize
10
- @grid = {}
11
- @coord_handler = CoordinateHandler.new
12
- @ships = []
13
- initialize_grid
14
- end
10
+ def initialize
11
+ @grid = {}
12
+ @coord_handler = CoordinateHandler.new
13
+ @ships = []
14
+ initialize_grid
15
+ end
15
16
 
16
- def place_ship ship, coordinate, orientation = :horizontally
17
- coords = all_ship_coords ship, coordinate, orientation
17
+ def place_ship ship, coordinate, orientation = :horizontally
18
+ coords = all_ship_coords ship, coordinate, orientation
18
19
 
19
- coords.each { |coord| grid[coord].content = ship }
20
- @ships << ship
21
- end
20
+ coords.each { |coord| grid[coord].content = ship }
21
+ @ships << ship
22
+ end
22
23
 
23
- def width
24
- SIZE
25
- end
24
+ def width
25
+ SIZE
26
+ end
26
27
 
27
- def height
28
- SIZE
29
- end
28
+ def height
29
+ SIZE
30
+ end
30
31
 
31
- def ships
32
- # note we do not pass the source array here as it would enable
33
- # callers to modify the board's ships, which would break encapsulation.
34
- # Instead we return a duplicate.
35
- @ships.dup
36
- end
32
+ def ships
33
+ # note we do not pass the source array here as it would enable
34
+ # callers to modify the board's ships, which would break encapsulation.
35
+ # Instead we return a duplicate.
36
+ @ships.dup
37
+ end
37
38
 
38
- def receive_shot coordinate
39
- coord_handler.validate coordinate
39
+ def receive_shot coordinate
40
+ coord_handler.validate coordinate
40
41
 
41
- validate_coord_not_shot coordinate
42
+ validate_coord_not_shot coordinate
42
43
 
43
- cell = grid[coordinate]
44
- cell.receive_shot
44
+ cell = grid[coordinate]
45
+ cell.receive_shot
45
46
 
46
- if cell.content
47
- cell.content.sunk? ? :sunk : :hit
48
- else
49
- :miss
47
+ if cell.content
48
+ cell.content.sunk? ? :sunk : :hit
49
+ else
50
+ :miss
51
+ end
50
52
  end
51
- end
52
53
 
53
- def [] coordinate
54
- coord_handler.validate coordinate
55
- grid[coordinate]
56
- end
54
+ def [] coordinate
55
+ coord_handler.validate coordinate
56
+ grid[coordinate]
57
+ end
57
58
 
58
- def all_ships_sunk?
59
- return false if ships.empty?
60
- ships.all?(&:sunk?)
61
- end
59
+ def all_ships_sunk?
60
+ return false if ships.empty?
61
+ ships.all?(&:sunk?)
62
+ end
62
63
 
63
- def inspect
64
- to_s
65
- end
64
+ def inspect
65
+ to_s
66
+ end
66
67
 
67
- private
68
+ private
68
69
 
69
- attr_reader :grid, :coord_handler
70
+ attr_reader :grid, :coord_handler
70
71
 
71
- def initialize_grid
72
- coord_handler.each do |coord|
73
- grid[coord] = Cell.new
72
+ def initialize_grid
73
+ coord_handler.each do |coord|
74
+ grid[coord] = Cell.new
75
+ end
74
76
  end
75
- end
76
77
 
77
- def all_ship_coords ship, coord, orientation
78
- coord_handler.validate coord
78
+ def all_ship_coords ship, coord, orientation
79
+ coord_handler.validate coord
79
80
 
80
- all_coords = coord_handler.from coord, ship.size, orientation
81
+ all_coords = coord_handler.from coord, ship.size, orientation
81
82
 
82
- validate_all_ship_coords all_coords, ship.size
83
- end
83
+ validate_all_ship_coords all_coords, ship.size
84
+ end
84
85
 
85
- def validate_all_ship_coords coords, size
86
- #ship is out of bounds if the ship is larger than the available coords
87
- fail 'Out of bounds' if size > coords.length
86
+ def validate_all_ship_coords coords, size
87
+ #ship is out of bounds if the ship is larger than the available coords
88
+ fail 'Out of bounds' if size > coords.length
88
89
 
89
- validate_all_coords_available coords
90
- end
90
+ validate_all_coords_available coords
91
+ end
91
92
 
92
- def validate_all_coords_available coords
93
- coords.each do |coord|
94
- fail 'Coordinate already occupied' unless grid[coord].empty?
93
+ def validate_all_coords_available coords
94
+ coords.each do |coord|
95
+ fail 'Coordinate already occupied' unless grid[coord].empty?
96
+ end
95
97
  end
96
- end
97
98
 
98
- def validate_coord_not_shot coord
99
- fail 'Coordinate has been shot already' if grid[coord].shot?
99
+ def validate_coord_not_shot coord
100
+ fail 'Coordinate has been shot already' if grid[coord].shot?
101
+ end
100
102
  end
101
- end
103
+ end
@@ -1,23 +1,25 @@
1
- class Battleships::Cell
2
- attr_accessor :content
1
+ module Battleships
2
+ class Cell
3
+ attr_accessor :content
3
4
 
4
- def receive_shot
5
- @shot = true
6
- content.hit unless empty?
7
- end
5
+ def receive_shot
6
+ @shot = true
7
+ content.hit unless empty?
8
+ end
8
9
 
9
- def shot?
10
- @shot
11
- end
10
+ def shot?
11
+ @shot
12
+ end
12
13
 
13
- def empty?
14
- !content
15
- end
14
+ def empty?
15
+ !content
16
+ end
16
17
 
17
- def status
18
- if shot?
19
- return empty? ? :miss : :hit
18
+ def status
19
+ if shot?
20
+ return empty? ? :miss : :hit
21
+ end
22
+ :none
20
23
  end
21
- :none
22
24
  end
23
- end
25
+ end
@@ -1,69 +1,71 @@
1
- class Battleships::CoordinateHandler
2
- include Enumerable
1
+ module Battleships
2
+ class CoordinateHandler
3
+ include Enumerable
3
4
 
4
- HORIZONTAL_COORDS = ('A'..'J').to_a
5
- VERTICAL_COORDS = (1..10).to_a
6
- COORD_REGEX = /^([A-J])([1-9]|10)$/
5
+ HORIZONTAL_COORDS = ('A'..'J').to_a
6
+ VERTICAL_COORDS = (1..10).to_a
7
+ COORD_REGEX = /^([A-J])([1-9]|10)$/
7
8
 
8
- def each
9
- VERTICAL_COORDS.each do |y|
10
- HORIZONTAL_COORDS.each do |x|
11
- yield coordinate x, y
9
+ def each
10
+ VERTICAL_COORDS.each do |y|
11
+ HORIZONTAL_COORDS.each do |x|
12
+ yield coordinate x, y
13
+ end
12
14
  end
13
15
  end
14
- end
15
16
 
16
- def each_row
17
- VERTICAL_COORDS.each do |y|
18
- row = HORIZONTAL_COORDS.map do |x|
19
- coordinate x, y
17
+ def each_row
18
+ VERTICAL_COORDS.each do |y|
19
+ row = HORIZONTAL_COORDS.map do |x|
20
+ coordinate x, y
21
+ end
22
+ yield row, y
20
23
  end
21
- yield row, y
22
24
  end
23
- end
24
25
 
25
- def validate coord
26
- fail 'Invalid coordinate' unless valid? coord
27
- end
26
+ def validate coord
27
+ fail 'Invalid coordinate' unless valid? coord
28
+ end
28
29
 
29
- def valid? coord
30
- COORD_REGEX.match coord.to_s
31
- end
30
+ def valid? coord
31
+ COORD_REGEX.match coord.to_s
32
+ end
32
33
 
33
- def from start, size, orientation = :horizontally
34
- match = COORD_REGEX.match start
34
+ def from start, size, orientation = :horizontally
35
+ match = COORD_REGEX.match start
35
36
 
36
- x = match.captures.first
37
- y = match.captures.last
37
+ x = match.captures.first
38
+ y = match.captures.last
38
39
 
39
- if orientation == :horizontally
40
- horizontal_coords_for x, y, size
41
- else
42
- vertical_coords_for x, y, size
40
+ if orientation.to_sym == :horizontally
41
+ horizontal_coords_for x, y, size
42
+ else
43
+ vertical_coords_for x, y, size
44
+ end
43
45
  end
44
- end
45
46
 
46
- private
47
+ private
47
48
 
48
- def horizontal_coords_for x, y, size
49
- x_coords(x, size).map { |x| coordinate x, y }
50
- end
49
+ def horizontal_coords_for x, y, size
50
+ x_coords(x, size).map { |x| coordinate x, y }
51
+ end
51
52
 
52
- def x_coords x, size
53
- start = HORIZONTAL_COORDS.index x
54
- HORIZONTAL_COORDS.slice start, size
55
- end
53
+ def x_coords x, size
54
+ start = HORIZONTAL_COORDS.index x
55
+ HORIZONTAL_COORDS.slice start, size
56
+ end
56
57
 
57
- def vertical_coords_for x, y, size
58
- y_coords(y, size).map { |y| coordinate x, y }
59
- end
58
+ def vertical_coords_for x, y, size
59
+ y_coords(y, size).map { |y| coordinate x, y }
60
+ end
60
61
 
61
- def y_coords y, size
62
- start = VERTICAL_COORDS.index Integer(y)
63
- VERTICAL_COORDS.slice start, size
64
- end
62
+ def y_coords y, size
63
+ start = VERTICAL_COORDS.index Integer(y)
64
+ VERTICAL_COORDS.slice start, size
65
+ end
65
66
 
66
- def coordinate x, y
67
- "#{x}#{y}".to_sym
67
+ def coordinate x, y
68
+ "#{x}#{y}".to_sym
69
+ end
68
70
  end
69
- end
71
+ end
@@ -1,11 +1,12 @@
1
- class Battleships::Game
2
- BOARD_MARKERS = {
3
- miss: '-',
4
- hit: '*',
5
- none: ' '
6
- }.freeze
7
-
8
- BOARD_TEMPLATE = <<TEMPLATE
1
+ module Battleships
2
+ class Game
3
+ BOARD_MARKERS = {
4
+ miss: '-',
5
+ hit: '*',
6
+ none: ' '
7
+ }.freeze
8
+
9
+ BOARD_TEMPLATE = <<TEMPLATE
9
10
  ABCDEFGHIJ
10
11
  ------------
11
12
  1|<1>|1
@@ -21,69 +22,70 @@ class Battleships::Game
21
22
  ------------
22
23
  ABCDEFGHIJ
23
24
  TEMPLATE
24
-
25
- BOARD_TEMPLATE.freeze
25
+
26
+ BOARD_TEMPLATE.freeze
26
27
 
27
- attr_reader :player_1, :player_2
28
+ attr_reader :player_1, :player_2
28
29
 
29
- def initialize(playerClass, boardClass)
30
- @player_1 = initialize_player playerClass, boardClass
31
- @player_2 = initialize_player playerClass, boardClass
30
+ def initialize(playerClass, boardClass)
31
+ @player_1 = initialize_player playerClass, boardClass
32
+ @player_2 = initialize_player playerClass, boardClass
32
33
 
33
34
 
34
- player_1.opponent = player_2
35
- player_2.opponent = player_1
36
- end
35
+ player_1.opponent = player_2
36
+ player_2.opponent = player_1
37
+ end
37
38
 
38
- def initialize_player(playerClass, boardClass)
39
- player = playerClass.new
40
- player.board = boardClass.new
41
- player
42
- end
39
+ def initialize_player(playerClass, boardClass)
40
+ player = playerClass.new
41
+ player.board = boardClass.new
42
+ player
43
+ end
43
44
 
44
- def has_winner?
45
- players.any?(&:winner?)
46
- end
45
+ def has_winner?
46
+ players.any?(&:winner?)
47
+ end
47
48
 
48
- def winner
49
- players.find(&:winner?)
50
- end
49
+ def winner
50
+ players.find(&:winner?)
51
+ end
51
52
 
52
- def own_board_view player
53
- create_print player.board do |cell|
54
- if cell.empty?
55
- BOARD_MARKERS[cell.status]
56
- else
57
- cell.shot? ? BOARD_MARKERS[:hit] : cell.content.type.to_s.upcase[0]
53
+ def own_board_view player
54
+ create_print player.board do |cell|
55
+ if cell.empty?
56
+ BOARD_MARKERS[cell.status]
57
+ else
58
+ cell.shot? ? BOARD_MARKERS[:hit] : cell.content.type.to_s.upcase[0]
59
+ end
58
60
  end
59
61
  end
60
- end
61
62
 
62
- def opponent_board_view player
63
- create_print player.opponent.board do |cell|
64
- BOARD_MARKERS[cell.status]
63
+ def opponent_board_view player
64
+ create_print player.opponent.board do |cell|
65
+ BOARD_MARKERS[cell.status]
66
+ end
65
67
  end
66
- end
67
68
 
68
- private
69
+ private
69
70
 
70
- def players
71
- [player_1, player_2]
72
- end
71
+ def players
72
+ [player_1, player_2]
73
+ end
73
74
 
74
75
 
75
- def create_print board
76
- coord_handler = CoordinateHandler.new
76
+ def create_print board
77
+ coord_handler = CoordinateHandler.new
77
78
 
78
- output = BOARD_TEMPLATE
79
+ output = BOARD_TEMPLATE
79
80
 
80
- coord_handler.each_row do |row, number|
81
- print_row = row.map do |coord|
82
- yield board[coord]
83
- end.join('')
84
-
85
- output = output.sub("<#{number}>", print_row)
81
+ coord_handler.each_row do |row, number|
82
+ print_row = row.map do |coord|
83
+ yield board[coord]
84
+ end.join('')
85
+
86
+ output = output.sub("<#{number}>", print_row)
87
+ end
88
+ output
86
89
  end
87
- output
88
90
  end
89
- end
91
+ end
@@ -1,27 +1,29 @@
1
- class Battleships::Player
2
- attr_accessor :board, :opponent, :name
1
+ module Battleships
2
+ class Player
3
+ attr_accessor :board, :opponent, :name
3
4
 
4
- def place_ship ship, coordinates, orientation = :horizontally
5
- board.place_ship ship, coordinates, orientation
6
- end
5
+ def place_ship ship, coordinates, orientation = :horizontally
6
+ board.place_ship ship, coordinates, orientation
7
+ end
7
8
 
8
- def receive_shot coordinates
9
- fail 'Player has no board' unless board
10
- board.receive_shot coordinates
11
- end
9
+ def receive_shot coordinates
10
+ fail 'Player has no board' unless board
11
+ board.receive_shot coordinates
12
+ end
12
13
 
13
- def shoot coordinates
14
- fail 'Player has no opponent' unless opponent
15
- opponent.receive_shot coordinates
16
- end
14
+ def shoot coordinates
15
+ fail 'Player has no opponent' unless opponent
16
+ opponent.receive_shot coordinates
17
+ end
17
18
 
18
- def winner?
19
- fail 'Player has no opponent' unless opponent
20
- opponent.all_ships_sunk?
21
- end
19
+ def winner?
20
+ fail 'Player has no opponent' unless opponent
21
+ opponent.all_ships_sunk?
22
+ end
22
23
 
23
- def all_ships_sunk?
24
- fail 'Player has no board' unless board
25
- board.all_ships_sunk?
24
+ def all_ships_sunk?
25
+ fail 'Player has no board' unless board
26
+ board.all_ships_sunk?
27
+ end
26
28
  end
27
- end
29
+ end
@@ -1,45 +1,47 @@
1
- class Battleships::Ship
2
- SIZES = {
3
- submarine: 1,
4
- destroyer: 2,
5
- cruiser: 3,
6
- battleship: 4,
7
- aircraft_carrier: 5
8
- }
9
-
10
- attr_reader :type, :size
11
-
12
- def initialize type
13
- @type = type
14
- @size = SIZES[type]
15
- @hits = 0
16
- end
17
-
18
- def hit
19
- @hits += 1
20
- end
21
-
22
- def sunk?
23
- @hits >= size
24
- end
25
-
26
- def self.submarine
27
- Ship.new(:submarine)
28
- end
29
-
30
- def self.destroyer
31
- Ship.new(:destroyer)
32
- end
33
-
34
- def self.cruiser
35
- Ship.new(:cruiser)
36
- end
37
-
38
- def self.battleship
39
- Ship.new(:battleship)
40
- end
41
-
42
- def self.aircraft_carrier
43
- Ship.new(:aircraft_carrier)
44
- end
45
- end
1
+ module Battleships
2
+ class Ship
3
+ SIZES = {
4
+ submarine: 1,
5
+ destroyer: 2,
6
+ cruiser: 3,
7
+ battleship: 4,
8
+ aircraft_carrier: 5
9
+ }
10
+
11
+ attr_reader :type, :size
12
+
13
+ def initialize type
14
+ @type = type.to_sym
15
+ @size = SIZES[@type]
16
+ @hits = 0
17
+ end
18
+
19
+ def hit
20
+ @hits += 1
21
+ end
22
+
23
+ def sunk?
24
+ @hits >= size
25
+ end
26
+
27
+ def self.submarine
28
+ Ship.new(:submarine)
29
+ end
30
+
31
+ def self.destroyer
32
+ Ship.new(:destroyer)
33
+ end
34
+
35
+ def self.cruiser
36
+ Ship.new(:cruiser)
37
+ end
38
+
39
+ def self.battleship
40
+ Ship.new(:battleship)
41
+ end
42
+
43
+ def self.aircraft_carrier
44
+ Ship.new(:aircraft_carrier)
45
+ end
46
+ end
47
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battleships
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Forrest