class Computer
Attributes
already_shot_positions[RW]
positions_of_ships[RW]
Public Class Methods
new()
click to toggle source
# File lib/nicktroyanov_riverbattle/computer.rb, line 5 def initialize @positions_of_ships = [] @already_shot_positions = [] end
Public Instance Methods
congratulate()
click to toggle source
# File lib/nicktroyanov_riverbattle/computer.rb, line 21 def congratulate puts "What a shame! You managed to loose to a computer!!" puts "If you want to play again type Game.new.play" end
make_turn(oponent_field, field)
click to toggle source
# File lib/nicktroyanov_riverbattle/computer.rb, line 15 def make_turn oponent_field, field shoot_position_index = ((0..9).to_a - already_shot_positions).sample oponent_field[shoot_position_index] += 2 already_shot_positions << shoot_position_index end
place_ships(field, number_of_ships = 5)
click to toggle source
# File lib/nicktroyanov_riverbattle/computer.rb, line 10 def place_ships field, number_of_ships = 5 positions_of_ships = (0..9).to_a.sample(number_of_ships) positions_of_ships.each { |p| field[p] = 1 } end