Class: RpsGem::LetsPlay

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exampleObject



28
29
30
31
# File 'lib/rps_gem.rb', line 28

def self.example
  path = File.dirname(__FILE__)
  system(" man #{path}/rps_gem_man")
end

Instance Method Details

#play(player_op) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rps_gem.rb', line 3

def play(player_op)
  options = ["rock","paper","scissors"]
  player_choice = options.find_index {|choice| choice.match(/^#{player_op.downcase}/)}
  computer_choice = rand(options.length)
  if player_choice.nil?
    "Invalid Choice :P"
  else
    if(options[computer_choice]==options[player_choice])
      "Tie!"
    else
      puts "Computer plyaed #{options[computer_choice]}"
      if(self.winner(options[player_choice], options[computer_choice])==options[player_choice])
        "You Won!"
      else
        "You Loose!"
      end
    end  
  end
end

#winner(p1, p2) ⇒ Object



23
24
25
26
# File 'lib/rps_gem.rb', line 23

def winner(p1, p2)
  wins = {rock: :scissors, scissors: :paper, paper: :rock}
  {true => p1, false => p2}[wins[p1] == p2]
end