我正在教自己Ruby并使用Visual Studio中的编译器作为Sapphire on Steel来运行代码。我正在为学校制作二十一点计划。到目前为止,我创建了一个Deck类,Card类和Player类。在甲板课上我有一个打印甲板内容的功能,我正在尝试使用相同的功能来打印玩家手中的内容。代码调用卡类getCard()的函数,并循环整个数组。 print方法适用于printDeck()函数,但不适用于showHand()函数。
创建deck对象,并将其内容传递给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:showHand': undefined method
getCard'代表#(NoMethodError)
来自C:\ Users \ Zach \ Documents \ Visual Studio 2010 \ Projects \ BlackJack \ BlackJack \ BlackJack.rb:117:in the 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
答案 0 :(得分:0)
block in showHand': undefined method 'getCard' for #<Array:0x007ff3799a8c28> (NoMethodError)
错误信息非常明确:您在Array对象上调用Card#getCard
,这显然不存在。
getCard()
是Card
类的实例方法,这意味着您只能 使用Card
对象作为接收者来调用它:
card = Card.new
card.getCard()
答案 1 :(得分:0)
问题在于drawHand()
,当您调用@deck.pop(5)
时,您将返回一个包含5张牌的数组。
在您的方法start
中,如果您将push
更改为+
,您就可以了。
def start(myDeck)
@pHand + myDeck.drawHand()
end
答案 2 :(得分:0)
在start方法中,您将推送myDeck.drawHand的返回值,这是一个数组。那么当你用每一个看手时,你得到的就是这个阵列而不是卡片。你想做类似
的事情myDeck.drawHand.each do |card|
@pHand << card
end