我建了一副纸牌:
class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def output_card
puts "#{self.rank} of #{self.suit}"
end
end
class Deck
def initialize
@cards = []
@ranks = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
@suits = [:hearts, :spades, :diamonds, :clubs]
@suits.each do |suit|
@ranks.each do |rank|
@cards << Card.new(rank, suit)
end
end
end
def shuffle
@cards.shuffle!
end
def deal
@cards.shift
end
def output_deck
@cards.each do |card|
card.output_card
end
end
end
deck = Deck.new
deck.shuffle
deck.deal
deck.output_deck
puts "Show top card:"
deck.deal.output_card
我正在努力添加Dealer
课程,以便为指定数量的玩家提供指定数量的牌。
如果有人解释,我会非常感激。
答案 0 :(得分:0)
class Player
attr_accessor :name, :cards
def initialize(name)
@name = name
@cards = []
end
end
class HoldEmPoker
def cards_per_player
2
end
end
class Dealer
def initialize(deck, game)
@deck = deck
@game = game
end
def deal_to(player)
@game.cards_per_player.times do
player.cards << @deck.deal
end
end
end
p1 = Player.new("Negreanu")
p2 = Player.new("Ivey")
p3 = Player.new("Hellmuth")
players = [p1, p2, p3]
deck = Deck.new
deck.shuffle
game = HoldEmPoker.new
dealer = Dealer.new(deck, game)
players.each { |p| dealer.deal_to(p) }
p1.cards #=> an array with two Card objects
如果需要,可以向经销商类
添加deal
方法
def deal(players)
players.each do |player|
@game.cards_per_player.times do
player.cards << @deck.deal
end
end
end
players = [...]
dealer.deal(players)