Class: RoShamBo

Inherits:
Object
  • Object
show all
Defined in:
lib/ro_sham_bo.rb,
lib/ro_sham_bo/version.rb

Defined Under Namespace

Modules: Version

Constant Summary collapse

RULES =
{
  rock: {rock: :draw, paper: :lose, scissors: :win}.freeze,
  paper: {rock: :win, paper: :draw, scissors: :lose}.freeze,
  scissors: {rock: :lose, paper: :win, scissors: :draw}.freeze
}.freeze
VERSION =

The version, as a string.

Returns:

  • (String)
Version.to_s

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cheater: nil, rounds: 3) ⇒ RoShamBo

Returns a new instance of RoShamBo.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ro_sham_bo.rb', line 20

def initialize(cheater: nil, rounds: 3)
  if rounds.even?
    raise ArgumentError, "rounds must be odd"
  elsif ![nil, :user, :computer].include?(cheater)
    raise ArgumentError, "cheater must be :computer or :user"
  end

  @cheater = cheater
  @rounds = rounds
  @score = {user: 0, computer: 0}
  @draws = 0
  @points_to_win = (rounds.to_f / 2).ceil
end

Instance Attribute Details

#cheaterObject (readonly)

Returns the value of attribute cheater.



10
11
12
# File 'lib/ro_sham_bo.rb', line 10

def cheater
  @cheater
end

#computer_choiceObject (readonly)

Returns the value of attribute computer_choice.



16
17
18
# File 'lib/ro_sham_bo.rb', line 16

def computer_choice
  @computer_choice
end

#drawsObject (readonly)

Returns the value of attribute draws.



13
14
15
# File 'lib/ro_sham_bo.rb', line 13

def draws
  @draws
end

#points_to_winObject (readonly)

Returns the value of attribute points_to_win.



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

def points_to_win
  @points_to_win
end

#round_winnerObject (readonly)

Returns the value of attribute round_winner.



17
18
19
# File 'lib/ro_sham_bo.rb', line 17

def round_winner
  @round_winner
end

#roundsObject (readonly)

Returns the value of attribute rounds.



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

def rounds
  @rounds
end

#scoreObject (readonly)

Returns the value of attribute score.



12
13
14
# File 'lib/ro_sham_bo.rb', line 12

def score
  @score
end

#user_choiceObject (readonly)

Returns the value of attribute user_choice.



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

def user_choice
  @user_choice
end

#winnerObject (readonly)

Returns the value of attribute winner.



18
19
20
# File 'lib/ro_sham_bo.rb', line 18

def winner
  @winner
end

Instance Method Details

#over?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ro_sham_bo.rb', line 45

def over?
  !winner.nil?
end

#play(input) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/ro_sham_bo.rb', line 34

def play(input)
  raise "Game Over!" if over?

  @user_choice = sanitized_choice(input)
  @computer_choice = computer_turn(user_choice)
  @round_winner = determine_round_winner(@user_choice, @computer_choice)
  @winner = score.key(points_to_win) if score.value?(points_to_win)

  self
end