初始化一副牌的更好方法
有没有更简单的方法来执行此操作,而不是手动将每个元素插入数组
stack_of_cards << Card.new("A", "Spades", 1)
stack_of_cards << Card.new("2", "Spades", 2)
stack_of_cards << Card.new("3", "Spades", 3)
stack_of_cards << Card.new("4", "Spades", 4)
stack_of_cards << Card.new("5", "Spades", 5)
stack_of_cards << Card.new("6", "Spades", 6)
stack_of_cards << Card.new("7", "Spades", 7)
stack_of_cards << Card.new("8", "Spades", 8)
stack_of_cards << Card.new("9", "Spades", 9)
stack_of_cards << Card.new("10", "Spades", 10)
stack_of_cards << Card.new("J", "Spades", 11)
stack_of_cards << Card.new("Q", "Spades", 12)
stack_of_cards << Card.new("K", "Spades", 13)
stack_of_cards << Card.new("A", "Hearts", 1)
stack_of_cards << Card.new("2", "Hearts", 2)
stack_of_cards << Card.new("3", "Hearts", 3)
stack_of_cards << Card.new("4", "Hearts", 4)
stack_of_cards << Card.new("5", "Hearts", 5)
stack_of_cards << Card.new("6", "Hearts", 6)
stack_of_cards << Card.new("7", "Hearts", 7)
stack_of_cards << Card.new("8", "Hearts", 8)
stack_of_cards << Card.new("9", "Hearts", 9)
stack_of_cards << Card.new("10", "Hearts", 10)
stack_of_cards << Card.new("J", "Hearts", 11)
stack_of_cards << Card.new("Q", "Hearts", 12)
stack_of_cards << Card.new("K", "Hearts", 13)
stack_of_cards << Card.new("A", "Diamonds", 1)
stack_of_cards << Card.new("2", "Diamonds", 2)
stack_of_cards << Card.new("3", "Diamonds", 3)
stack_of_cards << Card.new("4", "Diamonds", 4)
stack_of_cards << Card.new("5", "Diamonds", 5)
stack_of_cards << Card.new("6", "Diamonds", 6)
stack_of_cards << Card.new("7", "Diamonds", 7)
stack_of_cards << Card.new("8", "Diamonds", 8)
stack_of_cards << Card.new("9", "Diamonds", 9)
stack_of_cards << Card.new("10", "Diamonds", 10)
stack_of_cards << Card.new("J", "Diamonds", 11)
stack_of_cards << Card.new("Q", "Diamonds", 12)
stack_of_cards << Card.new("K", "Diamonds", 13)
stack_of_cards << Card.new("A", "Clubs", 1)
stack_of_cards << Card.new("2", "Clubs", 2)
stack_of_cards << Card.new("3", "Clubs", 3)
stack_of_cards << Card.new("4", "Clubs", 4)
stack_of_cards << Card.new("5", "Clubs", 5)
stack_of_cards << Card.new("6", "Clubs", 6)
stack_of_cards << Card.new("7", "Clubs", 7)
stack_of_cards << Card.new("8", "Clubs", 8)
stack_of_cards << Card.new("9", "Clubs", 9)
stack_of_cards << Card.new("10", "Clubs", 10)
stack_of_cards << Card.new("J", "Clubs", 11)
stack_of_cards << Card.new("Q", "Clubs", 12)
stack_of_cards << Card.new("K", "Clubs", 13)
Is there an easier way of doing this instead of inserting each element into the array manually
stack_of_cards << Card.new("A", "Spades", 1)
stack_of_cards << Card.new("2", "Spades", 2)
stack_of_cards << Card.new("3", "Spades", 3)
stack_of_cards << Card.new("4", "Spades", 4)
stack_of_cards << Card.new("5", "Spades", 5)
stack_of_cards << Card.new("6", "Spades", 6)
stack_of_cards << Card.new("7", "Spades", 7)
stack_of_cards << Card.new("8", "Spades", 8)
stack_of_cards << Card.new("9", "Spades", 9)
stack_of_cards << Card.new("10", "Spades", 10)
stack_of_cards << Card.new("J", "Spades", 11)
stack_of_cards << Card.new("Q", "Spades", 12)
stack_of_cards << Card.new("K", "Spades", 13)
stack_of_cards << Card.new("A", "Hearts", 1)
stack_of_cards << Card.new("2", "Hearts", 2)
stack_of_cards << Card.new("3", "Hearts", 3)
stack_of_cards << Card.new("4", "Hearts", 4)
stack_of_cards << Card.new("5", "Hearts", 5)
stack_of_cards << Card.new("6", "Hearts", 6)
stack_of_cards << Card.new("7", "Hearts", 7)
stack_of_cards << Card.new("8", "Hearts", 8)
stack_of_cards << Card.new("9", "Hearts", 9)
stack_of_cards << Card.new("10", "Hearts", 10)
stack_of_cards << Card.new("J", "Hearts", 11)
stack_of_cards << Card.new("Q", "Hearts", 12)
stack_of_cards << Card.new("K", "Hearts", 13)
stack_of_cards << Card.new("A", "Diamonds", 1)
stack_of_cards << Card.new("2", "Diamonds", 2)
stack_of_cards << Card.new("3", "Diamonds", 3)
stack_of_cards << Card.new("4", "Diamonds", 4)
stack_of_cards << Card.new("5", "Diamonds", 5)
stack_of_cards << Card.new("6", "Diamonds", 6)
stack_of_cards << Card.new("7", "Diamonds", 7)
stack_of_cards << Card.new("8", "Diamonds", 8)
stack_of_cards << Card.new("9", "Diamonds", 9)
stack_of_cards << Card.new("10", "Diamonds", 10)
stack_of_cards << Card.new("J", "Diamonds", 11)
stack_of_cards << Card.new("Q", "Diamonds", 12)
stack_of_cards << Card.new("K", "Diamonds", 13)
stack_of_cards << Card.new("A", "Clubs", 1)
stack_of_cards << Card.new("2", "Clubs", 2)
stack_of_cards << Card.new("3", "Clubs", 3)
stack_of_cards << Card.new("4", "Clubs", 4)
stack_of_cards << Card.new("5", "Clubs", 5)
stack_of_cards << Card.new("6", "Clubs", 6)
stack_of_cards << Card.new("7", "Clubs", 7)
stack_of_cards << Card.new("8", "Clubs", 8)
stack_of_cards << Card.new("9", "Clubs", 9)
stack_of_cards << Card.new("10", "Clubs", 10)
stack_of_cards << Card.new("J", "Clubs", 11)
stack_of_cards << Card.new("Q", "Clubs", 12)
stack_of_cards << Card.new("K", "Clubs", 13)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只需循环遍历等级和花色即可。
Just loop over both ranks and suits.
是的,有:创建一个面孔数组和一个花色数组,然后在嵌套循环中迭代它们。还要更改
Card
类,以便您不需要将面指定为字符串整数,因为这是多余的。如果只需要指定 int 参数是最方便的。这样代码将如下所示:
或者在 1.9.2 之前的 ruby 中:
Yes, there is: Create an array of faces and an array of suits then iterate over them in a nested loop. Also change the
Card
class so that you don't need to specify the face as a string an integer as that is redundant. It's most convenient if you only need to specify the int parameter.This way the code would look like this:
Or in ruby before 1.9.2:
首先:为什么要分别表示卡牌的等级和价值?是否有过这样的情况:例如,Jack不具有值 11?例如,为什么你有
而不是只是
并且是否有一个实例你会拥有
如果没有,那么这两个应该一起打包到一个对象中。
另外,为什么花色表示为字符串而不是
Suit
或至少表示为符号?我可能会做这样的事情:
First off: why do you represent the rank and the value of a card separately? Is there ever an instance where, say, a Jack does not have the value 11? For example, why do you have
instead of just
and is there ever an instance where you would have
If not, then those two should be packaged together into an object.
Also, why are the suits represented as strings and not as
Suit
s or at least as symbols?I'd probably do something like this:
基于 Mark Rushakoff 解决方案并使用 Ruby 1.9
Building on Mark Rushakoff solution and using Ruby 1.9
David A. Black 的The Well Grounded Rubyist使用牌组初始化作为一种方式演示
cycle
方法。我认为生成的代码很聪明,而且非常简单:此外,Ruby Quiz #1: The Solitaire Cipher涉及使用一副纸牌来编码消息。查看解决方案。您将看到用于解决此问题的几种不同方法。
David A. Black's The Well Grounded Rubyist uses deck initialization as a way to demonstrate the
cycle
method. I think the resulting code is clever, and very simple:Also, the Ruby Quiz #1: The Solitaire Cipher involved using a deck of cards to encode a message. Check out the solutions. You'll see a few different ways used to tackle this problem.
这里有两种用 Ruby 制作卡片组的方法。
1. 数组数组
...更容易,但如果您需要相互比较卡片,则可能会更困难(来自 Tealeaf Academy 的代码项目)
您还可以添加 .shuffle 或 .shuffle! ...
2. 一系列哈希值,其中包含与每张卡相关的数字分数
因此您可以通过“分数”轻松对卡进行排序或比较(由 General Assembly DC 的 @amaseda 提供)
Here are two ways to make a card deck in Ruby.
1. An array of arrays
...easier, but may be tougher if you need to compare cards against each other (courtesy code project from Tealeaf Academy)
You can also add on .shuffle or .shuffle! to the deck
2. An array of hashes with numerical scores associated with each card
...so you can easily sort or compare cards via "scores" (courtesy @amaseda at General Assembly DC)