我正在自学 Ruby,并使用带有 Visual Studio 的编译器(称为 Sapphire on Steel)来运行代码。我正在为学校制作一个二十一点计划。到目前为止,我创建了一个 Deck 类、Card 类和 Player 类。在甲板类中,我有一个打印甲板内容的函数,我试图使用相同的函数来打印玩家手牌的内容。代码调用卡片类 getCard() 的函数,并循环整个数组。打印方法适用于 printDeck() 函数,但不适用于 showHand() 函数。
甲板对象被创建,其内容被传递到 PlayerHand.start() 函数中的一个数组。PlayerHand.showHand() 函数打印甲板内容被传递到的数组的内容。
有人可以帮我弄清楚为什么吗?任何和所有的帮助将不胜感激
#Zach Albright
#4-11-2013
#Black Jack
class Card
def initialize(suit, value)
@suit = suit
@value = value
end
def getCard()
puts "#{@suit}: #{@value}\n"
end
def getSuit()
return @suit
end
def getValue()
return @value
end
end
class Deck
def initialize()
@deck = Array.new()
for i in 1..4
for j in 1..13
if i == 1
if j == 11
@deck.push(Card.new("Hearts", "Jack"))
elsif j == 12
@deck.push(Card.new("Hearts", "Queen"))
elsif j == 13
@deck.push(Card.new("Hearts", "King"))
else
@deck.push(Card.new("Hearts", j))
end
elsif i == 2
if j == 11
@deck.push(Card.new("Spades", "Jack"))
elsif j == 12
@deck.push(Card.new("Spades", "Queen"))
elsif j == 13
@deck.push(Card.new("Spades", "King"))
else
@deck.push(Card.new("Spades", j))
end
elsif i == 3
if j == 11
@deck.push(Card.new("Clubs", "Jack"))
elsif j == 12
@deck.push(Card.new("Clubs", "Queen"))
elsif j == 13
@deck.push(Card.new("Clubs", "King"))
else
@deck.push(Card.new("Clubs", j))
end
elsif i == 4
if j == 11
@deck.push(Card.new("Diamonds", "Jack"))
elsif j == 12
@deck.push(Card.new("Diamonds", "Queen"))
elsif j == 13
@deck.push(Card.new("Diamonds", "King"))
else
@deck.push(Card.new("Diamonds", j))
end
end
end
end
@deck.compact
end
def printDeck()
@deck.each do |i|
puts i.getCard()
end
end
def shuffleDeck()
@deck.shuffle!
end
def drawCard()
return @deck.pop()
end
def drawHand()
return @deck.pop(5)
end
end
class PlayerHand
def initialize()
@pHand = Array.new()
@pCard = Card.new("Hearts", 4)
end
def start(myDeck)
@pHand.push(myDeck.drawHand())
end
def drawCard(myDeck)
@pHand.push(myDeck.drawCard())
end
#still needs work to show the actual contents of the card .getCard is not working correctly
def showHand()
@pHand.each do |i|
puts i.getCard()
end
end
end
myCard = Card.new("Hearts", 4)
myArray = Array.new()
puts myCard.getCard()
myDeck = Deck.new()
myDeck.shuffleDeck()
player = PlayerHand.new()
player.start(myDeck)
player.showHand()
错误:C:\Users\Zach\Documents\Visual Studio 2010\Projects\BlackJack\BlackJack\BlackJack.rb:118:in showHand': undefined method
getCard' for # (NoMethodError)
来自 C:\Users\Zach\Documents\Visual Studio 2010\Projects\BlackJack\BlackJack\BlackJack.rb:117:in `each'
来自 C:\Users\Zach\Documents\Visual Studio 2010\Projects\BlackJack\BlackJack\BlackJack.rb:117:in `showHand'
来自 C:\Users\Zach\Documents\Visual Studio 2010\Projects\BlackJack\BlackJack\BlackJack.rb:133