Class: MegaRps::Game

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

Overview

Game handle game logic/flow Player Make move Human < Player Collect input from command line Computer < Player Generate move randomly

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



18
19
20
21
22
23
# File 'lib/mega_rps.rb', line 18

def initialize

  @player = Human.new
  @computer = Computer.new

end

Instance Method Details

#play_again?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
# File 'lib/mega_rps.rb', line 67

def play_again?

  puts "Would you like to play again? Y/N"

  if gets.chomp[0].downcase == 'y'
    true
  else 
    false
  end
end

#play_gameObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mega_rps.rb', line 26

def play_game

  print_instructions

  while play_again?
    @player.move
    @computer.move
    print_result
    play_again?
  end

end


40
41
42
43
44
45
# File 'lib/mega_rps.rb', line 40

def print_instructions

p "Let's play MEGA Rock, Paper, Scissors!"
p "Enter (r)ock, (p)aper, or (s)cissors to make your move >>"

end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mega_rps.rb', line 49

def print_result

  if @player.move == @computer.move
    puts "Draw"
  elsif @player.move == 's' && @computer.move == 'r' || 
        @player.move == 'p' && @computer.move == 's' ||
        @player.move == 'r' && @computer.move == 'p'

    puts "Computer Wins"

  else

     puts "You Win"

  end
  
end