Class: Deck

Inherits:
Object show all
Defined in:
lib/crystel.rb

Overview

deckcrystel

Direct Known Subclasses

CribbageDeck, EukerDeck, ExplodingDeck

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeck

Returns a new instance of Deck.



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/crystel.rb', line 177

def initialize
  @position = []
  @groups = []
  ["c", "h", "s", "d"].each do |suit|
    for card in 2..10
      @position.push("#{card}#{suit}")
    end
    ["a", "j", "q", "k"].each do |card|
      @position.push("#{card}#{suit}")
    end
  end
  shuffle
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



164
165
166
# File 'lib/crystel.rb', line 164

def groups
  @groups
end

#positionObject

Returns the value of attribute position.



163
164
165
# File 'lib/crystel.rb', line 163

def position
  @position
end

Instance Method Details

#card_at(card) ⇒ Object



190
191
192
# File 'lib/crystel.rb', line 190

def card_at(card)
  @position[card]
end

#combineObject



212
213
214
215
216
# File 'lib/crystel.rb', line 212

def combine
  @position.push(@groups.flatten).flatten!
  @groups = []
  self
end

#deal(num_cards, num_hands) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/crystel.rb', line 203

def deal(num_cards, num_hands)
  for i in 0...num_hands
    @groups.push(top(num_cards))
    for x in 0...num_cards
      @position.shift
    end
  end
  self
end

#deal_inObject



217
218
219
# File 'lib/crystel.rb', line 217

def deal_in
  combine
end

#group_no(group, top_no, place, placein = 1) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/crystel.rb', line 220

def group_no(group, top_no, place, placein=1)
  if (1..@groups.length).include?(place)
    @groups[place-1].insert(placein-1, @groups[group-1][0...top_no]).flatten!
    @groups[group-1][0...top_no].each do |card|
      @groups[group-1].delete(card)
    end
  else
    @position.insert(placein-1, @groups[group-1][0...top_no]).flatten!
    @groups[group-1][0...top_no].each do |card|
      @groups[group-1].delete(card)
    end
  end
end

#index_of(card) ⇒ Object



193
194
195
# File 'lib/crystel.rb', line 193

def index_of(card)
  @position.index(card)
end

#shuffleObject



171
172
173
174
175
176
# File 'lib/crystel.rb', line 171

def shuffle
  for x in 0..200
    swap
  end
  self
end

#swap(one = rand(@position.size), two = rand(@position.size)) ⇒ Object



165
166
167
168
169
170
# File 'lib/crystel.rb', line 165

def swap(one=rand(@position.size), two=rand(@position.size))
  swap = @position[one]
  @position[one] = @position[two]
  @position[two] = swap
  self
end

#top(number) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/crystel.rb', line 196

def top(number)
  if number > @position.length + 1
    @position
  else
    @position[0...number]
  end
end