Class: MAndMRps::Code

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

Instance Method Summary collapse

Constructor Details

#initializeCode

Returns a new instance of Code.



14
15
16
17
# File 'lib/code.rb', line 14

def initialize
  @options = ["rock", "paper", "scissors"]
  @player1 = Human.new
end

Instance Method Details

#compareObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/code.rb', line 31

def compare

    if @choice1 == @choice2
      puts "Tie. Try Again"

    elsif @choice1 == "rock"
      if @choice2 == "paper"
        outcome(2)
      else
        outcome(1)
      end

    elsif @choice1 == "paper"
      if @choice2 == "scissors"
        outcome(2)
      else
        outcome(1)
      end

    elsif @choice1 == "scissors"
      if @choice2 == "rock"
        outcome(2)
      else
        outcome(1)
      end

  end

end

#outcome(val) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/code.rb', line 61

def outcome(val)

  if val == 1
    puts "Player 1 wins"
  else
    puts "Player 2 wins"
  end

end

#playObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/code.rb', line 19

def play
 select_opponent

 @choice1 = @player1.choice(@options)
 p @choice1
 @choice2 = @player2.choice(@options)
 p @choice2

 compare

end

#select_opponentObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/code.rb', line 71

def select_opponent
  puts "Is your opponent human (h) or computer?"
  choice = gets.chomp

  if choice == "h"
    @player2 = Human.new
  else
    @player2 = Computer.new
  end

end