Module: Pc::Game

Includes:
Loop
Included in:
OddsAndEvens, RockPaperScissors
Defined in:
lib/pc/game.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loop

#exit, #goodbye, #run, #say

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/pc/game.rb', line 7

def self.included(base)
  base.extend base
end

Instance Method Details

#choose(choice) ⇒ Object



27
28
29
30
31
# File 'lib/pc/game.rb', line 27

def choose(choice)
  @move_p1 = choice
  @move_p2 = random_move
  say %{You played #{@move_p1} - I played #{@move_p2}}, next: ->{show_outcome}
end

#compare_moves(move_a, move_b) ⇒ Object



46
47
48
# File 'lib/pc/game.rb', line 46

def compare_moves(move_a, move_b)
  move_a <=> move_b
end

#movesObject



19
20
21
# File 'lib/pc/game.rb', line 19

def moves
  {}
end

#prompt_moveObject



23
24
25
# File 'lib/pc/game.rb', line 23

def prompt_move
  ask %{Choose a move}, Hash[*moves.map{|k, v| [k, ->{choose v}]}.flatten]
end

#random_moveObject



33
34
35
# File 'lib/pc/game.rb', line 33

def random_move
  moves.values.sample
end

#rulesObject



15
16
17
# File 'lib/pc/game.rb', line 15

def rules
  '[no rules defined]'
end

#show_outcomeObject



37
38
39
40
41
42
43
44
# File 'lib/pc/game.rb', line 37

def show_outcome
  outcome = case compare_moves(@move_p1, @move_p2)
    when 1  then %{You win!}
    when -1 then %{You lose!}
    when 0  then %{It’s a tie!}
  end
  say outcome, next: ->{exit}
end

#welcomeObject



11
12
13
# File 'lib/pc/game.rb', line 11

def welcome
  say %{Rules: #{rules}}, next: ->{prompt_move}
end