Last active
December 3, 2016 17:02
-
-
Save bdcheung/a8403cb3e86c367fc4510748863b034c to your computer and use it in GitHub Desktop.
Rock Paper Scissor Game for CareerFoundry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
class RockPaperScissorGame | |
attr_accessor :player_choice, :computer_choice | |
CHOICES = { | |
:p => 'Paper', | |
:r => 'Rock', | |
:s => 'Scissor' | |
}.freeze | |
WINNERS = { | |
[:p, :r] => :p, | |
[:p, :s] => :s, | |
[:r, :s] => :r | |
}.freeze | |
def initialize | |
@player_choice = get_player_choice | |
play | |
evaluate_again | |
end | |
def evaluate_again | |
puts "Play again? (y/n)" | |
choice = gets.chomp.downcase | |
if choice == 'y' | |
self.class.new | |
else | |
puts "Goodbye!" | |
end | |
end | |
def get_player_choice | |
puts "What is your selection?" | |
puts 'For Rock enter "R"' | |
puts 'For Paper enter "P"' | |
puts 'For Scissor enter "S"' | |
player_choice = gets.chomp.downcase.to_sym | |
if CHOICES.keys.include?(player_choice) | |
player_choice | |
else | |
puts "That's not a valid selection" | |
self.class.new | |
end | |
end | |
def choice_to_s(choice) | |
CHOICES[choice] | |
end | |
def computer_choice | |
@computer_choice ||= CHOICES.keys.sample | |
end | |
def play | |
puts "You chose #{CHOICES[player_choice]}, the computer chose #{CHOICES[computer_choice]}" | |
evaluate_outcome(player_choice, computer_choice) | |
end | |
def evaluate_outcome(player_choice, computer_choice) | |
if player_choice == computer_choice | |
puts "You and the computer both selected #{CHOICES[player_choice]}" | |
else | |
winning_symbol = determine_winner(player_choice, computer_choice) | |
report_winner(winning_symbol) | |
end | |
end | |
def report_winner(symbol) | |
case symbol | |
when player_choice | |
puts "The winner is you!" | |
when computer_choice | |
puts 'The computer won!' | |
else | |
puts 'I have no idea what just happened' | |
end | |
end | |
def determine_winner(s1, s2) | |
object_1 = eval(CHOICES[s1]).new | |
object_2 = eval(CHOICES[s2]).new | |
winner = object_1.beaten_by(object_2) ? s2 : s1 | |
winner | |
end | |
end | |
class Rock < RockPaperScissorGame | |
def initialize() | |
end | |
def beaten_by(comparison_object) | |
if comparison_object.is_a?(Paper) | |
true | |
else | |
false | |
end | |
end | |
end | |
class Paper < RockPaperScissorGame | |
def initialize() | |
end | |
def beaten_by(comparison_object) | |
if comparison_object.is_a?(Scissor) | |
true | |
else | |
false | |
end | |
end | |
end | |
class Scissor < RockPaperScissorGame | |
def initialize() | |
end | |
def beaten_by(comparison_object) | |
if comparison_object.is_a?(Rock) | |
true | |
else | |
false | |
end | |
end | |
end | |
RockPaperScissorGame.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment