Class: WiesenbergGemBuildingPractice::RockPaperScissors::Game

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

Instance Method Summary collapse

Instance Method Details

#another_game?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
# File 'lib/wiesenberg_gem_building_practice.rb', line 104

def another_game?
  print "\n Another game (Y or N)? "
  until %w(Y N).include? (answer = gets.chomp)
    print "Incorrect answer - try again: "
  end
  return true if answer == "Y"
  puts "\n Thanks for playing. Goodbye!\n\n"
  false
end

#choose_handshapesObject



65
66
67
# File 'lib/wiesenberg_gem_building_practice.rb', line 65

def choose_handshapes
  [@player_1, @player_2].each { |player| player.choose_handshape}
end

#declare_winnerObject



98
99
100
101
102
# File 'lib/wiesenberg_gem_building_practice.rb', line 98

def declare_winner
  print "\n Winner is #{@winner}. "
  puts "Congratulations!" if @winner != "Computer"
  puts "Bad luck!" if @winner == "Computer"
end

#enter_name(player_number) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/wiesenberg_gem_building_practice.rb', line 57

def enter_name(player_number)
  print " #{player_number.ljust(5, padstr = " ")} player, please enter your name: "
  while "" == (name = gets.chomp)
    print " Incorrect answer - try again: "
  end
  name
end

#enter_namesObject

welcome



46
47
48
49
50
51
52
53
54
55
# File 'lib/wiesenberg_gem_building_practice.rb', line 46

def enter_names
  print "\n Are you playing against the computer (Y or N)? "
  until %w(Y N).include? (answer = gets.chomp)
    print " Incorrect answer - try again: "
  end
  puts
  @player_1 = Human.new(enter_name("First"))
  @player_2 = Computer.new("Computer") if answer == "Y"
  @player_2 = Human.new(enter_name("Next")) if answer == "N"
end

#playObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wiesenberg_gem_building_practice.rb', line 12

def play
  welcome
  enter_names
  loop do
    loop do
      choose_handshapes
      render_choices
      break if winner_found?
    end
    declare_winner
    break unless another_game?
  end
end

#render_choicesObject



69
70
71
72
73
# File 'lib/wiesenberg_gem_building_practice.rb', line 69

def render_choices
  puts "\n #{"Name".ljust(12, padstr = " ")}#{(" " * 5)}Handshape"
  puts " #{("-" * 12) + (" " * 5) + ("-" * 9)}"
  [@player_1, @player_2].each { |player| puts " #{(player.name.ljust(12, padstr = " ")) + (" " * 5) + player.handshape.to_s.capitalize}" }
end

#welcomeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wiesenberg_gem_building_practice.rb', line 26

def welcome

  print %{

 WELCOME TO ROCK-PAPER-SCISSORS!

 Rules:
 1. Choose whether to play against a friend or the computer.
 2. Each human player chooses a handshape. The computer chooses one 
    at random.
 3. When both players choose the same hand shape, it is a draw. Go 
    back to step 2.
 4. When the hand shapes are different, the winner is as follows:
   (a) Rock and scissors: rock wins
   (b) Paper and rock: paper wins
   (c) scissors and paper: scissors win           
         }

end

#winner_1Object



90
91
92
# File 'lib/wiesenberg_gem_building_practice.rb', line 90

def winner_1
  @winner = @player_1.name
end

#winner_2Object



94
95
96
# File 'lib/wiesenberg_gem_building_practice.rb', line 94

def winner_2
  @winner = @player_2.name
end

#winner_found?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wiesenberg_gem_building_practice.rb', line 75

def winner_found?
  case @player_1.handshape
  when @player_2.handshape
    puts "\n Draw - choose again\n"
    return false
  when :rock
    @player_2.handshape == :scissors ? winner_1 : winner_2
  when :paper
    @player_2.handshape == :rock ? winner_1 : winner_2
  when :scissors
    @player_2.handshape == :paper ? winner_1 : winner_2
  end
  true
end