我正在尝试将一堆代码从JavaScript转换为Lua。我虽然坚持下一点。我不懂Lua面向对象的语法。有人能告诉我正确的语法是什么吗?谢谢!
//=======================================================
//=======================================================
//=======================================================
// The TMaze object
//------------------------------------------------------------------------------ Ctor
function TMaze(nHigh, nWide) {
this.high = nHigh;
this.wide = nWide;
this.ary = TMazeArray(nHigh, nWide);
this.totRooms = gnHigh * gnWide; // less (short) bottom line
this.curRoomCnt = 0; // rooms that have been visited/door opened
this.GetCell = function(x,y) {return this.ary[y][x];}
this.SetCell = function(x,y,value) {this.ary[y][x] = value;}
this.HasCellBit = function(x,y,value) {return ((this.ary[y][x] & value) == value);}
this.SetCellBit = function(x,y,value) {this.ary[y][x] |= value;}
this.ClearCellBit = function(x,y,value) {this.ary[y][x] &= ~value;}
this.ToggleCellBit = function(x,y,value) {this.ary[y][x] ^= value;}
this.IsEmptyCellAt = IsEmptyCellAt; // some member fns, defined below
this.IsValidXY = IsValidXY;
this.GetNewXY = GetNewXY;
}
//----------- TRUE if cell in that direction is empty and valid
//
function IsEmptyCellAt(x, y, dir) {
var o = this.GetNewXY(x, y, dir);
if (!this.IsValidXY(o.newX, o.newY)) return (false);
if (this.GetCell(o.newX, o.newY) != 0) return (false);
return true; // yes, it's possible to move into that cell
}
//------------------------------------------------------------------------------
// return -1 if that would be an invalid move (off the board)
// true if X,Y is on the board
//
function IsValidXY(x, y) {
if (y < 0) return (false);
if (y > this.high-1) return (false);
if (x < 0) return (false);
if (x > this.wide-1) return (false);
// if (y & 1) { // on off Y, max X an maxY are 1 less
// if (x > this.wide-2) return (false);
// if (y > this.high-2) return (false);
// }
return true; // possible to move into that direction
}
//------------------------------------------
// If I move in a direction, what are the new X,Y values?
// Return an object with newX and newY properties
//
function GetNewXY(x, y, dir) {
var oRet = {"newX":-1, "newY":-1, "isValid":false};
var newX = x;
var newY = y;
newX += ganDeltaX[dir]; // add the deltas
newY += ganDeltaY[dir];
if (this.IsValidXY(newX, newY)) {
oRet.newX = newX;
oRet.newY = newY;
oRet.isValid = true;
}
return oRet;
}
您可以忽略按位操作,因为我已经找到了如何翻译它。我最关心的是oop的东西。
[编辑]
这是我到目前为止所拥有的,但它崩溃了。
--=======================================================
--=======================================================
-- The TMaze object
-------------------------------------------------------------------------------- Ctor
TMaze =
{
high = gnHigh,
wide = gnWide,
ary = TMazeArray(gnHigh, gnWide),
totRooms = gnHigh * gnWide, -- less (short) bottom line
curRoomCnt = 0, -- rooms that have been visited/door opened
}
TMaze.GetCell = function(self,x,y) return self.ary[y][x] end
TMaze.SetCell = function(self,x,y,value) self.ary[y][x] = value end
TMaze.HasCellBit = function(self,x,y,value) return bit.band(self.ary[y][x], value) == value end
TMaze.SetCellBit = function(self,x,y,value) self.ary[y][x] = bit.bor(self.ary[y][x], value) end
TMaze.ClearCellBit = function(self,x,y,value) self.ary[y][x] = bit.band(self.ary[y][x], bit.bnot(value)) end
TMaze.ToggleCellBit = function(self,x,y,value) self.ary[y][x] = bit.bxor(self.ary[y][x], value) end
--=======================================================
------------- TRUE if cell in that direction is empty and valid
--
TMaze.IsEmptyCellAt = function(self, x, y, dir)
local o = self.GetNewXY(self, x, y, dir)
if (not self.IsValidXY(self, o.newX, o.newY)) then return 0 end
if (self.GetCell(self, o.newX, o.newY) ~= 0) then return 0 end
return 1 -- yes, it's possible to move into that cell
end
--------------------------------------------------------------------------------
-- return -1 if that would be an invalid move (off the board)
-- true if X,Y is on the board
--
TMaze.IsValidXY = function(self, x, y)
if (y < 1) then return 0 end
if (y > self.high) then return 0 end
if (x < 1) then return 0 end
if (x > self.wide) then return 0 end
-- if (y & 1) then -- on off Y, max X an maxY are 1 less
-- if (x > self.wide-2) then return 0 end
-- if (y > self.high-2) then return 0 end
-- end
return 1 -- possible to move into that direction
end
--------------------------------------------
-- If I move in a direction, what are the new X,Y values?
-- Return an object with newX and newY properties
--
TMaze.GetNewXY = function(self, x, y, dir)
local oRet = {newX=-1, newY=-1, isValid=0}
local newX = x
local newY = y
newX = newX + ganDeltaX[dir] -- add the deltas
newY = newY + ganDeltaY[dir]
if (self.IsValidXY(self, newX, newY)) then
oRet.newX = newX
oRet.newY = newY
oRet.isValid = 1
end
return oRet
end
答案 0 :(得分:0)
更改
self.IsValidXY(self, newX, newY)
到
self:IsValidXY(newX, newY)
因某种原因使它发挥作用。