class RockPapaerScissorsGame

Public Class Methods

continue(str1,str2,str3) click to toggle source

add continue method for asking the user if they want to play rock paper scissors

# File bin/rpsg, line 17
def continue(str1,str2,str3)
  puts  str1 
  print str2
  gets 
  puts  str3
end
new() click to toggle source

initialize variables

# File bin/rpsg, line 29
def initialize
  # set all equal to zero
  @player_score = @computer_score = @ties = 0 
end

Public Instance Methods

play(winning_score) click to toggle source

define play method, this will be the main function for playing rock paper scissors

# File bin/rpsg, line 35
def play(winning_score) 
  # make while loop
  while @player_score < winning_score && @computer_score < winning_score 
    puts "Player score: #{@player_score}, " + 
         "Computer score: #{@computer_score}, Ties: #{@ties}.\n"
    player = PrivateMethods.player_choice 
    computer = Constants::COMPUTER_CHOICES.sample # chooses a random option
    puts "\nPlayer chooses #{player.to_s.downcase}." 
    puts "Computer chooses #{computer.to_s.downcase}.\n" 
    case PrivateMethods.player_outcome [player, computer] 
    when :WIN
      puts "\n#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round.\n"  
      @player_score += 1
    when :LOSE
      puts "\n#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round.\n" 
      @computer_score += 1 
    else 
      puts "\nTie, choose again\n" 
      @ties += 1
    end
  end
  puts "\nFinal score: player: #{@player_score}, " +
       "computer: #{@computer_score} (ties: #{@ties}).\n" 
  case PrivateMethods.final_outcome(@player_score, @computer_score)
  when :WIN 
    puts "\nPlayer wins!" 
  when :LOSE
    puts "\nComputer wins!" 
  else 
    puts "\nIt's a tie!" 
  end 
  gets
end