Object 使用meta tabl模拟对象:table.insert返回;表预期,得到零“;

Object 使用meta tabl模拟对象:table.insert返回;表预期,得到零“;,object,lua,blackjack,Object,Lua,Blackjack,情况是这样的 我正在命令行中制作一个21点游戏,因此我构建了一个玩家“对象”,以便能够为用户和经销商(CPU)重用功能。整场比赛的机修工都在工作,除了分手的能力。所以,现在我要通过我的整个游戏来增加玩家拥有多只手的能力 元表的构造如下所示: function Player:new (o) o = o or {} -- create object if user does not provide one setmetatable(o, self) self.__inde

情况是这样的

我正在命令行中制作一个21点游戏,因此我构建了一个玩家“对象”,以便能够为用户和经销商(CPU)重用功能。整场比赛的机修工都在工作,除了分手的能力。所以,现在我要通过我的整个游戏来增加玩家拥有多只手的能力

元表的构造如下所示:

function Player:new (o)
    o = o or {}   -- create object if user does not provide one
    setmetatable(o, self)
    self.__index = self
    self.total = 0 -- total of the hand (adding card values)
    self.money = 0 -- Remaining credits
    self.curHand = 1 -- Keeps track of the current hand being played and which bet to use
    self.split = 0 -- Keeps track of the total amount of times the hand was split (total number of hands)
    self.bet = {0, 0, 0, 0} -- 4 bets for the 4 possible hands after a Split
    self.insurance = 0 -- Amount being payed for insurance when dealer shows an Ace
    self.blackjack = false
    return o
end
然后将用户和经销商实例化为:

user = Player:new({hand = {{}, {}, {}, {}}}) -- Instantiates the user and dealer objects
dealer = Player:new({hand = {}})
我的“命中”功能是我的问题开始的地方

function Player:hit(deck)
    table.insert(self.hand[self.curHand], draw(deck))
end
如果我“橡皮鸭”问题,我的代码应该执行以下操作:

1. user.hand starts as a table containing user.hand[1] through [4]
2. user.curHand == 1
3. So, user.hand[user.curHand] == user.hand[1] (for now at least)
4. table.insert will then add the first card (returned from the function "draw") to user.hand[user.curHand] ... in other words the first table of the four.
在崩溃文本中,它告诉table.insert需要一个表,但得到了nil,这意味着user.hand[1]上没有表。。。请记住,在我开始添加多只手之前,一切都正常


我确信解决方案是简单明了的,但我无法为我的爱找到答案。在我看来,一切都是有意义的!(但是,计算机完全按照我们告诉他们的去做……所以我知道我是问题所在,代码对我来说似乎工作正常。我无法重现错误。在您给我们的代码之外可能有问题。请阅读。您是如此正确!!但是!正是这条评论让我考虑检查另一个细节……经销商的手!这就是问题所在问题是。由于声明经销商的“手”中只有一个表,因此无法访问经销商。手[1].因此,我在这里编写的代码应该适用于玩家,但不适用于经销商,我通过添加一对花括号来修复这一问题:使其成为表中的一个表。代码似乎适用于我。我无法重现错误。在您提供给我们的代码之外可能有问题。请阅读。您是如此正确!!但是!仅此而已t comment让我想到检查另一个细节…经销商的手!这就是问题所在。由于经销商声明的“手”中只有一个表,因此无法访问经销商。手[1].所以,我在这里写的代码应该适用于玩家,但不适用于庄家,我通过添加一对花括号修复了这一问题:使它成为桌子中的一张桌子。