class RockPaperScissors
Constants
- CHOICES
make choices constant (2d list)
- COMPUTER_CHOICES
define possible computer choices
- LOSERS
map array for losers
- NTRY_TO_SYM
define an entry to symbol dictionary
- VALID_ENTRIES
define what our valid entries out
- WINNERS
define winners 2d array
Public Class Methods
new()
click to toggle source
define constructor for class
# File lib/RockPaperScissors.rb, line 47 def initialize @player_score = @computer_score = @ties = 0 end
Protected Class Methods
continue(str1,str2,str3)
click to toggle source
define entry point for game
# File lib/RockPaperScissors.rb, line 35 def continue(str1,str2,str3) puts str1 print str2 gets puts str3 end
Public Instance Methods
play(winning_score)
click to toggle source
define play game method
# File lib/RockPaperScissors.rb, line 53 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