在Lua中创建和使用面向对象的模块

我一直在尝试使用 Lua 中的模块来模拟对象。例如,我制作了一副卡牌:

local Card = require("Card")

local function firstCard()
    Card.newDeck()
    Card.drawCard()
    return Card.getCard()
end

local function secondCard()
    Card.newDeck()
    Card.drawCard()
    return Card.getCard()
end

first = firstCard()
second = secondCard()

print(first)
print(second)

我设置了 first = firstCard()second = secondCard(),但是当我打印这两个变量 second 时,偶尔会得到 nil。我很迷茫。以下是实际的模块本身。

local Card = {}

local deck
local value
local number, suit
local index = 0
local getCard
local getValue

function Card.newDeck()
   deck = {}
   value = {}
   for x = 1, 13 do
      if x == 1 then
         number = "Ace"
      elseif x == 11 then
         number = "Jack"
      elseif x == 12 then
         number = "Queen"
      elseif x == 13 then
         number = "King"
      else
         number = x
      end
      for x1 = 1, 4 do
         if x1 == 1 then
            suit = "Clubs"
         elseif x1 == 2 then
            suit = "Diamonds"
         elseif x1 == 3 then
            suit = "Hearts"
         else
            suit = "Spades"
         end
         index = index + 1
         deck[index] = number.." of "..suit
         value[index] = x
      end
   end
end

function Card.drawCard()
   index = math.random(52)
   getCard = deck[index]
   getValue = value[index]
end

function Card.getCard()
   return getCard
end

function Card.getValue()
   return getValue
end

function Card.getIndex()
   return index
end

return Card

我在面向对象编程方面对 Lua 的了解有限,说实话,我通常只使用它进行计算或制作小游戏以让自己倍感快乐——我只有 16 岁。尽管在我开始使用 Java 之前很早就开始使用 Lua,但我更习惯 Java。我只是想知道如何使这个方法起作用。在大多数情况下它是有效的,只是偶尔会出现 nil 值。

点赞
用户204011
用户204011

问题在于您已经在模块顶级处将 index 变量声明为局部变量。这意味着你在第一次调用drawCard()时计算的 index 的随机值会在第二次调用 newDeck() 时被重复使用。您可以在newDeck()开头处添加 print(index) 来查看我的意思。

有几种方法可以解决这个问题。一种方法是在 newDeck() 的顶部添加 index = 0。更好的方法是使用更小的范围声明变量,即将index 给每个使用它的函数局部化。

2014-12-13 00:04:53
用户4261756
用户4261756

尝试使用这个方法,看起来正常工作,如果索引有问题,它应该打印“ERROR NO VALUE”,如果出现这种情况(虽然不应该),只需在generateCard()函数中打印索引。

这是测试

local Card = require("Card");
local function firstCard()
    Card.newDeck()
    return Card.generateCard(); --两个参数,牌组和牌值
end
local function secondCard()
    Card.newDeck()
    return Card.generateCard();
end
first = firstCard()
second = secondCard()
print(first)
print(second)

这是模块

local Card = {deck={};value={};};
function Card.newDeck()
    local self = Card
    local deck = self.deck;
    local value = self.value;
    local cards = {[1]="Ace",[11]="Jack",[12]="Queen",[13]="King"};
    local types = {[1]="Clubs",[2]="Diamonds",[3]="Hearts",[4]="Spades"};
    for x = 1, 13 do
        for i = 1, 4 do
            local _card,_type=(cards[x] or x),types[i];
            deck[#deck+1] = _card.." of ".._type
            value[#deck+1] = x
        end
    end
end
function Card.generateCard()
    local self = Card;
    if(not math.round) then
        math.round = function(value) local mi,ma=math.floor(value),math.ceil(value)return(mi+0.5>value and mi or ma)end;
    end
    local index = math.round(math.random(1000, 52000)/1000);
    return (self.deck[index] or "NO VALUE FOR CARD"),(self.value[index] or "NO DECK FOR CARD");
end
return Card
2014-12-13 17:08:56