Class: Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Player

Returns a new instance of Player.



6
7
8
9
10
# File 'lib/greed_game/player.rb', line 6

def initialize(name)
  @name = name
  @score_roll = 0
  @score_total = 0
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/greed_game/player.rb', line 4

def name
  @name
end

#score_rollObject (readonly)

Returns the value of attribute score_roll.



3
4
5
# File 'lib/greed_game/player.rb', line 3

def score_roll
  @score_roll
end

#score_totalObject (readonly)

Returns the value of attribute score_total.



3
4
5
# File 'lib/greed_game/player.rb', line 3

def score_total
  @score_total
end

Instance Method Details

#calculate_dice_for_next_roll(dice) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/greed_game/player.rb', line 26

def calculate_dice_for_next_roll(dice)
  calculate_non_scoring_dice(dice)
  if @count == 0 and dice.count > 0
    @count = 5
  elsif @count == dice.count
    @count = 0
  end
  @count = 0 unless next_roll?
  @count
end

#calculate_non_scoring_dice(dice) ⇒ Object



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

def calculate_non_scoring_dice(dice)
  @count = dice.count
  dice.uniq.each do |n|
    if n == 1
      @count = @count - dice.count(1)
    elsif n == 5
      @count = @count - dice.count(5)
    else
      @count = @count - 3 if dice.count(n) >= 3
    end
  end
  @count
end

#calculate_score_roll(score) ⇒ Object



67
68
69
70
71
# File 'lib/greed_game/player.rb', line 67

def calculate_score_roll(score)
  score == 0 ? @score_roll = 0 : @score_roll += score
  @score = 0
  @score_roll
end

#calculate_score_total(score_roll) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/greed_game/player.rb', line 73

def calculate_score_total(score_roll)
  if @score_total == 0
    @score_total += score_roll if score_roll >= 300
  else
    @score_total += score_roll      
  end
  @score_roll = 0
  @score_total
end

#next_roll?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/greed_game/player.rb', line 37

def next_roll?
  if @count > 0
    puts "Player #{name}, You have #{@score} score and You can use #{@count} dice. \n" +
  "If You want do roll, enter 'y'"
    answer = STDIN.gets.chop!
    answer == "y"
  else
    false
  end
end

#roll(dices) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/greed_game/player.rb', line 83

def roll(dices)
  while dices > 0 
    dice_set = DiceSet.new
    dice_roll = dice_set.roll(dices)
    score = score(dice_roll)
    dices = calculate_dice_for_next_roll(dice_roll)
    score_roll = calculate_score_roll(score)
    calculate_score_total(score_roll) if dices == 0
  end
end

#score(dice) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/greed_game/player.rb', line 48

def score(dice)
  @score = 0
  dice.uniq.each do |n|
    if n == 1
      c = dice.count(1)
      @score += 100 * dice.count(1) if c < 3
      @score += 1000 + 100 * (c - 3) if  c >= 3
    elsif n == 5
      c = dice.count(5)
      @score += 50 * c if c < 3
      @score += 5  * 100 + 50 * (c - 3) if c >= 3
    else
      c = dice.count(n)
      @score += n * 100 if c >= 3
    end
  end
  @score
end