初始化一副牌的更好方法

发布于 2024-10-18 12:35:47 字数 2751 浏览 3 评论 0原文

有没有更简单的方法来执行此操作,而不是手动将每个元素插入数组

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

画骨成沙 2024-10-25 12:35:47

只需循环遍历等级和花色即可。

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

Just loop over both ranks and suits.

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
清风不识月 2024-10-25 12:35:47

是的,有:创建一个面孔数组和一个花色数组,然后在嵌套循环中迭代它们。还要更改 Card 类,以便您不需要将面指定为字符串整数,因为这是多余的。如果只需要指定 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 之前的 ruby​​ 中:

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

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:

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

Or in ruby before 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
烟织青萝梦 2024-10-25 12:35:47

首先:为什么要分别表示卡牌的等级和价值?是否有过这样的情况:例如,Jack具有值 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) }

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

Card.new("7", "Spades", 7)

instead of just

Card.new(7, "Spades")

and is there ever an instance where you would have

Card.new("7", "Spades", 42)

If not, then those two should be packaged together into an object.

Also, why are the suits represented as strings and not as Suits or at least as symbols?

I'd probably do something like this:

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) }
谜兔 2024-10-25 12:35:47

基于 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

Building on Mark Rushakoff solution and using 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
呆头 2024-10-25 12:35:47

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涉及使用一副纸牌来编码消息。查看解决方案。您将看到用于解决此问题的几种不同方法。

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:

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

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.

浅暮の光 2024-10-25 12:35:47
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
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
你不是我要的菜∠ 2024-10-25 12:35:47

这里有两种用 Ruby 制作卡片组的方法。

1. 数组数组

...更容易,但如果您需要相互比较卡片,则可能会更困难(来自 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...

2. 一系列哈希值,其中包含与每张卡相关的数字分数

因此您可以通过“分数”轻松对卡进行排序或比较(由 General Assembly 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...

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)

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...

You can also add on .shuffle or .shuffle! to the deck

deck = values.product(suits).shuffle

=> [[9, "diamonds"], [2, "clubs"], [7, "spades"], [4, "clubs"] #etc...

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)

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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文