我在使这个井字游戏算法起作用时遇到了一些麻烦。这是一种检查移动是否获胜的算法

I'm having a bit of trouble making this tic-tac-toe algorithm work. It's an algorithm for checking if a move makes a win

这对于对角线和第三行或第三列非常有效。出于某种原因,当有人在第一行和第二行或第二列获胜时,无论是 X 还是 O,都不会发出获胜信号。 Row 和 Col 就像演戏的地方,player 就像演戏的人。

让我感到困惑的是,它无法识别所有行和列的胜利,这对我来说太奇怪了,以至于我只是想念问题所在。

--[[
    Function that checks if there is a win whenever a play is made
]]

function checkWin(row, col, player)
    -- check rows. I have the row that the marker was placed. Go through it and see if there is a win.
    for c = 1, 3 do
        if board[row][c] ~= player then
            break
        end
        if c == 3 then
            gWhoWon = player
            gameOver = true
        end
    end


    -- check columns. I have the col that the marker was placed. Go through it and see if there is a win.
    for r = 1, 3 do
        if board[r][col] ~= player then
            break
        end
        if r == 3 then
            gWhoWon = player
            gameOver = true
        end
    end


    -- check diagonals. Check both diagonals and see if there is a win (can make this more efficient, only looking at 
    -- diagonals that matter).

    -- first diag
    for d = 1, 3 do
        if board[d][d] ~= player then
            break
        end
        if d == 3 then
            gWhoWon = player
            gameOver = true
        end
    end        

    -- anti diag
    for d = 1, 3 do
        if board[d][4-d] ~= player then
            break
        end
        if d == 3 then
            gWhoWon = player
            gameOver = true
        end
    end


    --[[
    if turn == 9 then
        gameOver = true
    end
    ]]
end

这是我初始化板子和调用 checkWin 函数的函数:

function PlayState:init()
    xTurn = true
    gameOver = false
    turn = 1
    gWhoWon = ""

    -- board
    board = {
        {"", "", ""},
        {"", "", ""},
        {"", "", ""}
    }
end


function PlayState:update(dt)
    -- transition to win state when someone wins
    if gameOver then
        gStateMachine:change('win')
    end

    -- if there is a left mouse click, insert an "x" or an "o" on that square
    if love.mouse.wasPressed(1) then
        -- get mouse position
        x = love.mouse.getX()
        y = love.mouse.getY()
    
        -- iterate through the board
        for row = 1, 3 do
            for col = 1, 3 do
                -- if mouse click was inside a square and it is empty
                if isInsideSquare(x, y, row, col) and isEmpty(row, col) then
                    -- put an "x" on the square
                    if xTurn then
                        board[col][row] = 'x'
                        checkWin(row, col, 'x')  -- check if this move makes a win
                        xTurn = false
                    -- put an "o" on the square
                    else
                        board[col][row] = 'o'
                        checkWin(row, col, 'o')  -- check if this move makes a win
                        xTurn = true
                    end
                turn = turn + 1
                end
            end
        end
    end
end

mix-up board 矩阵的维度使用行值和列值:

  • update 方法中你有这样的代码:board[col][row] = 'x',所以列是 first 维矩阵,但是:

  • checkWin函数中,你有类似board[row][c] ~= player的代码,所以列是矩阵的second维度

这会导致意想不到的结果。确保协调您的代码,以便两个数组维度中的每一个始终用于同一轴(行与列)。