Class: RPS

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

Overview

START

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] }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRPS

Returns a new instance of RPS.



52
53
54
# File 'lib/rps.rb', line 52

def initialize
    @player_score = @computer_score = @ties = 0
end

Class Method Details

.startupObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rps.rb', line 37

def startup()
    puts "|=====================|"
    puts "| Rock Paper Scissors |"
    puts "|---------------------|"
    puts "| Made by: bag33188   |"
    puts "|=====================|"  
    print  "\r\nYou are about to enter a rock-paper-scissors best of 3 match.\r\n"
    print "\r\nPress the return/enter key to continue..."
    gets
    print "\r\n"
end

Instance Method Details

#final_outcome(pl, co) ⇒ Object



108
109
110
111
112
# File 'lib/rps.rb', line 108

def final_outcome(pl, co)
    return :WIN  if pl > co
    return :LOSE if pl < co
    # return :TIE if pl == co
end

#play(winning_score) ⇒ Object



56
57
58
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
# File 'lib/rps.rb', line 56

def play(winning_score)
    while @player_score < winning_score && @computer_score < winning_score
        puts "\r\nPlayer score: #{@player_score}, " +
            "Computer score: #{@computer_score}, Ties: #{@ties}.\r\n"
        player = player_choice
        computer = COMPUTER_CHOICES.sample # chooses a random option
        puts "\r\nPlayer chooses #{player.to_s.downcase}."
        puts "Computer chooses #{computer.to_s.downcase}.\r\n"
        case player_outcome [player, computer]
        when :WIN
            printf "\r\n%s beats %s, player wins the round. \r\n" % [player.to_s.capitalize, computer.to_s.downcase]
            @player_score += 1
        when :LOSE
            printf "\r\n%s beats %s, computer wins the round.\r\n" % [computer.to_s.capitalize, player.to_s.downcase]
            @computer_score += 1
        else
            puts "\r\nTie, choose again\r\n"
            @ties += 1
        end
    end
    printf "\r\nFinal score:\r\nplayer: %i, computer: %i; (ties: %i).\r\n" % [@player_score, @computer_score, @ties]
    case final_outcome(@player_score, @computer_score)
    when :WIN
        puts "Player wins!\r\n"
    when :LOSE
        puts "Computer wins!\r\n"
    end
    print "\n[press the enter/return key to exit the game]"
    gets
    puts
end

#player_choiceObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rps.rb', line 88

def player_choice
    loop do
        print "\r\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 "\r\nThat entry is invalid. Please re-enter.\r\n"
        else
            return nil
        end
    end
end

#player_outcome(plays) ⇒ Object



102
103
104
105
106
# File 'lib/rps.rb', line 102

def player_outcome(plays)
    return :WIN  if WINNERS.include?(plays)
    return :LOSE if LOSERS.include?(plays)
    return :TIE  if !:WIN | !:LOSE
end