1

I am having a little trouble getting my code to run and have been at it for at least 4 hours...I can't seem to figure it out. BTW I am a newb at programming.

This is the UML diagram for the card/deck https://i.stack.imgur.com/CyTA5.jpg

class Card
  #Cards rank from lowest to highest
    VALUE = %w(2 3 4 5 6 7 8 9 10 J Q K A)
  SUITS = %w(C D H S)
  #(Club Diamond Heart Spade)

 def initialize(the_rank, the_suit)
    [@rank = the_rank]
    [@suit = the_suit]
    [@symbols = [nil, nil, '2', '3', '4', '5', '6', '7', 
                '8', '9', '10', 'J', 'Q', 'K', 'A']
  end

  def rank
    [return @symbols[@rank]]
  end

  def suit
    return @suit
  end

  def to_s
    "#{rank()}#{suit()}"
  end
end

  #double loop for Deck#initialize
  @cards = [ ]
    for rank in 2..14
    for suit in ['C', 'D', 'H', 'S']
    # create a new card with the specified rank
    # and suit and append it to the array.   
    end
  end 
  suits.each do |suit|
      (ranks.size).times do |i|
        @cards.push(Card.new(ranks[i], suit,(i+1)))
      end
    end

  #Remove a card from the top
  def deal
    [@cards.pop()](1)
  end
  #Add card to the bottom of the deck
  def add_to_bottom(the_card)
    @cards.insert(0, the_card)
  end
   #Add card to the top of the deck
  def add_to_top(the_card)
    @cards << the_card
  end

 #Shuffle the Card objects in the deck
  def shuffle!
    @cards.shuffle!
  end

    def count()
    @cards.count()
  end

   def empty?
    @cards.length == 0
  end

  def to_s
    string = ""
    @cards.each do |card|
      string += card.to_s + " "
    end
  end

  def cards
    @cards
  end
end
reedstonefood
  • 131
  • 2
  • 15

2 Answers2

0

I haven't tested this very thoroughly, but it basically implements the UML diagram you linked to:

class Deck

  def initialize
    @ranks = %w(2 3 4 5 6 7 8 9 10 J Q K A)
    @suits = %w(Clubs Diamonds Hearts Spades)
    @cards = []

    @ranks.each do |rank|
      @suits.each do |suit|
        @cards << Card.new(rank, suit)
      end
    end
  end

  def deal
    @cards.shift
  end

  def add_to_bottom(card)
    @cards.unshift(card)
  end

  def add_to_top(card)
    @cards << card
  end

  def count
    @cards.count
  end

  def empty?
    @cards.empty?
  end

  def shuffle!
    @cards.shuffle
  end

  def to_s
    result = ''
    @cards.each do |card|
      result = result + card.to_s + "\n"
    end
    return result
  end

end


class Card
  attr_reader :rank, :suit, :color
  def initialize(rank, suit)
    @rank = rank
    @suit = suit
    if @rank == 'Clubs' || @rank == 'Spades'
      @color = 'black'
    else
      @color = 'red'
    end
  end

  def to_s
    "Card: #{@color} #{@rank} of #{@suit}"
  end

end

# Run it, and try some stuff...
my_deck = Deck.new

puts my_deck.to_s
my_deck.deal
my_deck.count
my_deck.shuffle!
puts my_deck.to_s
my_deck.deal
my_deck.count
joshua.paling
  • 13,762
  • 4
  • 45
  • 60
0

Now I am trying to figure how the testing method. I have the first working

d = Deck.new
d.cards.each do |card|
  puts "#{card.rank} #{card.suit}"
end

And the second method that needs to test: tests both the Card and Deck classes using the unit test framework with assert_equal methods. Write unit tests for all methods in the Card class, but you only have to test these methods in the Deck class: new, deal, add_to_bottom, add_to_top, count, empty?

  • What's the context for this exercise? Are you doing it as part of a class / course? I think you'd benefit a lot from some face-to-face time with an experienced developer. Lots of big cities have Ruby meetups where you can do this. – joshua.paling Oct 14 '14 at 22:30
  • Where abouts (city / country) are you located? – joshua.paling Oct 14 '14 at 22:34