Class: SplendorGame::Turn

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, player) ⇒ Turn

Returns a new instance of Turn.



6
7
8
9
10
11
# File 'lib/splendor_game/turn.rb', line 6

def initialize(game, player)
  @game = game
  @player = player
  @action_done = false
  @noble_claimed = false
end

Instance Attribute Details

#action_doneObject (readonly)

Returns the value of attribute action_done.



4
5
6
# File 'lib/splendor_game/turn.rb', line 4

def action_done
  @action_done
end

#playerObject (readonly)

Returns the value of attribute player.



4
5
6
# File 'lib/splendor_game/turn.rb', line 4

def player
  @player
end

Instance Method Details

#action_return_token(colour) ⇒ Object

in order to claim tokens you may need to return tokens, eg pick up a gold when you have 10, or you have 8 and want to take 3 different.



107
108
109
110
111
112
# File 'lib/splendor_game/turn.rb', line 107

def action_return_token(colour)
  return false if !@player.tableau.tokens.include?(colour) 
  return false if @player.tableau.tokens[colour] <= 0
  @player.tableau.remove_token(colour)
  @game.bank.add_token(colour)
end

#affordable_cardsObject

from the 12 cards on display plus up to 3 reserved ones, return the ones affordable, in an Array



14
15
16
17
18
19
20
21
# File 'lib/splendor_game/turn.rb', line 14

def affordable_cards
  answer = Array.new()
  (@game.all_displayed_cards + @player.tableau.reserved_cards).each do |card|
    @cost = @player.tableau.tokens_required(card)
    answer << card if !@cost==false
  end
  answer
end

#claim_noble(noble) ⇒ Object

Move noble from bank to player, assuming it’s affordable



114
115
116
117
118
119
120
# File 'lib/splendor_game/turn.rb', line 114

def claim_noble(noble) # Move noble from bank to player, assuming it's affordable
  return false if @noble_claimed # you can only claim 1 per turn
  return false if !noble_affordable?(noble)
  @game.nobles.delete_at(@game.nobles.index(noble) || display_row.length)
  @player.nobles << noble
  @noble_claimed = true
end

#claimable_noblesObject



122
123
124
# File 'lib/splendor_game/turn.rb', line 122

def claimable_nobles
  @game.nobles.find_all { |noble| noble_affordable?(noble) }
end

#end_turnObject

make sure there are 4 cards showing from each deck, assuming there are any left



133
134
135
136
137
138
139
140
# File 'lib/splendor_game/turn.rb', line 133

def end_turn # make sure there are 4 cards showing from each deck, assuming there are any left
  #assumption : max 1 card is missing from each row
  nonfull_rows = @game.display.select { |_level, subdeck| subdeck.count < @game.options[:display_cards_per_row] }
  nonfull_rows.each do |row_num, display_row|
    relevant_deck = @game.deck[row_num]
    display_row << relevant_deck.pop if relevant_deck.count > 0
  end
end

#give_gold_tokenObject



47
48
49
50
51
52
# File 'lib/splendor_game/turn.rb', line 47

def give_gold_token
  if @game.bank.tokens[:gold] > 0 && @player.tableau.token_space_remaining > 0
    @game.bank.remove_token(:gold)
    @player.tableau.add_token(:gold)
  end
end

#noble_affordable?(noble) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'lib/splendor_game/turn.rb', line 126

def noble_affordable?(noble)
  noble.cost.each do |colour, cost|
    return false if @player.tableau.colours_on_cards(colour) < cost
  end
  true
end

#purchase_card(card) ⇒ Object

do the requisite card/token changes, if the card is in affordable_cards



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/splendor_game/turn.rb', line 23

def purchase_card(card) # do the requisite card/token changes, if the card is in affordable_cards
  return false if @action_done
  return false if !affordable_cards.include?(card)
  cost_to_player = @player.tableau.tokens_required(card)
  if @player.tableau.reserved_cards.include?(card)
    @player.tableau.play_reserved_card(card)
  else
    @player.tableau.purchase_card(card)
  end
  cost_to_player.each do |colour, val|
    val.times { @player.tableau.remove_token(colour) }
    val.times { @game.bank.add_token(colour) }
  end
  display_row = @game.display[card.level]
  display_row.delete_at(display_row.index(card) || display_row.length)
  @action_done = true
end

#reserve_card_checks?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/splendor_game/turn.rb', line 41

def reserve_card_checks?
  return false if @action_done
  return false if !@player.tableau.can_reserve_card?
  true
end

#reserve_displayed_card(card) ⇒ Object

put cards in to player’s reserved set. Remove card from display



54
55
56
57
58
59
60
61
62
# File 'lib/splendor_game/turn.rb', line 54

def reserve_displayed_card(card) # put cards in to player's reserved set. Remove card from display
  return false if !reserve_card_checks?
  return false if !@game.display.flatten(2).include?(card)
  @player.tableau.reserve_card(card)
  display_row = @game.display[card.level]
  display_row.delete_at(display_row.index(card) || display_row.length)
  give_gold_token
  @action_done = true
end

#reserve_random_card(deck_number) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/splendor_game/turn.rb', line 64

def reserve_random_card(deck_number)
  return false if !reserve_card_checks?
  return false if @game.deck[deck_number].nil?
  return false if @game.deck[deck_number].count==0
  card = @game.deck[deck_number].pop
  @player.tableau.reserve_card(card)
  give_gold_token
  @action_done = true
end

#take_different_tokens(colours) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/splendor_game/turn.rb', line 90

def take_different_tokens(colours)
  colours.map!{|c| c.to_sym}
  return false if !validate_token_pickup?(colours)
  return false if colours.count != 3
  return false if colours.uniq.length != colours.length
  return false if colours.select { |c| @game.bank.tokens[c]==0}.length > 0
  return false if @player.tableau.token_space_remaining < 3
  colours.each do |c|
    @player.tableau.add_token(c)
    @game.bank.remove_token(c)
  end
  @action_done = true
end

#take_two_tokens_same_colour(colour) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/splendor_game/turn.rb', line 80

def take_two_tokens_same_colour(colour)
  colour = colour.to_sym
  return false if !validate_token_pickup?(colour)
  return false if @game.bank.tokens[colour] < @game.options[:min_to_take_two]
  return false if @player.tableau.token_space_remaining < 2
  2.times { @player.tableau.add_token(colour) }
  2.times { @game.bank.remove_token(colour) }
  @action_done = true
end

#validate_token_pickup?(colour) ⇒ Boolean

Returns:

  • (Boolean)


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

def validate_token_pickup?(colour)
  return false if @action_done
  return false if Array(colour).include?(:gold)
  true
end