Random &引用;"井字游戏;触摸Lua随机方块得到填充?

Random &引用;"井字游戏;触摸Lua随机方块得到填充?,random,lua,draw,tic-tac-toe,Random,Lua,Draw,Tic Tac Toe,所以我最近在Touch Lua上购买了draw库!我开始尝试做一个简单的井字游戏。我正在使用一个简单的设置,他们用来检测NumPad默认程序上的点击,所以按钮应该可以工作 问题是当你点击一个方块时,O填充到看似随机的方块中,有时超过1个,最多4个方块可能被填充 我怀疑问题在于选择的函数,它将标题设置为X/O,然后更新板 local Turn = nil local Move = "O" local Mode = nil ::ModePick:: print("1 player mode? (y

所以我最近在Touch Lua上购买了draw库!我开始尝试做一个简单的井字游戏。我正在使用一个简单的设置,他们用来检测NumPad默认程序上的点击,所以按钮应该可以工作

问题是当你点击一个方块时,O填充到看似随机的方块中,有时超过1个,最多4个方块可能被填充

我怀疑问题在于选择的函数,它将标题设置为X/O,然后更新板

local Turn = nil
local Move = "O"
local Mode = nil

::ModePick::
print("1 player mode? (y/n)")
local plrs = io.read()
if plrs == "y" then
   Mode = 1
   goto TurnChoice
elseif plrs == "n" then
   Mode = 2
   goto Game
else
   goto ModePick
end

::TurnChoice::
 print("Would you like to go first? (Be O) (y/n)")
do
   local pick = io.read()
   if pick == "y" then
       Turn = 1
   elseif pick == "n" then
      Turn = 2
   else
      goto TurnChoice
   end
end

::Game::

local Buttons = {}

draw.setscreen(1)
draw.settitle("Tic Tac Toe")
draw.clear()
width, height = draw.getport()

function Picked(b)
   for i,v in pairs(Buttons) do
      if v == b then
         b.title = Move
         UpdateBoard()
      end
   end
   --Fill in X/O details
   --Detect if there's a tic/tac/toe
   --Set winning screen
   if Move == "O" then
       --Compute Move (1 player)
       --Move = "X" (2 player)
   else
      Move = "O"
   end
end

function DrawButton(b)
   draw.setfont('Helvetica', 50)
   draw.setlinestyle(2, 'butt')
   local x1, y1 = b.x, b.y
   local x2, y2 = x1+b.width, y1+b.height
   draw.rect(x1, y1, x2, y2, b.color)

   local w, h = draw.stringsize(b.title)
   local x = b.x + (b.width - w)/2
   local y = b.y + (b.height - h)/2
   draw.string(b.title, x, y, draw.black)
   return b
end

function Button(x, y, x2, y2, title, color, action)
   local action = action or function() end
   local button = {x = x, y = y, width = x2, height = y2, color = color, title = title, action = action}
   table.insert(Buttons, button)
   return button
end

function LookUpButton(x, y)
   for i = 1, #Buttons do
      local b = Buttons[i]
      if x > b.x and x < b.x+b.width and y > b.y and y < b.y+b.height then
         return b
      end
   end
   return nil
end

function TouchBegan(x, y)
   local b = LookUpButton(x, y)
   if b then
      b.action(b)
   end
end

function TouchMoved(x, y)
end

function TouchEnded(x, y)
end

draw.tracktouches(TouchBegan, TouchMoved, TouchEnded)


function CreateButton(x,y,x2,y2,txt,col,func)
   return DrawButton(Button(x, y, x2, y2, txt, col, func))
end

function UpdateBoard()
   draw.clear()
   for i = 1,3 do
        for ii = 1,3 do
             CreateButton(100 * (ii - 1) + 7.5, 100 * (i - 1) + 75, 100, 100,  Buttons[i + ii].title, draw.blue, Picked)
      end
   end
end

for i = 1,3 do
   for ii = 1,3 do
      CreateButton(100 * (ii - 1) + 7.5, 100 * (i - 1) + 75, 100, 100,  "", draw.blue, Picked)
   end
end

while true do
   draw.doevents()
   sleep(1)
end
编辑:多亏了Etan,我修复了UpdateBoard,方块仍然是随机的:

function UpdateBoard()
   draw.clear()
   local n = 1
   for i = 1,3 do
      for ii = 1,3 do
         CreateButton(100 * (ii - 1) + 7.5, 100 * (i - 1) + 75, 100, 100,  Buttons[n].title, draw.blue, Picked, Buttons[n])
         n = n + 1
      end
   end
end

我已经有一段时间没有发布完成的代码了,但它看起来是这样的:

function UpdateBoard(ended)
   local ended = ended or false
   local Act = nil
   if ended == true then
      Act = function() end
   else
      Act = Picked
   end
   draw.clear()
   local Buttons2 = {}
   for i,v in pairs(Buttons) do
      Buttons2[i] = v
   end
   Buttons = {}
   local n = 1
   for i = 1,3 do
      for ii = 1,3 do
         CreateButton(100 * (ii - 1) + 7.5, 100 * (i - 1) + 75, 100, 100,  Buttons2[n].title, draw.blue, Act, Buttons2[n], i, ii)
         n = n + 1
      end
   end
   OpenButtons = {}
   ClosedButtons = {}
   for i,v in pairs(Buttons) do
      if v.title == "" then
         table.insert(OpenButtons, v)
      else
         table.insert(ClosedButtons, v)
      end
   end
end
这个问题可能看起来有点复杂,但这是在许多其他事情之后的代码


这里您应该注意的主要区别是,它正在重新创建按钮表,因此它不会重新计算新按钮,而是使用n来计算,而不是使用i和ii。

问题出在哪里?什么不起作用?对不起,我从来没有注意到我没有说明问题。现在已修复。每次调用
UpdateBoard
时,您似乎都在创建新按钮,这将在
按钮中获得一堆额外的条目,并且您的
拾取的
以未指定的顺序在
按钮上循环。拾取的仍然要求它与传递的参数相等,那么,订单为什么重要呢?如果v==b,则您正在清除电路板,而不是
按钮
表。你只要不断地在里面插入新的按钮。我不知道这正是你面临的问题,但这是一个问题,而且肯定是一个令人困惑的因素。修复它,看看你是否还有其他问题。还要选择一个用于浏览
按钮的顺序
,并始终如一地使用它(它只是让事情更容易理解)。除非您解释更改的内容和原因,否则这不是一个有用的答案。
function UpdateBoard(ended)
   local ended = ended or false
   local Act = nil
   if ended == true then
      Act = function() end
   else
      Act = Picked
   end
   draw.clear()
   local Buttons2 = {}
   for i,v in pairs(Buttons) do
      Buttons2[i] = v
   end
   Buttons = {}
   local n = 1
   for i = 1,3 do
      for ii = 1,3 do
         CreateButton(100 * (ii - 1) + 7.5, 100 * (i - 1) + 75, 100, 100,  Buttons2[n].title, draw.blue, Act, Buttons2[n], i, ii)
         n = n + 1
      end
   end
   OpenButtons = {}
   ClosedButtons = {}
   for i,v in pairs(Buttons) do
      if v.title == "" then
         table.insert(OpenButtons, v)
      else
         table.insert(ClosedButtons, v)
      end
   end
end