class EatTheOcean::Battleship
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.rb, line 12 def initialize @user_ship_1x2 = Ship.new @user_ship_1x3 = Ship.new @opponent_ship_1x2 = Ship.new @opponent_ship_1x2.random_1x2 @opponent_ship_1x3 = Ship.new(@opponent_ship_1x2.coordinates) @opponent_ship_1x3.random_1xSize(3) @user_map = Map.new(4) @opponent_map = Map.new(4) @user_evaluator = Evaluator.new(@opponent_ship_1x2, @opponent_ship_1x3, nil, nil, @opponent_map) @opponent_evaluator = Evaluator.new(@user_ship_1x2, @user_ship_1x3, nil, nil, @user_map) @first_time = true @second_time = false end
Public Instance Methods
already_guessed(coordinate, evaluator)
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 153 def already_guessed(coordinate, evaluator) #begin janky looped = false if evaluator.guess_record.include?(coordinate) evaluator == @user_evaluator ? puts(Printer.already_guessed) : nil looped = true self.send(caller[0][/`.*'/][1..-2].to_sym) end looped #end janky end
computer_guess()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 172 def computer_guess computer_coordinate = ["A", "B", "C", "D"].sample + rand(1..4).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.rb, line 179 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 == 2 self.win_game elsif @user_ship_1x2.sunk + @user_ship_1x3.sunk == 2 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
lose_game()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 205 def lose_game puts "\n" + "You lose. The computer demoralized you in #{@opponent_evaluator.guess_record.length} moves." + "\n\n" # puts "Play again? (y\\n)" # answer = gets.chomp # if answer == "y" $user_choice = "q" end
mark_initial_ship_position_on_map()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 129 def mark_initial_ship_position_on_map @user_ship_1x2.coordinates.each do |coordinate| @user_map.grid_mark(coordinate, "🐙") end @user_ship_1x3.coordinates.each do |coordinate| @user_map.grid_mark(coordinate, "🐬") end end
prompt_user()
click to toggle source
# File lib/eat_the_ocean/battleship.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 self.user_input_second_boat else puts Printer.guess_opponent_coordinate self.user_guess end end
show_opponent_map()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 146 def show_opponent_map puts Printer.opponent_map puts @opponent_map.grid_array @opponent_ship_1x2.sunk == 1 ? puts(Printer.one_by_two_sunk) : nil @opponent_ship_1x3.sunk == 1 ? puts(Printer.one_by_three_sunk) : nil end
show_user_map()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 139 def show_user_map puts Printer.user_map puts @user_map.grid_array @user_ship_1x2.sunk == 1 ? puts(Printer.comp_one_by_two_sunk) : nil @user_ship_1x3.sunk == 1 ? puts(Printer.comp_one_by_three_sunk) : nil end
user_guess()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 165 def user_guess user_coordinate = gets.chomp validated_coordinate = self.validate_input(user_coordinate) self.already_guessed(validated_coordinate, @user_evaluator) self.guess(validated_coordinate, @user_evaluator) end
user_input_first_boat()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 66 def user_input_first_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 @user_ship_1x2.coordinates[0] = first_coordinate @user_ship_1x2.coordinates[1] = second_coordinate unless @user_ship_1x2.straight?(@user_ship_1x2.coordinates) @first_time = true @second_time = false puts Printer.not_in_line end self.prompt_user end
user_input_second_boat()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 87 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.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.rb, line 47 def validate_input(aString) upcased = aString.upcase if upcased[/[ABCD][1234]/] && 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[/[ABCD][1234]/] input_invalid=false end end end upcased[0..1] end
win_game()
click to toggle source
# File lib/eat_the_ocean/battleship.rb, line 200 def win_game puts "\n" + "You win this (unethical) game! You defeated the computer in #{@user_evaluator.guess_record.length} moves." + "\n\n" $user_choice = "q" end