puts "Enter your a number:"
class Card
attr_accessor :rank, :suit
def initialize(rank,suit)
self.rank = rank
self.suit = suit
end
def output_card
puts "#{self.rank} of #{self.suit}"
end
end
class Deck
def initialize
@cards = []
@ranks = [:A, 2, 3, 4, 5, 6, 7, 8, 9, 10, :J, :Q, :K]
@suits = [:Spades, :Hearts, :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
end
class Hand
def initialize(deck)
@hand = []
5.times do
@hand << deck.deal
end
end
def display_hand
@hand.each do |card|
@hand.output_card
end
end
end
hand = Hand.new
hand.display_hand
puts hand
我正在尝试建立一个输出5张牌的纸牌游戏。但是我输出5张卡时遇到了麻烦。我一直收到这个错误:
card_game.rb:46:in `initialize': wrong number of arguments (given 0, expected 1) (ArgumentError)
from card_game.rb:64:in `new'
from card_game.rb:64:in `<main>'
我打电话的时候:
hand = Hand.new
hand.display_hand
puts hand
答案 0 :(得分:1)
错误告诉您需要构建一个Hand with a Deck。下面两个类尝试使用此代码:
deck = Deck.new # create new deck
hand = Hand.new deck # initialize hand with the deck
hand.display_hand
puts hand