gpt4 book ai didi

ruby - 用两个数组确定一手牌

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:54 24 4
gpt4 key购买 nike

我想用 ruby​​ 创建一个简单的多人扑克游戏程序,但我觉得我在重新发明轮子:

table = [card1, card2, card3, card4, card5]
mike = [card6, card7]
john = [card8, card9]

card6.face = "A"
card6.suit = "spades"

我很难编写一个算法来决定每个玩家可能拥有哪些牌。

例如,为了确定玩家是否拿到了同花,我写了这样的东西:

  together = table + hand

# Populate hash with number of times each suit was found
$suits.each do |suit|
matched[suit] = together.select{|card| card.suit == suit}.size
end
matched.each do |k,v|
if v == 5
puts "#{k} were seen five times, looks like a flush."
end
end

这看起来不是很全面(无法判断它是 A 高牌还是 6 高牌同花),也不是很像 ruby 。

有没有更明显的方法来确定扑克中的手牌?

最佳答案

它可能远非完美,但我写了一些方法来检测扑克手来解决 project euler问题。也许它可以给你一些想法;完整代码在这里:https://github.com/aherve/Euler/blob/master/pb54.rb

简而言之,Hand 是由Card 数组定义的,它响应Card.valueCard.type :

  def royal_flush?
return @cards if straight_flush? and @cards.map(&:value).max == Poker::values.max
end

def straight_flush?
return @cards if straight? and @cards.map(&:type).uniq.size == 1
end

def four_of_a_kind?
x_of_a_kind?(4)
end

def full_house?
return @hand if three_of_a_kind? and Hand.new(@cards - three_of_a_kind?).one_pair?
return nil
end

def flush?
return @cards if @cards.map(&:type).uniq.size == 1
end

def straight?
return @cards if (vs = @cards.map(&:value).sort) == (vs.min..vs.max).to_a
end

def three_of_a_kind?
x_of_a_kind?(3)
end

def two_pairs?
if (first_pair = one_pair?) and (second = Hand.new(@cards - one_pair?).one_pair?)
return first_pair + second
else
return false
end
end

def one_pair?
x_of_a_kind?(2)
end

def high_card?
@cards.sort_by{|c| c.value}.last
end

private
def x_of_a_kind?(x)
Poker::values.each do |v|
if (ary = @cards.select{|c| c.value == v}).size == x
return ary
end
end
return false
end
end

关于ruby - 用两个数组确定一手牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21928616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com