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



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

def initialize
  @score = ScoreSheet.new
end

Instance Attribute Details

#scoreScoreSheet (readonly)



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

def score
  @score
end

Instance Method Details

#display_dice(i) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/player.rb', line 41

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

#take_turnvoid



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|
    display_dice i
    turn_over = user_input i 
    break if turn_over
  end
end

#user_input(i) ⇒ Boolean

Note:

Gameplay



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/player.rb', line 55

def user_input(i)
  if i < 3
    print "Select dice to re-roll or select a score category: "
  else
    print "No rolls remaining. Select a score category: "
  end
  input = gets.chomp.downcase
  input_symbol = input.to_sym
  user_input = Set.new(input.split(''))
  dice_controls = Set.new("zxcvb".split(''))

  # If user wants to enter score
  if ScoreAbbr.keys.include? input_symbol
    score.enter_score ScoreAbbr[input_symbol]
    puts score
    @score.dice.roll_all
    sleep 2
    return true
  # Else if user wants to roll the dice
  elsif i < 3 && (user_input.subset? dice_controls)
    dice_to_roll = (0..4).to_a.select { |index| input.include? dice_controls.to_a[index]}
    @score.dice.roll(dice_to_roll)
    sleep 0.5
    2.times {puts ?\n}
    puts " Rolling Dice!\ ".center 80, "* "
    sleep 1
    return false 
  else
    puts "Invalid input. Please try again."
    sleep 1.5
    puts ?\n
    user_input i
  end
end