Class: RockPaperScissorsGame

Inherits:
Object
  • Object
show all
Defined in:
lib/Main.rb

Overview

create master class for rpsg

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRockPaperScissorsGame

initialize variables



24
25
26
27
# File 'lib/Main.rb', line 24

def initialize
  # initialize variables and set all equal to zero
  @player_score = @computer_score = @ties = 0
end

Class Method Details

.continue(str1, str2, str3) ⇒ Object

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



12
13
14
15
16
17
# File 'lib/Main.rb', line 12

def continue(str1,str2,str3)
  puts  str1
  print str2
  gets
  puts  str3
end

Instance Method Details

#play(winning_score) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/Main.rb', line 30

def play(winning_score)
  # make while loop
  while @player_score < winning_score && @computer_score < winning_score
    puts "\nPlayer score: #{@player_score}, " +
         "Computer score: #{@computer_score}, Ties: #{@ties}.\n"
    player = PrivateMethods.player_choice
    computer = ProtectedConstants::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!\n"
  when :LOSE
    puts "\nComputer wins!\n"
  else
    puts "\nIt's a tie!\n"
  end
  print "\n[press the enter/return key to exit game]"
  gets
end