Ruby 在纸牌游戏中比较价值

Ruby 在纸牌游戏中比较价值,ruby,Ruby,我想制作一个纸牌游戏程序,比较分配给player\u value和dealer\u value的纸牌值。如果玩家\u值大于经销商\u值,则应显示“您赢了”。这是我的密码: def get_card (card) type = case ((card-1)/13) when 0 then "of clubs" when 1 then "of diamonds" when 2 then "of hearts" when 3 then "of spades" end car

我想制作一个纸牌游戏程序,比较分配给
player\u value
dealer\u value
的纸牌值。如果
玩家\u值
大于
经销商\u值
,则应显示
“您赢了”
。这是我的密码:

def get_card (card)
  type = case ((card-1)/13)
  when 0 then "of clubs"
  when 1 then "of diamonds"
  when 2 then "of hearts"
  when 3 then "of spades"
  end
  card = case (card%13)
  when 0 then "king #{type}"
  when 1 then "ace #{type}"                 
  when 11 then "jack #{type}"
  when 12 then "queen #{type}"
  else card%13
  end
  "#{card} #{type}"
end

def deal_cards
  total_cards = (1..52).to_a.shuffle
  player_value = [total_cards.pop, total_cards.pop]
  dealer_value = [total_cards.pop, total_cards.pop]
  puts "Your cards are #{get_card(player_value[0]).to_s} and #{get_card(player_value[1]).to_s}"
  puts "The dealer shows #{get_card(dealer_value[0])}"

  if(dealer_value > player_value)
    puts "You lose"
  else (player_value > dealer_value)
    puts "You win"
  end
  end  

deal_cards()

我不清楚这为什么不起作用,我希望能得到任何帮助。

我真的不明白为什么要将数组分配给
player\u value
dealer\u value
,但是你不能用
来比较数组。我真的不明白为什么你要把数组分配给
玩家值
庄家值
,但是你不能用
来比较数组。让我为同样的问题提供这个面向对象的解决方案,因为这是Ruby真正的亮点,看到它以程序化的方式被使用,我真的很恼火。面向对象为脚手架增加了更多的线条,但在易读性、可重用性和概念清晰性方面增加了更多


我们可以使用三个基本构建块来表示域

首先,我们需要一个
Card
对象,它能够保存关于自身的一些数据(等级和套装),并且能够将自身表示为字符串:

class Card
  SUITS = [:clubs, :diamonds, :spades, :hearts]
  RANKS = [:ace, *2..10, :jack, :queen, :king]

  attr_reader :suit, :rank

  def initialize(n)
    @suit = (n - 1) / 13
    @rank = (n - 1) % 13
  end

  def to_s
    "#{ RANKS[@rank] } of #{ SUITS[@suit] }"
  end
end
class Hand

  attr_reader :cards

  def initialize(cards)
    @cards = cards
  end

  def <=>(other_hand)
    @cards.strength <=> other_hand.strength
  end

  def to_s
    @cards.map(&:to_s).join(", ")
  end

  private

  def strength
    @cards.map(&:rank).inject(:+)
  end
end

接下来,我们需要一个
Hand
对象。基本上是一组卡片,可以将其强度与其他手牌进行比较,也可以将其自身表示为一根弦:

class Card
  SUITS = [:clubs, :diamonds, :spades, :hearts]
  RANKS = [:ace, *2..10, :jack, :queen, :king]

  attr_reader :suit, :rank

  def initialize(n)
    @suit = (n - 1) / 13
    @rank = (n - 1) % 13
  end

  def to_s
    "#{ RANKS[@rank] } of #{ SUITS[@suit] }"
  end
end
class Hand

  attr_reader :cards

  def initialize(cards)
    @cards = cards
  end

  def <=>(other_hand)
    @cards.strength <=> other_hand.strength
  end

  def to_s
    @cards.map(&:to_s).join(", ")
  end

  private

  def strength
    @cards.map(&:rank).inject(:+)
  end
end

现在我们已经建立了基本的构建块,使用它们是很简单的:

def deal_cards
  deck = Deck.new

  player_hand = Hand.new(deck.draw(2))
  dealer_hand = Hand.new(deck.draw(2))

  puts "Your have: #{ player_hand }"
  puts "The dealer has: #{ dealer_hand }"

  if(player_hand > dealer_hand)
    puts "You win!"
  elsif(dealer_hand < player_hand)
    puts "Aw. You lose."
  else
    puts "Woah! It's a tie!"
  end
