eat_the_ocean 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ship.rb ADDED
@@ -0,0 +1,126 @@
1
+ module EatTheOcean
2
+ class Ship
3
+ attr_accessor :coordinates, :hits, :size, :sunk, :board_size, :other_ship_array
4
+
5
+ def initialize(other_ship_array = [], board_size = 4)
6
+ @other_ship_array = other_ship_array
7
+ @coordinates = []
8
+ @size = @coordinates.length
9
+ @sunk = 0
10
+ @hits = 0
11
+ @board_size = board_size
12
+ @sample_array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"][0..(@board_size - 1)]
13
+ end
14
+
15
+ def random_1x2
16
+ @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s
17
+ while @coordinates[0] == ((@sample_array[-1] + (@board_size - 1).to_s) || (@sample_array[-2] + (@board_size).to_s) || (@sample_array[-1] + @board_size.to_s))
18
+ @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s
19
+ end
20
+
21
+ if self.on_bottom_edge?(@coordinates[0],2)
22
+ @coordinates [1] = @coordinates[0][0] + @coordinates[0][1].next
23
+ else
24
+ @coordinates [1] = @coordinates[0][0].next + @coordinates[0][1]
25
+ end
26
+ @coordinates
27
+ end
28
+
29
+ def random_1xSize(size)
30
+ @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s
31
+ while @other_ship_array.include?(@coordinates[0]) || in_corner?(@coordinates[0], size)
32
+ @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s
33
+ end
34
+
35
+ if on_bottom_edge?(@coordinates[0], size)
36
+ 0.upto(size-2) do |x|
37
+ @coordinates << linear_placer(@coordinates[x], "h")
38
+ end
39
+ elsif on_right_edge?(@coordinates[0], size)
40
+ 0.upto(size-2) do |x|
41
+ @coordinates << linear_placer(@coordinates[x], "v")
42
+ end
43
+ else
44
+ if rand > 0.5
45
+ 0.upto(size-2) do |x|
46
+ @coordinates << linear_placer(@coordinates[x], "h")
47
+ end
48
+ else
49
+ 0.upto(size-2) do |x|
50
+ @coordinates << linear_placer(@coordinates[x], "v")
51
+ end
52
+ end
53
+ end
54
+
55
+ if blocked?(@coordinates)
56
+ self.random_1xSize(size)
57
+ end
58
+
59
+ @coordinates
60
+ end
61
+
62
+ def linear_placer(coordinate, vert_hor)
63
+ new_coordinate = nil
64
+ if vert_hor == "v"
65
+ new_coordinate = coordinate[0].next + coordinate[1]
66
+ else
67
+ new_coordinate = coordinate[0] + coordinate[1].next
68
+ end
69
+ new_coordinate
70
+ end
71
+
72
+ def in_corner?(coordinate, ship_size)
73
+ the_corner = []
74
+ the_corner << (@sample_array[ - (ship_size-1)] + (@board_size - (ship_size - 2)).to_s)
75
+
76
+ 0.upto(ship_size - 3) do |x|
77
+ the_corner << linear_placer(the_corner[x], "v")
78
+ end
79
+
80
+ #it makes 1 more coordinate than necessary
81
+ array_length = the_corner.length
82
+ if array_length == 2
83
+ 0.upto(1) do |x|
84
+ the_corner << linear_placer(the_corner[x], "h")
85
+ end
86
+ else
87
+ 0.upto(array_length * (array_length - 1)) do |x|
88
+ the_corner << linear_placer(the_corner[x], "h")
89
+ end
90
+ end
91
+ return the_corner.include?(coordinate)
92
+ end
93
+
94
+ def on_bottom_edge?(coordinate, ship_size)
95
+ @sample_array[-(ship_size - 1)..-1].include?(coordinate[0])
96
+ end
97
+
98
+ def on_right_edge?(coordinate, ship_size)
99
+ ((@board_size - ship_size + 2)..@board_size).to_a.include?(coordinate[1].to_i)
100
+ end
101
+
102
+ def straight?(array)
103
+ array.sort!
104
+ straight_count = 0
105
+ 1.upto(array.length-1) do |x|
106
+ if array[x-1][0].next + array[x-1][1] == array[x]
107
+ straight_count += 1
108
+ elsif array[x-1][0] + array[x-1][1].next == array[x]
109
+ straight_count += 1
110
+ end
111
+ end
112
+ true if straight_count == array.length - 1
113
+ end
114
+
115
+ def blocked?(array)
116
+ blocked = false
117
+ array.each do |coordinate|
118
+ if @other_ship_array.include?(coordinate)
119
+ blocked = true
120
+ break
121
+ end
122
+ end
123
+ blocked
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,53 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require './bin/battleship'
4
+ require './lib/ship'
5
+ require './lib/evaluator'
6
+ require './lib/printer'
7
+ require './lib/map'
8
+ module EatTheOcean
9
+ class BattleshipTest < MiniTest::Test
10
+ def setup
11
+ @battleship = EatTheOcean::Battleship.new
12
+ end
13
+
14
+ def test_it_exists
15
+ assert @battleship
16
+ end
17
+
18
+ # def test_it_marks_initial_user_position_on_map
19
+ # @battleship.mark_initial_ship_position_on_map
20
+ # puts @battleship.user_map.grid_array
21
+ # puts @battleship.opponent_map.grid_array
22
+ # end
23
+
24
+ def test_it_validates_user_inputs
25
+ assert_equal "C2", @battleship.validate_input("C2")
26
+ # assert_equal Printer.invalid_input, @battleship.validate_input("CC")
27
+ end
28
+
29
+ # def test_it_registers_and_shows_successful_guesses
30
+ # @battleship.mark_initial_ship_position_on_map
31
+ # @battleship.opponent_ship_1x2.coordinates = ["A1", "A2"]
32
+ # @battleship.opponent_ship_1x3.coordinates = ["B1", "B2", "B3"]
33
+ # @battleship.guess("A1",@battleship.user_evaluator)
34
+ # @battleship.guess("B4",@battleship.user_evaluator)
35
+ # end
36
+
37
+ # def test_it_loops_for_repeat_guesses
38
+ # @battleship.user_evaluator.guess_record = ["A1","A2"]
39
+ # @battleship.user_guess
40
+ # end
41
+
42
+ def test_it_outputs_the_user_sunk_status
43
+ @battleship.mark_initial_ship_position_on_map
44
+ @battleship.opponent_ship_1x2.coordinates = ["A1", "A2"]
45
+ @battleship.opponent_ship_1x3.coordinates = ["B1", "B2", "B3"]
46
+ @battleship.guess("A1",@battleship.user_evaluator)
47
+ @battleship.guess("A2",@battleship.user_evaluator)
48
+ @battleship.guess("B1",@battleship.user_evaluator)
49
+ @battleship.guess("B2",@battleship.user_evaluator)
50
+ @battleship.guess("B3",@battleship.user_evaluator)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,112 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require './lib/ship'
4
+ require './lib/evaluator'
5
+ require './lib/printer'
6
+ require './lib/map'
7
+ module EatTheOcean
8
+ class EvaluatorTest < MiniTest::Test
9
+ def setup
10
+ @ship_1x2 = EatTheOcean::Ship.new(nil, 4)
11
+ @ship_1x2.coordinates = ["A1", "A2"]
12
+ @ship_1x3 = EatTheOcean::Ship.new(@ship_1x2.coordinates, 4)
13
+ @evaluator = EatTheOcean::Evaluator.new(@ship_1x2)
14
+ @map = EatTheOcean::Map.new(4)
15
+ @evaluator.map = @map
16
+ end
17
+
18
+ def test_it_exists
19
+ assert @evaluator
20
+ end
21
+
22
+ def test_it_registers_a_hit
23
+ @evaluator.hit("A1")
24
+ assert_equal 1, @ship_1x2.hits
25
+ @evaluator.hit("A3")
26
+ assert_equal 1, @ship_1x2.hits
27
+ end
28
+
29
+ def test_it_returns_true_on_hit
30
+ assert @evaluator.hit("A1")
31
+ refute @evaluator.hit("A4")
32
+ end
33
+
34
+ def test_it_hits_on_a_hit
35
+ @evaluator.hit("A1")
36
+ @evaluator.hit("A2")
37
+ assert_equal 2, @ship_1x2.hits
38
+ end
39
+
40
+ def test_it_sinks_a_1x2
41
+ @evaluator.hit("A1")
42
+ @evaluator.hit("A2")
43
+ assert_equal 1, @ship_1x2.sunk
44
+ end
45
+
46
+ def test_it_sinks_a_1x3
47
+ @ship_1x3.random_1xSize(3)
48
+ @evaluator.ship_array[1] = @ship_1x3
49
+ @ship_1x3.coordinates[0] = "B1"
50
+ @ship_1x3.coordinates[1] = "B2"
51
+ @ship_1x3.coordinates[2] = "B3"
52
+ @evaluator.hit("B1")
53
+ @evaluator.hit("B2")
54
+ @evaluator.hit("B3")
55
+ assert_equal 1, @ship_1x3.sunk
56
+ end
57
+
58
+ def test_it_ouputs_the_hit_entries
59
+ @evaluator.hit("A1")
60
+ @evaluator.hit("A2")
61
+ assert_equal ["A1", "A2"], @evaluator.hits_record
62
+ end
63
+
64
+ def test_it_ouputs_the_miss_entries
65
+ miss_evaluator = EatTheOcean::Evaluator.new(Ship.new)
66
+ map = Map.new(4)
67
+ miss_evaluator.map = map
68
+ miss_evaluator.hit("A3")
69
+ miss_evaluator.hit("A4")
70
+ assert_equal ["A3", "A4"], miss_evaluator.misses_record
71
+ end
72
+
73
+ def test_it_shows_hit_coordinates
74
+ @ship_1x3.random_1xSize(3)
75
+ @evaluator.ship_array[1] = @ship_1x3
76
+ @ship_1x3.coordinates = ["B1", "B2", "B3"]
77
+ @ship_1x2.coordinates.each do |coordinate|
78
+ @map.grid_mark(coordinate, "🐬")
79
+ end
80
+ @ship_1x3.coordinates.each do |coordinate|
81
+ @map.grid_mark(coordinate, "🐋")
82
+ end
83
+ @evaluator.hit("A1")
84
+ @evaluator.hit("B2")
85
+ @evaluator.hit("B3")
86
+ @evaluator.hit("B4")
87
+ @evaluator.hit("D1")
88
+ @evaluator.hit("D2")
89
+ puts @map.grid_array
90
+
91
+ end
92
+
93
+ def test_it_sinks_both_ships
94
+ @ship_1x2.random_1x2
95
+ @ship_1x3.random_1xSize(3)
96
+ @evaluator.ship_array[0] = @ship_1x2
97
+ @evaluator.ship_array[1] = @ship_1x3
98
+ @ship_1x2.coordinates[0] = "A1"
99
+ @ship_1x2.coordinates[1] = "A2"
100
+ @ship_1x3.coordinates[0] = "B1"
101
+ @ship_1x3.coordinates[1] = "B2"
102
+ @ship_1x3.coordinates[2] = "B3"
103
+ @evaluator.hit("A1")
104
+ @evaluator.hit("A2")
105
+ @evaluator.hit("B1")
106
+ @evaluator.hit("B2")
107
+ @evaluator.hit("B3")
108
+ assert_equal 1, @ship_1x3.sunk
109
+ assert_equal 1, @ship_1x2.sunk
110
+ end
111
+ end
112
+ end
data/test/map_test.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require './lib/ship'
4
+ require './lib/evaluator'
5
+ require './lib/printer'
6
+ require './lib/map'
7
+ module EatTheOcean
8
+ class MapTest < MiniTest::Test
9
+ def setup
10
+ @map = EatTheOcean::Map.new(12)
11
+ end
12
+
13
+ # def test_it_exists
14
+ # assert @map
15
+ # end
16
+
17
+ def test_it_generates_borders_of_varying_size
18
+ @map.size = 16
19
+ puts @map.border_create
20
+ end
21
+
22
+ # def test_it_generates_number_line
23
+ # puts @map.grid_create
24
+ # end
25
+
26
+ # def test_it_generates_the_grid
27
+ # @map.grid_create
28
+ # @map.border_create
29
+ # puts @map.grid_array
30
+ # end
31
+
32
+ def test_it_places_marks
33
+ @map.grid_create
34
+ @map.border_create
35
+ @map.grid_mark("A1", "🐋")
36
+ @map.grid_mark("A2", "🐋")
37
+ @map.grid_mark("A3", "🍣")
38
+ @map.grid_mark("B1", "🐬")
39
+ @map.grid_mark("B2", "🐬")
40
+ @map.grid_mark("D1", "🐬")
41
+ @map.grid_mark("D2", "🍣")
42
+ @map.grid_mark("D3", "💦")
43
+ puts @map.grid_array
44
+ end
45
+ end
46
+ end
data/test/ship_test.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require './lib/ship'
4
+ require './lib/evaluator'
5
+ require './lib/printer'
6
+ require './lib/map'
7
+ module EatTheOcean
8
+ class ShipTest < MiniTest::Test
9
+ def setup
10
+ @ship_1x2 = EatTheOcean::Ship.new
11
+ @ship_1x2.coordinates = ["A1", "A2"]
12
+ @ship_1x3 = EatTheOcean::Ship.new(@ship_1x2.coordinates)
13
+ @ship_1x3.random_1xSize(3)
14
+ end
15
+
16
+ def test_it_generates_a_predictable_1x2_ship
17
+ assert_equal ["A1", "A2"], @ship_1x2.coordinates
18
+ end
19
+
20
+ def test_it_generates_a_random_1x2_ship
21
+ random_1x2 = EatTheOcean::Ship.new(nil, 10)
22
+ puts random_1x2.random_1x2
23
+ end
24
+
25
+ def test_it_straights
26
+ assert @ship_1x2.straight?(["A1","A2"])
27
+ assert @ship_1x2.straight?(["A1","B1"])
28
+ refute @ship_1x2.straight?(["A1","B2"])
29
+ end
30
+
31
+ # def test_it_blocks
32
+ # refute @ship_1x3.blocked?(["B2","B3","B4"])
33
+ # assert @ship_1x3.blocked?(["A1","A2","A3"])
34
+ # assert @ship_1x3.blocked?(["A2","B2","C2"])
35
+ # end
36
+
37
+ def test_it_places_linearly_or_vertically
38
+ assert_equal "B2", @ship_1x3.linear_placer("A2","v")
39
+ assert_equal "A3", @ship_1x3.linear_placer("A2","h")
40
+ end
41
+
42
+ def test_is_the_given_ship_cornered
43
+ assert @ship_1x3.in_corner?("C3", 3)
44
+ refute @ship_1x3.in_corner?("C2", 3)
45
+ assert @ship_1x3.in_corner?("C4", 3)
46
+ assert @ship_1x3.in_corner?("D4", 3)
47
+ another_ship = EatTheOcean::Ship.new(nil, 5)
48
+ assert another_ship.in_corner?("E3", 4)
49
+ assert another_ship.in_corner?("C3", 4)
50
+ refute another_ship.in_corner?("C2", 4)
51
+ assert another_ship.in_corner?("D4", 4)
52
+ assert another_ship.in_corner?("E5", 4)
53
+ end
54
+
55
+ def test_it_knows_bottom_and_top_edges
56
+ new_ship = EatTheOcean::Ship.new(nil, 4)
57
+ assert new_ship.on_bottom_edge?("C2", 3)
58
+ refute new_ship.on_bottom_edge?("B2", 3)
59
+ assert new_ship.on_right_edge?("C3",3)
60
+ refute new_ship.on_right_edge?("C2",3)
61
+ new_ship_2 = EatTheOcean::Ship.new(nil, 6)
62
+ assert new_ship_2.on_bottom_edge?("C2", 5)
63
+ refute new_ship_2.on_bottom_edge?("B2", 5)
64
+ assert new_ship_2.on_right_edge?("C3", 5)
65
+ refute new_ship_2.on_right_edge?("C2", 5)
66
+ end
67
+
68
+ def test_it_generates_random_coordinate_according_to_size
69
+ @ship_1x2.coordinates = ["A1", "A2"]
70
+ new_ship = EatTheOcean::Ship.new(@ship_1x2.coordinates, 4)
71
+ puts new_ship.random_1xSize(3)
72
+ end
73
+
74
+ # def test_it_generates_a_1x4_on_a_5x5_grid
75
+ # ship_1x2 = Ship.new(nil, 9)
76
+ # ship_1x2.random_1x2
77
+ # ship_1x3 = Ship.new(ship_1x2.coordinates, 9)
78
+ # ship_1x3.random_1xSize(3)
79
+ # ship_1x4 = Ship.new(ship_1x2.coordinates + ship_1x3.coordinates, 9)
80
+ # ship_1x4.random_1xSize(4)
81
+
82
+ # puts ship_1x2.coordinates + ship_1x3.coordinates + ship_1x4.coordinates
83
+ # end
84
+
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eat_the_ocean
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey Gu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A marine animal-skinned version of battleship. You can initialize 4x4,
28
+ 6x6, and 8x8 gameboards.
29
+ email: jeffwgu@gmail.com
30
+ executables:
31
+ - eat_the_ocean
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - bin/battleship.rb
36
+ - bin/eat_the_ocean
37
+ - lib/battleship_hardmedium.rb
38
+ - lib/evaluator.rb
39
+ - lib/map.rb
40
+ - lib/printer.rb
41
+ - lib/ship.rb
42
+ - test/battleship_test.rb
43
+ - test/evaluator_test.rb
44
+ - test/map_test.rb
45
+ - test/ship_test.rb
46
+ - LICENSE
47
+ - README
48
+ homepage: http://www.jeffreygu.com
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.14
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A marine animal-skinned version of battleship.
72
+ test_files:
73
+ - test/battleship_test.rb
74
+ - test/evaluator_test.rb
75
+ - test/map_test.rb
76
+ - test/ship_test.rb