class EatTheOcean::MediumBattleship
Attributes
first_time[RW]
opponent_map[R]
opponent_ship_1x2[RW]
opponent_ship_1x3[RW]
user_evaluator[R]
user_map[R]
Public Class Methods
new()
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 8 def initialize @user_ship_1x2 = Ship.new([], 6) @user_ship_1x3 = Ship.new([], 6) @user_ship_1x4 = Ship.new([], 6) @opponent_ship_1x2 = Ship.new(nil, 6) @opponent_ship_1x2.random_1x2 @opponent_ship_1x3 = Ship.new(@opponent_ship_1x2.coordinates, 6) @opponent_ship_1x3.random_1xSize(3) @opponent_ship_1x4 = Ship.new(@opponent_ship_1x2.coordinates + @opponent_ship_1x3.coordinates, 6) @opponent_ship_1x4.random_1xSize(4) @user_map = Map.new(6) @opponent_map = Map.new(6) @user_evaluator = Evaluator.new(@opponent_ship_1x2, @opponent_ship_1x3, @opponent_ship_1x4, nil, @opponent_map) @opponent_evaluator = Evaluator.new(@user_ship_1x2, @user_ship_1x3, @user_ship_1x4, nil, @user_map) @first_time = true @second_time = false @third_time = false end
Public Instance Methods
computer_guess()
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 176 def computer_guess computer_coordinate = ["A", "B", "C", "D", "E", "F"].sample + rand(1..6).to_s unless self.already_guessed(computer_coordinate, @opponent_evaluator) self.guess(computer_coordinate, @opponent_evaluator) end end
guess(aGuess, evaluator)
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 183 def guess(aGuess, evaluator) hit_or_not = evaluator.hit(aGuess) if evaluator == @user_evaluator hit_or_not ? puts("\n" + Printer.user_guess_right) : puts("\n" + Printer.user_guess_wrong) self.show_opponent_map puts "\n" self.computer_guess self.show_user_map puts "\n" if @opponent_ship_1x2.sunk + @opponent_ship_1x3.sunk + @opponent_ship_1x4.sunk == 3 self.win_game elsif @user_ship_1x2.sunk + @user_ship_1x3.sunk + @user_ship_1x4.sunk == 3 self.lose_game else self.prompt_user end else hit_or_not ? puts(Printer.comp_guess_right) : puts(Printer.comp_guess_wrong + aGuess + ".") end end
mark_initial_ship_position_on_map()
click to toggle source
Calls superclass method
EatTheOcean::Battleship#mark_initial_ship_position_on_map
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 159 def mark_initial_ship_position_on_map super @user_ship_1x4.coordinates.each do |coordinate| @user_map.grid_mark(coordinate, "🐳") end end
prompt_user()
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 31 def prompt_user if @first_time == true puts Printer.first_boat_loop @first_time = false @second_time = true self.user_input_first_boat elsif @second_time == true puts Printer.second_boat_loop @second_time = false @third_time = true self.user_input_second_boat elsif @third_time == true puts Printer.third_boat_loop @third_time = false self.user_input_third_boat else puts Printer.guess_opponent_coordinate self.user_guess end end
show_opponent_map()
click to toggle source
Calls superclass method
EatTheOcean::Battleship#show_opponent_map
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 171 def show_opponent_map super @opponent_ship_1x4.sunk == 1 ? puts(Printer.one_by_four_sunk) : nil end
show_user_map()
click to toggle source
Calls superclass method
EatTheOcean::Battleship#show_user_map
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 166 def show_user_map super @user_ship_1x4.sunk == 1 ? puts(Printer.comp_one_by_four_sunk) : nil end
user_input_second_boat()
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 71 def user_input_second_boat to_validate = gets.chomp validated = self.validate_input(to_validate) first_coordinate = validated puts Printer.next_coordinate to_validate_2 = gets.chomp validated_2 = self.validate_input(to_validate_2) second_coordinate = validated_2 puts Printer.next_coordinate to_validate_3 = gets.chomp validated_3 = self.validate_input(to_validate_3) third_coordinate = validated_3 @user_ship_1x3.coordinates[0] = first_coordinate @user_ship_1x3.coordinates[1] = second_coordinate @user_ship_1x3.coordinates[2] = third_coordinate unless @user_ship_1x3.straight?(@user_ship_1x3.coordinates) @first_time = false @second_time = true puts Printer.not_in_line self.prompt_user end @user_ship_1x3.other_ship_array << @user_ship_1x2.coordinates @user_ship_1x3.other_ship_array.flatten! if @user_ship_1x3.blocked?(@user_ship_1x3.coordinates) @first_time = false @second_time = true puts Printer.blocked self.prompt_user end self.prompt_user end
user_input_third_boat()
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 110 def user_input_third_boat to_validate = gets.chomp validated = self.validate_input(to_validate) first_coordinate = validated puts Printer.next_coordinate to_validate_2 = gets.chomp validated_2 = self.validate_input(to_validate_2) second_coordinate = validated_2 puts Printer.next_coordinate to_validate_3 = gets.chomp validated_3 = self.validate_input(to_validate_3) third_coordinate = validated_3 puts Printer.next_coordinate to_validate_4 = gets.chomp validated_4 = self.validate_input(to_validate_4) fourth_coordinate = validated_4 @user_ship_1x4.coordinates[0] = first_coordinate @user_ship_1x4.coordinates[1] = second_coordinate @user_ship_1x4.coordinates[2] = third_coordinate @user_ship_1x4.coordinates[3] = fourth_coordinate unless @user_ship_1x4.straight?(@user_ship_1x4.coordinates) @second_time = false @third_time = true puts Printer.not_in_line self.prompt_user end @user_ship_1x4.other_ship_array << @user_ship_1x2.coordinates @user_ship_1x4.other_ship_array << @user_ship_1x3.coordinates @user_ship_1x4.other_ship_array.flatten! if @user_ship_1x4.blocked?(@user_ship_1x4.coordinates) @second_time = false @third_time = true puts Printer.blocked self.prompt_user end self.mark_initial_ship_position_on_map self.show_user_map puts Printer.prompt_first_guess self.prompt_user end
validate_input(aString)
click to toggle source
# File lib/eat_the_ocean/battleship_hardmedium.rb, line 52 def validate_input(aString) upcased = aString.upcase if upcased[/[ABCDEF][123456]/] && upcased.length == 2 return upcased[0..1] elsif aString == "q" $user_choice = "q" else input_invalid = true while input_invalid puts Printer.invalid_input upcased = gets.chomp.upcase if upcased[/[ABCDEF][123456]/] input_invalid=false end end end upcased[0..1] end