【问题标题】:Check Winner Naughts and Crosses game - Lua/CoronaCheck Winner Naughts and Crosses 游戏 - Lua/Corona
【发布时间】:2018-03-25 02:28:55
【问题描述】:

我对 Lua 语言很陌生,正在尝试创建一个简单的井字游戏。我很难弄清楚如何检查获胜者。我尝试过 if-then 语句,但我认为我用错了。不确定检查获胜者的最佳方法是什么,因此非常感谢任何建议或帮助。

代码是-

local composer = require( "composer" )

local scene = composer.newScene()

d = display
w20 = d.contentWidth * .2
h20 = d.contentHeight * .2
w40 = d.contentWidth * .4
h40 = d.contentHeight * .4
w60 = d.contentWidth * .6
h60 = d.contentHeight * .6
w80 = d.contentWidth * .8
h80 = d.contentHeight * .8

----DRAW LINES FOR BOARD
local lline = d.newLine(w40,h20,w40,h80 )
lline.strokeWidth = 5

local rline = d.newLine(w60,h20,w60,h80 )
rline.strokeWidth = 5

local bline = d.newLine(w20,h40,w80,h40 )
bline.strokeWidth = 5

local tline = d.newLine(w20,h60,w80,h60 )
tline.strokeWidth = 5


--PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE
board ={
{"tl",1,w20,h40,w40,h20,0},
{"tm",2,w40,h40,w60,h20,0},
{"tr",3,w60,h40,w80,h20,0},
{"ml",4,w20,h60,w40,h40,0},
{"mm",5,w40,h60,w60,h40,0},
{"mr",6,w60,h60,w80,h40,0},
{"bl",7,w20,h80,w40,h60,0},
{"bm",8,w40,h80,w60,h60,0},
{"br",9,w60,h80,w80,h60,0}
}
--

local EMPTY, X, O = 0, 1, 2
local whichTurn = 0



--FILL COMPARTMENT W/ COLOUR WHEN TOUCHED
local function fill(event)
if (event.phase == "ended") then
    for t = 1,9 do
        if event.x > board[t][3] and event.x < board[t][5] then
            if event.y < board[t][4] and event.y > board[t][6] then 
             if board[t][7] == EMPTY then
              if whichTurn == 1 then
                whichTurn = 2
              else
                whichTurn = 1
              end
                board[t][7] = whichTurn
                 if board[t][7] == 1 then
           local xText = display.newText("X", board[t][3], board[t][4], "Arial", 80)
           xText.anchorX = 0
           xText.anchorY = 100
                 elseif board[t][7] == 2 then 
                   local oText = display.newText("O", board[t][3], board[t][4], "Arial", 80)
                   oText.anchorX = 0
                   oText.anchorY = 100
                 end
             end
          end
        end
    end
end


local function checkWinner()
  for i = 1,9 do
    if board[i][2] == 1 and board[i][7] == 1 then
      boxOne = "x"
    end
    if board[i][2] == 2 and board[i][7] == 1 then
      boxTwo = "x"
    end
    if board[i][2] == 3 and board[i][7] == 1 then
      boxThree = "x"
    end
    if boxOne == "x" and boxTwo == "x" and boxThree == "x" then
      display.newText("Winner", 10, 100, "Arial", 200)
    end
  end
end



end

Runtime:addEventListener ("touch", fill)



return scene

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:
    local All_Lines = {{1,5,9}, {3,5,7}, {1,2,3}, {4,5,6}, {7,8,9}, {1,4,7}, {2,5,8}, {3,6,9}}
    
    local function checkWinner()
       for _, Line in ipairs(All_Lines) do
          local values = 0
          for _, idx in ipairs(Line) do
             values = values * 10 + board[idx][7]
          end
          if values == 111 then
             display.newText("Winner", 10, 100, "Arial", 200)
             return
          elseif values == 222 then
             display.newText("Loser", 10, 100, "Arial", 200)
             return
          end
       end
    end
    

    【讨论】:

    • 请为您的回答提供上下文。从长远来看,仅提供代码对 OP 没有帮助:)
    • @RoelStrolenberg - 我的代码应该在 OP 代码的上下文中考虑(我的代码实际上是一个函数 checkWinner,它取决于 OP 代码中定义的对象)。我同意我的没有上下文的代码没有任何价值。
    • 嗨@EgorSkriptunoff 谢谢你的回复。我已将它包含在代码中,并且不能做正确的事情,因为它似乎不起作用。它似乎根本没有运行该功能。有什么我应该包含的东西来调用一个函数,因为我找不到任何告诉我怎么做的东西吗?
    • @Katie - 你应该将这个函数的调用包含在一些事件处理程序中。
    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2016-07-31
    • 2015-01-07
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多