end
def交易卡
甲板
玩家手=手。新(牌组。抽签(2))
经销商手=手。新(甲板图(2))
放上“你的手:{player_hand}”
放置“经销商有:#{dealer_hand}”
如果(玩家手>庄家手)
写上“你赢了!”
elsif(庄家手<玩家手)
写着“啊,你输了。”
其他的
写着“哇!是领带!”
结束
结束

值得注意的是,此解决方案缺少错误处理,例如将未知的
n
传递给
构造函数,或
从空卡片中绘制,但可以很容易地添加进去。

让我为同样的问题提供此面向对象的解决方案,因为这是Ruby真正的亮点,看到它以程序化的方式被使用,我真的很恼火。面向对象为脚手架增加了更多的线条,但在易读性、可重用性和概念清晰性方面增加了更多


我们可以使用三个基本构建块来表示域

首先,我们需要一个
Card
对象,它能够保存关于自身的一些数据(等级和套装),并且能够将自身表示为字符串:

class Card
  SUITS = [:clubs, :diamonds, :spades, :hearts]
  RANKS = [:ace, *2..10, :jack, :queen, :king]

  attr_reader :suit, :rank

  def initialize(n)
    @suit = (n - 1) / 13
    @rank = (n - 1) % 13
  end

  def to_s
    "#{ RANKS[@rank] } of #{ SUITS[@suit] }"
  end
end
class Hand

  attr_reader :cards

  def initialize(cards)
    @cards = cards
  end

  def <=>(other_hand)
    @cards.strength <=> other_hand.strength
  end

  def to_s
    @cards.map(&:to_s).join(", ")
  end

  private

  def strength
    @cards.map(&:rank).inject(:+)
  end
end

接下来,我们需要一个
Hand
对象。基本上是一组卡片,可以将其强度与其他手牌进行比较,也可以将其自身表示为一根弦:

class Card
  SUITS = [:clubs, :diamonds, :spades, :hearts]
  RANKS = [:ace, *2..10, :jack, :queen, :king]

  attr_reader :suit, :rank

  def initialize(n)
    @suit = (n - 1) / 13
    @rank = (n - 1) % 13
  end

  def to_s
    "#{ RANKS[@rank] } of #{ SUITS[@suit] }"
  end
end
class Hand

  attr_reader :cards

  def initialize(cards)
    @cards = cards
  end

  def <=>(other_hand)
    @cards.strength <=> other_hand.strength
  end

  def to_s
    @cards.map(&:to_s).join(", ")
  end

  private

  def strength
    @cards.map(&:rank).inject(:+)
  end
end

现在我们已经建立了基本的构建块,使用它们是很简单的:

def deal_cards
  deck = Deck.new

  player_hand = Hand.new(deck.draw(2))
  dealer_hand = Hand.new(deck.draw(2))

  puts "Your have: #{ player_hand }"
  puts "The dealer has: #{ dealer_hand }"

  if(player_hand > dealer_hand)
    puts "You win!"
  elsif(dealer_hand < player_hand)
    puts "Aw. You lose."
  else
    puts "Woah! It's a tie!"
  end
end
def交易卡
甲板
玩家手=手。新(牌组。抽签(2))
经销商手=手。新(甲板图(2))
放上“你的手:{player_hand}”
放置“经销商有:#{dealer_hand}”
如果(玩家手>庄家手)
写上“你赢了!”
elsif(庄家手<玩家手)
写着“啊,你输了。”
其他的
写着“哇!是领带!”
结束
结束

值得注意的是,此解决方案缺少错误处理,例如将未知的
n
传递给
构造函数,或从空卡片组中提取
但可以很容易地添加到中。

它如何不起作用?根据什么标准比较这些卡?组合面值最高的牌会赢吗?它怎么会不起作用?你用什么标准来比较牌?具有最高组合面值的手牌是否获胜?数组类本身不适用于大于或小于运算符。我错了。很抱歉。我把它与
混淆了。数组类本身不能与大于或小于运算符一起工作。我错了。很抱歉。我把它和
混淆了。