Class: RockPaperScissors
- Inherits:
-
Object
- Object
- RockPaperScissors
- Defined in:
- lib/RockPaperScissors.rb
Constant Summary collapse
- CHOICES =
Make choices constant (2d list)
this 2d array contains all the options (in string format) the user can use
[['r', 'rock'], ['p', 'paper'], ['s', 'scissors']]
- NTRY_TO_SYM =
Define an entry to symbol hash
this is a “hash” that contains the keys and values for winning and losing
{ CHOICES[0][0] => :ROCK , CHOICES[0][1] => :ROCK , CHOICES[1][0] => :PAPER , CHOICES[1][1] => :PAPER , CHOICES[2][0] => :SCISSORS, CHOICES[2][1] => :SCISSORS }
- VALID_ENTRIES =
Define what our valid entries out
grab the keys of the hash
NTRY_TO_SYM.keys
- COMPUTER_CHOICES =
Define possible computer choices
grab the values of the hash
NTRY_TO_SYM.values
- WINNERS =
Define winners 2d array
defines winners in 2d array/list format
[ [:SCISSORS, :PAPER ], [:PAPER , :ROCK ], [:ROCK , :SCISSORS] ]
- LOSERS =
Map array for losers
this flips the items in the ‘WINNERS` 2d array
WINNERS.map { |winning_choice,losing_choice| [losing_choice,winning_choice] }
Class Method Summary collapse
Instance Method Summary collapse
-
#final_outcome(pl, co) ⇒ Object
Define final outcome function this will determine who wins the whole match in the end.
-
#initialize ⇒ RockPaperScissors
constructor
Define constructor for class.
-
#play(winning_score) ⇒ Object
Define play game method it will also constain most of the “play” functioonality for this game.
-
#player_choice ⇒ Object
Make player choice method this will sanity check the user’s choice.
-
#player_outcome(plays) ⇒ Object
Define player outcome this defines the round outcome of the game.
Constructor Details
#initialize ⇒ RockPaperScissors
Define constructor for class
53 54 55 |
# File 'lib/RockPaperScissors.rb', line 53 def initialize @player_score = @computer_score = @ties = 0 end |
Class Method Details
.continue(str1, str2, str3) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/RockPaperScissors.rb', line 42 def continue(str1,str2,str3) puts str1 print str2 gets puts str3 end |
Instance Method Details
#final_outcome(pl, co) ⇒ Object
Define final outcome function
this will determine who wins the whole match in the end
117 118 119 120 |
# File 'lib/RockPaperScissors.rb', line 117 def final_outcome(pl, co) return :WIN if pl > co return :LOSE if pl < co end |
#play(winning_score) ⇒ Object
Define play game method
it will also constain most of the “play” functioonality for this game
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/RockPaperScissors.rb', line 59 def play(winning_score) while @player_score < winning_score && @computer_score < winning_score puts "\nPlayer score: #{@player_score}, " + "Computer score: #{@computer_score}, Ties: #{@ties}.\n" player = player_choice computer = COMPUTER_CHOICES.sample # chooses a random option puts "\nPlayer chooses #{player.to_s.downcase}." puts "Computer chooses #{computer.to_s.downcase}.\n" case player_outcome [player, computer] when :WIN printf "\n%s beats %s, player wins the round. \n\n" % [player.to_s.capitalize, computer.to_s.downcase] @player_score += 1 when :LOSE printf "\n%s beats %s, computer wins the round.\n\n" % [computer.to_s.capitalize, player.to_s.downcase] @computer_score += 1 else puts "\nTie, choose again\n" @ties += 1 end end printf "\nFinal score:\nplayer: %i, computer: %i; (ties: %i).\n\n" % [@player_score, @computer_score, @ties] case final_outcome(@player_score, @computer_score) when :WIN puts "Player wins!\n" when :LOSE puts "Computer wins!\n" end print "\n[press the enter/return key to exit game]" gets puts end |
#player_choice ⇒ Object
Make player choice method
this will sanity check the user’s choice
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/RockPaperScissors.rb', line 93 def player_choice loop do print "\nChoose: Rock (r), Paper (p), or Scissors (s): " choice = gets.chomp.downcase if NTRY_TO_SYM.key?(choice) return NTRY_TO_SYM[choice] elsif choice != VALID_ENTRIES puts "\nThat entry is invalid. Please re-enter.\n" else return nil end end end |
#player_outcome(plays) ⇒ Object
Define player outcome
this defines the round outcome of the game
109 110 111 112 113 |
# File 'lib/RockPaperScissors.rb', line 109 def player_outcome(plays) return :WIN if WINNERS.include?(plays) return :LOSE if LOSERS.include?(plays) return :TIE if !:WIN | !:LOSE end |