【问题标题】:"Tic Tac Toe" Touch Lua random squares getting filled in?“井字游戏”Touch Lua 随机方块被填满?
【发布时间】:2014-09-26 22:03:52
【问题描述】:

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

问题在于,当您点击一个正方形时,O 会填充到看似随机的正方形中,有时会超过 1 个,最多可能会填充 4 个以上的正方形。

我怀疑问题出在 Picked 函数上,它将标题设置为 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

注意:很抱歉,如果缩进出现错误,我将所有这些代码都粘贴到了我的 iPod 上,所以我不得不手动在每行开始输入 4 个空格。

如果有人可以帮助我解决我遇到的这个小挫折,我很乐意帮助,如果我缺少任何东西,我很乐意在 cmets 中回复编辑它:D

编辑:我已经修改了一些代码来修复表格如何不断获得新按钮,这是我现在拥有的代码,同样的问题,按钮被添加到错误的位置(现在被删除):

function Button(x, y, x2, y2, title, color, action, prev)
   local action = action or function() end
   local button = {x = x, y = y, width = x2, height = y2, color = color, title = title, action = action}
   if prev then
      for i,v in pairs(Buttons) do
         if v == prev then
            table.remove(Buttons, i)
         end
      end
   end
   table.insert(Buttons, button)
   return button
end

function CreateButton(x,y,x2,y2,txt,col,func, prev)
   return DrawButton(Button(x, y, x2, y2, txt, col, func, prev))
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, Buttons[i + ii])
      end
   end
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

【问题讨论】:

  • 有什么问题?什么不工作?
  • 对不起,我什至没有注意到我没有说明问题。现已修复。
  • 看起来您每次调用UpdateBoard 时都在创建新按钮,这将在Buttons 中为您带来一堆额外的条目,并且您的Picked 循环在Buttons 上循环未指定的顺序。
  • Picked 仍然要求它等于传递的参数,那么为什么顺序很重要?如果 v == b 那么
  • 您正在清除板,但不是 Buttons 表。您只需继续在其中插入新按钮。我不知道这正是您遇到的问题,但这是一个问题,而且肯定是一个混淆因素。修复它,看看你是否还有其他问题。还要选择一个用于遍历Buttons 的顺序并始终如一地使用它(它只是让事情更容易理解)。

标签: random lua draw tic-tac-toe


【解决方案1】:

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

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。

【讨论】:

  • 除非您解释更改的内容和原因,否则这不是一个有用的答案。
猜你喜欢
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2014-12-18
  • 2015-06-12
  • 2015-07-21
相关资源
最近更新 更多