Class: DeckObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ttr/objects/deck.rb

Overview

Copyright © 2011 Jesse Sielaff

Instance Method Summary collapse

Constructor Details

#initializeDeckObject

Returns a new instance of DeckObject.



7
8
9
10
11
12
13
14
15
# File 'lib/ttr/objects/deck.rb', line 7

def initialize
  @draw_pile = []
  @selection = []
  @discard_pile = 14.times.map { CardObject.new(:wild) }
  
  %w:black blue green orange pink red white yellow:.each do |color|
    @discard_pile.concat(12.times.map { CardObject.new(color.to_sym) })
  end
end

Instance Method Details

#available_colorsObject

Returns an Array of the colors of the Cards in the selection.



19
20
21
# File 'lib/ttr/objects/deck.rb', line 19

def available_colors
  @selection.map {|card| card.color }
end

#discard(card) ⇒ Object

Moves the given Card to the discard pile.



25
26
27
# File 'lib/ttr/objects/deck.rb', line 25

def discard (card)
  @discard_pile << card
end

#drawObject

Returns the top Card of the Deck, shuffling first if necessary. If there are no Cards left in the Deck, returns nil.



32
33
34
35
36
37
38
39
# File 'lib/ttr/objects/deck.rb', line 32

def draw
  if @draw_pile.empty?
    @draw_pile.replace(@discard_pile.shuffle)
    @discard_pile.clear
  end
  
  @draw_pile.pop
end

#empty?Boolean

Returns true if there are no Cards remaining in the Deck, false otherwise.

Returns:

  • (Boolean)


43
44
45
# File 'lib/ttr/objects/deck.rb', line 43

def empty?
  (@draw_pile + @discard_pile + @selection).empty?
end

#fill_selectionObject

Moves Cards from the draw pile to the selection (shuffling when necessary) until the selection contains 5 Cards, or as many Cards as are left in the Deck. If there are ever 3 wild Cards showing (and at least 3 non-wild cards left in the Deck), moves the selection Cards to the discard pile and attempts to fill the selection again.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ttr/objects/deck.rb', line 53

def fill_selection
  until @selection.length > 4
    return unless card = draw
    @selection << card
    
    if (@selection.count {|card| card.color == :wild } >= 3) && ((@draw_pile + @discard_pile + @selection).count {|card| card.color != :wild } > 2)
      @selection.each {|card| discard(card) }
      @selection.clear
    end
  end
end

#select(color) ⇒ Object

Removes a Card of the given color from the selection, refills the selection, then returns the Card. If no Card of the given color is found, returns nil.



68
69
70
71
72
73
74
# File 'lib/ttr/objects/deck.rb', line 68

def select (color)
  if index = @selection.index {|card| card.exact_match?(color) }
    card = @selection.delete_at(index)
    fill_selection
    card
  end
end

#wilds_only?Boolean

Returns true if the deck is empty except for wild Cards in the selection.

Returns:

  • (Boolean)


78
79
80
# File 'lib/ttr/objects/deck.rb', line 78

def wilds_only?
  (@draw_pile + @discard_pile).empty? && @selection.all? {|card| card.color == :wild }
end