Class: CribbageGame::Score
- Inherits:
-
Object
- Object
- CribbageGame::Score
show all
- Includes:
- ScoreUtils
- Defined in:
- lib/cribbage_game/score.rb
Instance Method Summary
collapse
Methods included from ScoreUtils
#are_same_suit, #get_pile_points, #is_run, #score_consecutive, #score_fifteens, #score_flush, #score_hand_runs, #score_n_of_a_kind, #score_nobs, #score_pile_runs
Constructor Details
#initialize(game) ⇒ Score
Returns a new instance of Score.
7
8
9
10
|
# File 'lib/cribbage_game/score.rb', line 7
def initialize game
@game = game
@scorecards = {}
end
|
Instance Method Details
#empty_scorecard ⇒ Object
16
17
18
|
# File 'lib/cribbage_game/score.rb', line 16
def empty_scorecard
{crib: {total_score: 0}, hand: {total_score: 0}}
end
|
#get_cards(card_ids) ⇒ Object
12
13
14
|
# File 'lib/cribbage_game/score.rb', line 12
def get_cards card_ids
card_ids.map { |id| @game.deck[id] }
end
|
#get_scorecard(player) ⇒ Object
20
21
22
23
24
|
# File 'lib/cribbage_game/score.rb', line 20
def get_scorecard player
@scorecards[@game.round] = @scorecards[@game.round] || {play: []}
@scorecards[@game.round][player.id] = @scorecards[@game.round][player.id] || empty_scorecard
@scorecards[@game.round][player.id]
end
|
#score_crib ⇒ Object
26
27
28
29
|
# File 'lib/cribbage_game/score.rb', line 26
def score_crib
cards = get_cards(@game.crib + [@game.cut_card])
get_scorecard(@game.dealer)[:crib][:total_score] = score_hand(cards, true)
end
|
#score_hand(cards, is_crib = false) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/cribbage_game/score.rb', line 38
def score_hand cards, is_crib = false
n_of_kind_score = score_n_of_a_kind cards
sum_score = score_fifteens cards
run_score = score_hand_runs cards
flush_score = score_flush(cards, is_crib)
nobs_score = score_nobs(cards)
n_of_kind_score + sum_score + run_score + flush_score + nobs_score
end
|
#score_hands ⇒ Object
31
32
33
34
35
36
|
# File 'lib/cribbage_game/score.rb', line 31
def score_hands
@game.players.each do |player|
cards = get_cards(player.hand.keys + [@game.cut_card])
get_scorecard(player)[:hand][:total_score] = score_hand cards
end
end
|
#score_play(pile_ids, is_last_card, player) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/cribbage_game/score.rb', line 47
def score_play pile_ids, is_last_card, player
@scorecards[@game.round] = @scorecards[@game.round] || {play: []}
pile_cards = get_cards pile_ids
points = get_pile_points(pile_cards, is_last_card)
@scorecards[@game.round][:play] << {
points: points,
player_id: player.id,
pile: pile_ids,
card_id: pile_ids.last
}
end
|
#submit_play_score(player, submitted_scores = nil) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/cribbage_game/score.rb', line 59
def submit_play_score player, submitted_scores = nil
raise "You must submit scores if auto_score=false" if submitted_scores.nil? && !@game.auto_score
if @game.auto_score
player.total_score += @scorecards[@game.round][:play].last[:points]
end
end
|
#submit_scores(player, card_group, submitted_scores = nil) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/cribbage_game/score.rb', line 67
def submit_scores player, card_group, submitted_scores = nil
raise "You must submit scores if auto_score=false" if submitted_scores.nil? && !@game.auto_score
raise "card_group must be :hand or :crib" if ![:hand, :crib].include? card_group
possible_scores = get_scorecard(player)[card_group]
if @game.auto_score
player.total_score += possible_scores[:total_score]
end
end
|