Class: Player

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

Constant Summary collapse

ScoreAbbr =

Abbreviations used in-game

{ # Abbreviations used in-game

  ?1.to_sym => :ones,
  ?2.to_sym => :twos,
  ?3.to_sym => :threes,
  ?4.to_sym => :fours,
  ?5.to_sym => :fives,
  ?6.to_sym => :sixes,
  ss: :small_straight,
  ls: :large_straight,
  tok: :three_of_a_kind,
  fok: :four_of_a_kind,
  fh: :full_house,
  y: :yahtzee,
  "?".to_sym => :chance
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



24
25
26
# File 'lib/player.rb', line 24

def initialize
  @score = ScoreSheet.new
end

Instance Attribute Details

#scoreScoreSheet (readonly)

Returns:



22
23
24
# File 'lib/player.rb', line 22

def score
  @score
end

Instance Method Details

#display_dice(i) ⇒ String

Parameters:

  • i (Fixnum)

    times rolled

Returns:

  • (String)


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

def display_dice(i)
  sleep 0.5
  result = String.new
  result << String.new.center(80, ?-) + ?\n 
  result << "Here are your dice. You have have #{3-i} #{i==2? "roll":"rolls"} remaining.\n\n\n"
  result << "\tDice\t\tZ\tX\tC\tV\tB" + ?\n
  result << "\tValues\t\t" + score.dice.values.map{|value| value.to_s}.join(?\t) + ?\n
  result << String.new.center(80, ?-) + ?\n
  return result
end

#take_turnvoid

TODO:

finish method

This method returns an undefined value.



32
33
34
35
36
37
38
39
# File 'lib/player.rb', line 32

def take_turn
  turn_over = false
  (1..3).each do |i|
    puts display_dice i
    turn_over = user_input i 
    break if turn_over
  end
end

#user_input(i) ⇒ Boolean

Note:

Gameplay

Parameters:

  • i (Fixnum)

    Amount of times rolled

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/player.rb', line 61

def user_input(i)

  print_instructions i

  input = gets.chomp.downcase
  user_input = Set.new(input.split(''))
  dice_controls = Set.new("zxcvb".split(''))

  # If user wants to enter score

  if ScoreAbbr.keys.include? input.to_sym
    user_enter_score input.to_sym, i
  # Else if user wants to roll the dice

  elsif i < 3 && (user_input.subset? dice_controls)
    user_roll_dice input, dice_controls
  else
    invalid_input
  end
end