ga-wdi-boston / ruby-enumerable-comparable

Other
1 stars 139 forks source link

Deck needs an each method (solution branch) #19

Closed raq929 closed 7 years ago

raq929 commented 7 years ago

Likely similar to: https://stackoverflow.com/questions/7220634/how-do-i-use-the-enumerable-mixin-in-my-class

raq929 commented 7 years ago

@jrhorn424 can probably say more about what he's looking for here.

jrhorn424 commented 7 years ago
# frozen_string_literal: true

require_relative 'card'

# A simple represenation of a storage of playing cards
class Deck
  include Enumerable

  def each
    storage.each do |card|
      yield card
    end
  end

  attr_reader :storage
  private :storage

  def initialize
    @storage = Card::SUITS.map do |suit|
      Card::RANKS.map { |rank| Card.new(rank, suit) }
    end.flatten
  end

  # swap front and back somewhere in the middle third.
  def cut
    count = storage.length
    random = Random.rand(count / 3)
    cut_point = (count / 3 + random)
    storage.replace(
      storage.slice(cut_point, count - cut_point) + storage.slice(0, cut_point)
    )
    self
  end

  def draw
    storage.shift
  end

  def shuffle
    storage.shuffle!
    self
  end

  def deal(cards, *hands); end
end
jrhorn424 commented 7 years ago

Done as of https://github.com/ga-wdi-boston/ruby-enumerable-comparable/commit/60983593d891c7ef6e63cebb8254387e15885f1e