Class: EatTheOcean::Ship
- Inherits:
-
Object
- Object
- EatTheOcean::Ship
- Defined in:
- lib/eat_the_ocean/ship.rb
Instance Attribute Summary collapse
-
#board_size ⇒ Object
Returns the value of attribute board_size.
-
#coordinates ⇒ Object
Returns the value of attribute coordinates.
-
#hits ⇒ Object
Returns the value of attribute hits.
-
#other_ship_array ⇒ Object
Returns the value of attribute other_ship_array.
-
#size ⇒ Object
Returns the value of attribute size.
-
#sunk ⇒ Object
Returns the value of attribute sunk.
Instance Method Summary collapse
- #blocked?(array) ⇒ Boolean
- #in_corner?(coordinate, ship_size) ⇒ Boolean
-
#initialize(other_ship_array = [], board_size = 4) ⇒ Ship
constructor
A new instance of Ship.
- #linear_placer(coordinate, vert_hor) ⇒ Object
- #on_bottom_edge?(coordinate, ship_size) ⇒ Boolean
- #on_right_edge?(coordinate, ship_size) ⇒ Boolean
- #random_1x2 ⇒ Object
- #random_1xSize(size) ⇒ Object
- #straight?(array) ⇒ Boolean
Constructor Details
#initialize(other_ship_array = [], board_size = 4) ⇒ Ship
Returns a new instance of Ship.
5 6 7 8 9 10 11 12 13 |
# File 'lib/eat_the_ocean/ship.rb', line 5 def initialize(other_ship_array = [], board_size = 4) @other_ship_array = other_ship_array @coordinates = [] @size = @coordinates.length @sunk = 0 @hits = 0 @board_size = board_size @sample_array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"][0..(@board_size - 1)] end |
Instance Attribute Details
#board_size ⇒ Object
Returns the value of attribute board_size.
3 4 5 |
# File 'lib/eat_the_ocean/ship.rb', line 3 def board_size @board_size end |
#coordinates ⇒ Object
Returns the value of attribute coordinates.
3 4 5 |
# File 'lib/eat_the_ocean/ship.rb', line 3 def coordinates @coordinates end |
#hits ⇒ Object
Returns the value of attribute hits.
3 4 5 |
# File 'lib/eat_the_ocean/ship.rb', line 3 def hits @hits end |
#other_ship_array ⇒ Object
Returns the value of attribute other_ship_array.
3 4 5 |
# File 'lib/eat_the_ocean/ship.rb', line 3 def other_ship_array @other_ship_array end |
#size ⇒ Object
Returns the value of attribute size.
3 4 5 |
# File 'lib/eat_the_ocean/ship.rb', line 3 def size @size end |
#sunk ⇒ Object
Returns the value of attribute sunk.
3 4 5 |
# File 'lib/eat_the_ocean/ship.rb', line 3 def sunk @sunk end |
Instance Method Details
#blocked?(array) ⇒ Boolean
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/eat_the_ocean/ship.rb', line 115 def blocked?(array) blocked = false array.each do |coordinate| if @other_ship_array.include?(coordinate) blocked = true break end end blocked end |
#in_corner?(coordinate, ship_size) ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/eat_the_ocean/ship.rb', line 72 def in_corner?(coordinate, ship_size) the_corner = [] the_corner << (@sample_array[ - (ship_size-1)] + (@board_size - (ship_size - 2)).to_s) 0.upto(ship_size - 3) do |x| the_corner << linear_placer(the_corner[x], "v") end #it makes 1 more coordinate than necessary array_length = the_corner.length if array_length == 2 0.upto(1) do |x| the_corner << linear_placer(the_corner[x], "h") end else 0.upto(array_length * (array_length - 1)) do |x| the_corner << linear_placer(the_corner[x], "h") end end return the_corner.include?(coordinate) end |
#linear_placer(coordinate, vert_hor) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/eat_the_ocean/ship.rb', line 62 def linear_placer(coordinate, vert_hor) new_coordinate = nil if vert_hor == "v" new_coordinate = coordinate[0].next + coordinate[1] else new_coordinate = coordinate[0] + coordinate[1].next end new_coordinate end |
#on_bottom_edge?(coordinate, ship_size) ⇒ Boolean
94 95 96 |
# File 'lib/eat_the_ocean/ship.rb', line 94 def on_bottom_edge?(coordinate, ship_size) @sample_array[-(ship_size - 1)..-1].include?(coordinate[0]) end |
#on_right_edge?(coordinate, ship_size) ⇒ Boolean
98 99 100 |
# File 'lib/eat_the_ocean/ship.rb', line 98 def on_right_edge?(coordinate, ship_size) ((@board_size - ship_size + 2)..@board_size).to_a.include?(coordinate[1].to_i) end |
#random_1x2 ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/eat_the_ocean/ship.rb', line 15 def random_1x2 @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s 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)) @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s end if self.on_bottom_edge?(@coordinates[0],2) @coordinates [1] = @coordinates[0][0] + @coordinates[0][1].next else @coordinates [1] = @coordinates[0][0].next + @coordinates[0][1] end @coordinates end |
#random_1xSize(size) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/eat_the_ocean/ship.rb', line 29 def random_1xSize(size) @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s while @other_ship_array.include?(@coordinates[0]) || in_corner?(@coordinates[0], size) @coordinates[0] = @sample_array.sample + rand(1..@board_size).to_s end if on_bottom_edge?(@coordinates[0], size) 0.upto(size-2) do |x| @coordinates << linear_placer(@coordinates[x], "h") end elsif on_right_edge?(@coordinates[0], size) 0.upto(size-2) do |x| @coordinates << linear_placer(@coordinates[x], "v") end else if rand > 0.5 0.upto(size-2) do |x| @coordinates << linear_placer(@coordinates[x], "h") end else 0.upto(size-2) do |x| @coordinates << linear_placer(@coordinates[x], "v") end end end if blocked?(@coordinates) self.random_1xSize(size) end @coordinates end |
#straight?(array) ⇒ Boolean
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/eat_the_ocean/ship.rb', line 102 def straight?(array) array.sort! straight_count = 0 1.upto(array.length-1) do |x| if array[x-1][0].next + array[x-1][1] == array[x] straight_count += 1 elsif array[x-1][0] + array[x-1][1].next == array[x] straight_count += 1 end end true if straight_count == array.length - 1 end |