Class: RockPaperScissors

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

Constant Summary collapse

CHOICES =
[['r', 'rock'], ['p', 'paper'], ['s', 'scissors']]
NTRY_TO_SYM =
{
    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 =
NTRY_TO_SYM.keys
COMPUTER_CHOICES =
NTRY_TO_SYM.values
WINNERS =
[
    [:SCISSORS, :PAPER   ],
    [:PAPER   , :ROCK    ],
    [:ROCK    , :SCISSORS]
]
LOSERS =
WINNERS.map { |winning_choice,losing_choice| [losing_choice,winning_choice] }

Instance Method Summary collapse

Instance Method Details

#play(winning_score) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/RockPaperScissors.rb', line 47

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 "\nPlayer wins!\n"
    when :LOSE
        puts "\nComputer wins!\n"
    end
    print "\n[press the enter/return key to exit game]"
    gets
end