Class: SplendorGame::Tableau

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

Constant Summary collapse

@@max_reserved_cards =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_limit) ⇒ Tableau

Returns a new instance of Tableau.



8
9
10
11
12
13
14
# File 'lib/splendor_game/tableau.rb', line 8

def initialize(token_limit)
  @cards = Array.new()
  @tokens = Hash.new(0)
  @unlimited = token_limit <= 0 ? true : false
  @token_limit = token_limit
  @reserved_cards = Array.new()
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



6
7
8
# File 'lib/splendor_game/tableau.rb', line 6

def cards
  @cards
end

#reserved_cardsObject (readonly)

Returns the value of attribute reserved_cards.



6
7
8
# File 'lib/splendor_game/tableau.rb', line 6

def reserved_cards
  @reserved_cards
end

#tokensObject (readonly)

Returns the value of attribute tokens.



6
7
8
# File 'lib/splendor_game/tableau.rb', line 6

def tokens
  @tokens
end

Instance Method Details

#add_token(token_colour) ⇒ Object

add tokens, remove tokens, counting tokens



23
24
25
26
27
28
29
30
31
32
# File 'lib/splendor_game/tableau.rb', line 23

def add_token(token_colour)
  return false if !VALID_COLOUR_SYMBOLS.include?(token_colour)
  return false if !@unlimited && token_count >= @token_limit
  if @tokens.include?(token_colour)
    @tokens[token_colour] += 1
  else
    @tokens[token_colour] = 1
  end
  true
end

#all_colours_on_cardsObject



97
98
99
100
101
# File 'lib/splendor_game/tableau.rb', line 97

def all_colours_on_cards
  output = Hash.new()
  VALID_COLOUR_SYMBOLS.each { |c| output[c] = colours_on_cards(c) if colours_on_cards(c) > 0 }
  output
end

#can_reserve_card?Boolean

reserving cards

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/splendor_game/tableau.rb', line 56

def can_reserve_card?
  return false if @reserved_cards.size >= @@max_reserved_cards
  true
end

#colours_on_cards(colour) ⇒ Object



103
104
105
# File 'lib/splendor_game/tableau.rb', line 103

def colours_on_cards(colour)
  @cards.inject(0) { |sum,card| card.colour == colour ? sum+1 : sum }.to_i
end

#distinct_token_colour_countObject



50
51
52
# File 'lib/splendor_game/tableau.rb', line 50

def distinct_token_colour_count
  @tokens.count { |_k,v| v >0 }
end

#is_empty?Boolean

Other

Returns:

  • (Boolean)


109
110
111
# File 'lib/splendor_game/tableau.rb', line 109

def is_empty?
  @cards.size==0 && @tokens.size==0 ? true : false
end

#play_reserved_card(card) ⇒ Object



66
67
68
69
70
71
# File 'lib/splendor_game/tableau.rb', line 66

def play_reserved_card(card)
  return false if tokens_required(card) == false
  return false unless @reserved_cards.include?(card)
  @cards << card
  @reserved_cards.delete(card)
end

#purchase_card(card) ⇒ Object

related to purchasing cards



75
76
77
78
# File 'lib/splendor_game/tableau.rb', line 75

def purchase_card(card)
  return false if tokens_required(card) == false
  @cards << card
end

#remove_token(token_colour) ⇒ Object



34
35
36
37
38
39
# File 'lib/splendor_game/tableau.rb', line 34

def remove_token(token_colour)
  return false if !@tokens.key?(token_colour)
  return false if @tokens[token_colour] <= 0
  @tokens[token_colour] -= 1
  true
end

#reserve_card(card) ⇒ Object



61
62
63
64
# File 'lib/splendor_game/tableau.rb', line 61

def reserve_card(card)
  return false if !can_reserve_card?
  @reserved_cards << card
end

#seed_bank(args) ⇒ Object



16
17
18
19
# File 'lib/splendor_game/tableau.rb', line 16

def seed_bank(args)
  seed_bank_non_gold(args[:options][:starting_non_gold_tokens][args[:player_count]])
  seed_bank_gold(args[:options][:starting_gold_tokens])
end

#token_countObject



41
42
43
# File 'lib/splendor_game/tableau.rb', line 41

def token_count
  @tokens.inject(0) { |sum,(_k,v)| sum + v }
end

#token_space_remainingObject



45
46
47
48
# File 'lib/splendor_game/tableau.rb', line 45

def token_space_remaining
  return nil if @unlimited == true #meaning, undefined
  @token_limit - token_count
end

#tokens_required(card) ⇒ Object

Returns a Hash of the tokens required to buy the card If the tableau does not have sufficient cards/tokens, it returns false



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/splendor_game/tableau.rb', line 82

def tokens_required(card)
  answer = Hash.new(0)
  card.cost.each do |colour, col_cost|
    theoretical_tokens = col_cost - colours_on_cards(colour)
    if theoretical_tokens <= @tokens[colour]
      answer[colour] = theoretical_tokens if theoretical_tokens > 0
    else
      answer[colour] = @tokens[colour]
      answer[:gold] += theoretical_tokens - @tokens[colour]
    end
  end
  return false if answer[:gold] > @tokens[:gold]
  answer
end