devlin_battleships 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/boat.rb +26 -0
- data/lib/boat_list.rb +38 -0
- data/lib/devlin_battleships.rb +60 -0
- data/lib/grid.rb +24 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d40213faaffab7151b093bd46ed521bb490a28490fb87831f535108c50b95005
|
4
|
+
data.tar.gz: 7f9ce26be6bc260a4072b7b6f2f63363953539186c374626ea7b8b7e42155418
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3fa62404ef596693165dac469fcc90aa9f02e2bbafd4f4fe364b9378be682493c6110123d6dbffbe6d7ada139409621cf3c5258b77ebd717bbf808cf3d9d6742
|
7
|
+
data.tar.gz: 2f3613dfd9a356089574c0fcbed858b52351e28c7a5c529057bc7110bd4819a56c207d8627d7ef113b3fcc5b2cb48087e22faeba612348fddb6c7d510aecdfda
|
data/lib/boat.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Boat
|
2
|
+
|
3
|
+
attr_reader :set_of_grid_points
|
4
|
+
|
5
|
+
def initialize(set_of_grid_points)
|
6
|
+
@set_of_grid_points = Hash.new
|
7
|
+
set_of_grid_points.each do |grid_point|
|
8
|
+
@set_of_grid_points[grid_point] = false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def any_point_matched?(target_grid_point)
|
13
|
+
@set_of_grid_points.each_key do |grid_point|
|
14
|
+
return true if grid_point == target_grid_point
|
15
|
+
end
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
def record_hit(target_grid_point)
|
20
|
+
@set_of_grid_points[target_grid_point] = true
|
21
|
+
end
|
22
|
+
|
23
|
+
def sunk?
|
24
|
+
@set_of_grid_points.values.all?
|
25
|
+
end
|
26
|
+
end
|
data/lib/boat_list.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class BoatList
|
2
|
+
|
3
|
+
def initialize(boat_list)
|
4
|
+
@boat_list = boat_list
|
5
|
+
end
|
6
|
+
|
7
|
+
def any_boat_hit?(target_grid_point)
|
8
|
+
@boat_list.each do |boat|
|
9
|
+
return true if boat.any_point_matched?(target_grid_point)
|
10
|
+
end
|
11
|
+
false
|
12
|
+
end
|
13
|
+
|
14
|
+
def record_boat_hit(target_grid_point)
|
15
|
+
@boat_list.each do |boat|
|
16
|
+
if boat.any_point_matched?(target_grid_point)
|
17
|
+
boat.record_hit(target_grid_point)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def boat_sunk?(target_grid_point)
|
23
|
+
@boat_list.each do |boat|
|
24
|
+
if boat.any_point_matched?(target_grid_point)
|
25
|
+
return true if boat.sunk?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def count_boats_not_sunk
|
32
|
+
@boat_list.length - @boat_list.count {|boat| boat.sunk?}
|
33
|
+
end
|
34
|
+
|
35
|
+
def all_boats_sunk?
|
36
|
+
@boat_list.all? {|boat| boat.sunk?}
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require_relative 'grid.rb'
|
2
|
+
require_relative 'boat.rb'
|
3
|
+
require_relative 'boat_list.rb'
|
4
|
+
|
5
|
+
class BattleshipsGame
|
6
|
+
|
7
|
+
attr_accessor :guesses_left
|
8
|
+
attr_reader :grid, :boat_1
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@guesses_left = 5
|
12
|
+
@grid = Grid.new
|
13
|
+
@boat_1 = Boat.new([2,3])
|
14
|
+
@boat_2 = Boat.new([16,17])
|
15
|
+
@boat_list = BoatList.new([@boat_1,@boat_2])
|
16
|
+
end
|
17
|
+
|
18
|
+
def convert_to_grid_point(user_input)
|
19
|
+
user_input.to_i - 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def input_not_in_correct_format?(user_input)
|
23
|
+
user_input.to_i < 1 || user_input.to_i > 25
|
24
|
+
end
|
25
|
+
|
26
|
+
def input_has_been_entered_previously?(grid_point)
|
27
|
+
@grid.input_has_been_entered_previously?(grid_point)
|
28
|
+
end
|
29
|
+
|
30
|
+
def hit?(grid_point)
|
31
|
+
@boat_list.any_boat_hit?(grid_point)
|
32
|
+
end
|
33
|
+
|
34
|
+
def hit_mechanics(grid_point)
|
35
|
+
@grid.record_hit(grid_point)
|
36
|
+
@boat_list.record_boat_hit(grid_point)
|
37
|
+
end
|
38
|
+
|
39
|
+
def boat_sunk?(grid_point)
|
40
|
+
@boat_list.boat_sunk?(grid_point)
|
41
|
+
end
|
42
|
+
|
43
|
+
def miss_mechanics(grid_point)
|
44
|
+
@grid.record_miss(grid_point)
|
45
|
+
@guesses_left -= 1
|
46
|
+
end
|
47
|
+
|
48
|
+
def won?
|
49
|
+
@boat_list.all_boats_sunk?
|
50
|
+
end
|
51
|
+
|
52
|
+
def lost?
|
53
|
+
@guesses_left == 0
|
54
|
+
end
|
55
|
+
|
56
|
+
def boats_left?
|
57
|
+
@boat_list.count_boats_not_sunk
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/lib/grid.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Grid
|
2
|
+
|
3
|
+
attr_reader :grid_points
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@grid_points = [" 01"," 02"," 03"," 04"," 05",
|
7
|
+
" 06"," 07"," 08"," 09"," 10",
|
8
|
+
" 11"," 12"," 13"," 14"," 15",
|
9
|
+
" 16"," 17"," 18"," 19"," 20",
|
10
|
+
" 21"," 22"," 23"," 24"," 25"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def input_has_been_entered_previously?(grid_point)
|
14
|
+
@grid_points[grid_point] == "⚓" || @grid_points[grid_point] == "☠"
|
15
|
+
end
|
16
|
+
|
17
|
+
def record_hit(grid_point)
|
18
|
+
@grid_points[grid_point] = "⚓"
|
19
|
+
end
|
20
|
+
|
21
|
+
def record_miss(grid_point)
|
22
|
+
@grid_points[grid_point] = "☠"
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devlin_battleships
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Devlin Glasman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem which provides core functionality for a e-version of the traditional
|
14
|
+
Battleships game. This needs to be wrapped to be usable with a user interface.
|
15
|
+
email: devlin.glasman@outlook.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/boat.rb
|
21
|
+
- lib/boat_list.rb
|
22
|
+
- lib/devlin_battleships.rb
|
23
|
+
- lib/grid.rb
|
24
|
+
homepage: http://rubygems.org/gems/devlin_battleships
|
25
|
+
licenses:
|
26
|
+
- XX
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.7.6
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Battleships game core.
|
48
|
+
test_files: []
|