【问题标题】:Implement and test Card and Deck classes in Ruby在 Ruby 中实现和测试 Card 和 Deck 类
【发布时间】:2014-12-08 23:16:19
【问题描述】:

我在运行我的代码时遇到了一些麻烦,并且已经运行了至少 4 个小时......我似乎无法弄清楚。顺便说一句,我是编程新手。

这是卡片/卡片组的 UML 图 http://imgur.com/lBJw2z0

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

【问题讨论】:

  • 为了将来参考,不需要在stackoverflow的标签中包装代码示例——只要确保所有代码都缩进4个空格。
  • 这个问题的背景是什么?到目前为止,您是否设法使任何工作正常进行?你到底卡在哪里了?最终目标是什么?
  • 好的,谢谢,下次我会记住的。

标签: ruby


【解决方案1】:

我没有对此进行过彻底的测试,但它基本上实现了您链接到的 UML 图:

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

【讨论】:

  • 你的代码实际上看起来比我的更干净,更有意义!
  • 现在我想弄清楚测试方法如何。我有第一个工作
  • 老实说,通过自己的努力,你会学到很多东西。
  • my_deck.shuffle! 不会改变 my_deck。
  • 您使用的是非常旧的 Ruby 版本吗?有关更多信息,请参阅此问题:stackoverflow.com/questions/1816378/…
【解决方案2】:

现在我想弄清楚测试方法如何。我有第一个工作

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

还有第二种需要测试的方法: 使用带有 assert_equal 方法的单元测试框架测试 Card 和 Deck 类。为 Card 类中的所有方法编写单元测试,但您只需在 Deck 类中测试这些方法:new、deal、add_to_bottom、add_to_top、count、empty?

【讨论】:

  • 这个练习的背景是什么?您是否将其作为课程/课程的一部分?我认为你会从与经验丰富的开发人员面对面的时间中受益匪浅。许多大城市都有 Ruby 聚会,您可以在其中进行此操作。
  • 你在哪里(城市/国家)?
猜你喜欢
  • 2021-02-07
  • 2016-02-13
  • 1970-01-01
  • 2012-05-04
  • 2011-08-23
  • 2013-02-09
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多