编程技术:如何创建一个简单的纸牌游戏纸牌、创建一个、编程技术、简单

2023-09-11 00:28:09 作者:信仰之名

因为我学习Ruby语言,我越来越接近实际的编程。我想创建一个简单的纸牌游戏。我的问题是不是红宝石为主,但我知道要学习如何解决这个问题,一个真正的面向对象的方法。在我的卡牌游戏,我想有四个玩家,使用标准的甲板52张牌,没有王牌/通配符。在游戏中,我也不会用王牌作为一款双卡,它始终是最高的卡。

as I am learning the Ruby language, I am getting closer to actual programming. I was thinking of creating a simple card game. My question isn't Ruby oriented, but I do know want to learn how to solve this problem with a genuine OOP approach. In my card game, I want to have four players, using a standard deck with 52 cards, no jokers/wildcards. In the game, I won't use the ace as a dual card, it is always the highest card.

那么,我想知道编程问题如下:

So, the programming problems I wonder about are the following:

我如何排序/随机的扑克牌?有四种类型,每一个具有13个值。最终只能有唯一值,所以挑选随机值可能会产生重复。

How can I sort/randomize the deck of cards? There are four types, each having 13 values. Eventually there can be only unique values, so picking random values could generate duplicates.

我如何实现一个简单的AI?由于有吨的纸牌游戏,有人会想通这部分出来了,所以引用将是巨大的。

How can I implement a simple AI? As there are tons of card games, someone would have figured this part out already, so references would be great.

我是一个真正的红宝石NUBY,在这里我的目标是要学会解决问题,所以伪code将是巨大的,只是为了了解如何以编程方式解决这个问题。我为我的语法和写作风格道歉,如果现在还不清楚,因为它不是我的母语。

I am a true Ruby nuby, and my goal here is to learn to solve problems, so pseudo code would be great, just to understand how to solve the problem programmatically. I apologize for my grammar and writing style if it's unclear, for it is not my native language.

此外,指向网站,这些挑战解释将是一个巨大的资源!

Also, pointers to sites where such challenges are explained would be a great resource!

感谢您的意见,解答和反馈!

Thank you for your comments, answers and feedback!

推荐答案

您可以确保独特的卡很容易被利用的数字从0到51。

Something to get you started

You can ensure unique cards very easily by using numbers from 0 to 51.

阵列#洗牌方法是基于关闭高德纳 - 费雪耶茨洗牌算法。 http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

The Array#shuffle method is based off the Knuth-Fisher-Yates shuffle algorithm. http://en.wikipedia.org/wiki/Fisher–Yates_shuffle

class Card
  RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A)
  SUITS = %w(Spade Heart Club Diamond)

  attr_accessor :rank, :suit

  def initialize(id)
    self.rank = RANKS[id % 13]
    self.suit = SUITS[id % 4]
  end
end

class Deck
  attr_accessor :cards
  def initialize
    # shuffle array and init each Card
    self.cards = (0..51).to_a.shuffle.collect { |id| Card.new(id) }
  end
end

# people with Ruby 1.9 (or 1.8.7 with backports) can safely ignore this duck punch
class Array
  # knuth-fisher-yates shuffle algorithm
  def shuffle!
    n = length
    for i in 0...n
      r = rand(n-i)+i
      self[r], self[i] = self[i], self[r]
    end
    self
  end
  def shuffle
    dup.shuffle!
  end
end

测试

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

输出

6 Spade
5 Heart
2 Heart
8 Heart
8 Diamond
7 Club
J Diamond
4 Club
K Spade
5 Diamond
J Heart
8 Spade
10 Club
4 Diamond
9 Heart
7 Diamond
3 Diamond
K Diamond
7 Spade
Q Diamond
9 Diamond
6 Heart
A Heart
9 Club
A Spade
5 Club
J Club
Q Spade
2 Club
2 Spade
Q Heart
A Diamond
10 Spade
10 Diamond
Q Club
3 Club
A Club
K Club
6 Club
10 Heart
2 Diamond
3 Spade
K Heart
5 Spade
9 Spade
7 Heart
4 Spade
J Spade
3 Heart
4 Heart
8 Club
6 Diamond