Hi! I’m having trouble coding the place_random_ship method. Please HELP!
In the spec, it says “place ships until the board is full”
so I wrote:
def place_random_ship
raise “Sorry, there is no space for another ship!” if full?
size = @grid.length
random = rand(0…size-1) @grid[random][random] = :s if empty?
unless full? @grid[random][random] = :s
end
end # my method will fail the entire spec instead of giving me feedback of failures
but in the solution : #Why until empty? instead of until full?
def place_random_ship
raise “hell” if full?
pos = random_pos
until empty?(pos)
pos = random_pos
end
self[pos] = :s # since @grid is a two dimension arr, why are we replacing the entire subarr
end #with :s instead of replacing just one element in the subarr with :s
# the example such as two_ship_grid [[:s,:s], [nil,nil]] has :s as element in the
# subarr instead of the main arr…
def randomize(count = 10)
count.times { place_random_ship }
end
When you scroll to the top, it should tell you why the spec isn’t running. You should see a message such as syntax error, unexpected end-of-input, expecting keyword_end.
Thanks! I found the error message, it says something about ‘raise_error’, however, if i removed the second [random], just like the solution, the error message goes away.
WARNING: Using the raise_error matcher without providing a specific error or message risks false positives, since raise_error will match when Ruby raises a NoMethodError, NameError or ArgumentError, potentially allowing the expectation to pass without even executing the method you are intending to call. Actual error raised was #<RuntimeError: Sorry, there is no space for another ship!>. Instead consider providing a specific error class or message. This message can be suppressed by setting: RSpec::Expectations.configuration.on_potential_false_positives = :nothing. Called from /Users/admin/Desktop/Battleship/spec/board_spec.rb:97:in `block (4 levels) in <top (required)>’.