是否有更简单的方法来执行此操作,而不是手动将每个元素插入到数组中
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)
答案 0 :(得分:6)
绕过两个级别和套装。
ranks = %w{A 2 3 4 5 6 7 8 9 10 J Q K}
suits = %w{Spades Hearts Diamonds Clubs}
suits.each do |suit|
ranks.size.times do |i|
stack_of_cards << Card.new( ranks[i], suit, i+1 )
end
end
答案 1 :(得分:2)
是的,有:创建一个面数组和一个套装数组,然后在嵌套循环中迭代它们。同时更改Card
类,这样就不需要将face指定为字符串整数,因为这是多余的。如果您只需要指定int参数,那么最方便。
这样代码看起来像这样:
faces = 1..13
suits = %w(Spades Hearts Diamonds Clubs)
cards = suits.flat_map do |suit|
faces.map |face_int_value|
Card.new(suit, face_int_value)
end
end
或者在1.9.2之前的红宝石中:
faces = 1..13
suits = %w(Spades Hearts Diamonds Clubs)
cards = suits.map do |suit|
faces.map |face_int_value|
Card.new(suit, face_int_value)
end
end.flatten
答案 2 :(得分:1)
首先:为什么你要分别代表卡的等级和价值?有没有一个例子,比如杰克不的价值是11?例如,你为什么有
Card.new("7", "Spades", 7)
而不仅仅是
Card.new(7, "Spades")
并且有没有你想要的实例
Card.new("7", "Spades", 42)
如果没有,那么这两个应该被打包成一个对象。
另外,为什么西装表示为字符串而不是Suit
或至少作为符号?
我可能会这样做:
Rank = Struct.new(:rank, :value) do
def to_s; rank end
alias_method :inspect, :to_s
end
Card = Struct.new(:rank, :suit) do
def to_s; "#{rank} of #{suit.capitalize}" end
alias_method :inspect, :to_s
end
ranks = %w[Ace 2 3 4 5 6 7 8 9 10 Jack Queen King].map.with_index {|rank, value|
Rank.new(rank, value + 1)
}
suits = [:spades, :hearts, :diamonds, :clubs]
deck = suits.product(ranks).map {|suit, rank| Card.new(rank, suit) }
答案 3 :(得分:0)
以Mark Rushakoff解决方案为基础并使用Ruby 1.9
Card = Struct.new(:rank, :suit,:rank_id)
ranks = %w{A 2 3 4 5 6 7 8 9 10 J Q K}
suits = %w{Spades Hearts Diamonds Clubs}
stack_of_cards = suits.each_with_object([]) do |suit,res|
ranks.size.times do |i|
res << Card.new(ranks[i], suit,i + 1)
end
end
puts stack_of_cards.inspect
答案 4 :(得分:0)
David A. Black的 The Well Grounded Rubyist 使用套牌初始化作为演示cycle
方法的方法。我认为生成的代码很聪明,非常简单:
class Card
SUITS = %w{ clubs diamonds hearts spades }
RANKS=%w{2345678910JQKA}
class Deck
attr_reader :cards
def initialize(n=1)
@cards = [] SUITS.cycle(n) do |s|
RANKS.cycle(1) do |r| @cards << "#{r} of #{s}"
end
end
end
end
end
此外,Ruby Quiz #1: The Solitaire Cipher涉及使用一副牌来编码消息。查看解决方案。您将看到用于解决此问题的几种不同方法。
答案 5 :(得分:0)
Card = Struct.new(:name, :suit,:number)
stack_of_cards = []
%w{'Spades Hearts Diamonds Clubs'}.each do |suit|
%w{'A 2 3 4 5 6 7 8 9 10 J Q K'}.each_with_index do |name, i|
stack_of_cards << Card.new(name, suit, i+1)
end
end
p stack_of_cards
答案 6 :(得分:0)
以下是使用Ruby制作卡片组的两种方法。
...更容易,但如果您需要相互比较卡片可能会更加困难(来自Tealeaf Academy的礼貌代码项目)
values = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K" ]
suits = [ "hearts", "spades", "clubs", "diamonds" ]
deck = values.product(suits)
=> [["A", "hearts"], ["A", "spades"], ["A", "clubs"], ["A", "diamonds"] #etc...
您还可以添加.shuffle或.shuffle!到甲板
deck = values.product(suits).shuffle
=> [[9, "diamonds"], [2, "clubs"], [7, "spades"], [4, "clubs"] #etc...
...所以你可以通过“得分”轻松地对牌进行排序或比较(在大会DC的@amaseda提供)
def deck_o_cards
values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
suits = ['hearts', 'diamonds', 'clubs', 'spades']
deck = []
values.each_with_index do |v, i|
suits.each do |s|
deck.push({
score: i,
value: v,
suit: s,
})
end
end
return deck.shuffle
end
=> [{:score=>8, :value=>10, :suit=>"hearts"}, {:score=>4, :value=>6, :suit=>"diamonds"}, #etc...