Lua 算法在井字游戏中保存移动

我在用 Lua 编程,正在尝试制作一个 AI。每当我运行代码时(目前只有 AI 的移动代码),它会在棋盘上随机选择一个位置,但它不会保存之前的移动。每一次都是一个全新的棋盘,只有一个位置被占用。能有人帮我解决这个问题吗?如果可以的话,我将不胜感激!代码如下:

    function moves()
        local Possible = {'1,1','1,2','1,3','2,1','2,2','2,3','3,1','3,2','3,3'}
        math.randomseed(os.time())
        math.random()
        local AImove = Possible[math.random( #Possible )]
        print('The Opponent Chose',AImove)
        --[[removal of numbers from possibility to only provide legal moves0]]--
        if AImove == '1,1' then
            table.remove(Possible,1)
            print("_ _ _")
            print("_ _ _")
            print("X _ _")
        end
        if AImove == '1,2' then
            table.remove(Possible,2)
            print("___")
            print("X__")
            print("___")
        end
        if AImove == '1,3' then
            table.remove(Possible,3)
            print("X _ _")
            print("_ _ _")
            print("_ _ _")
        end
        if AImove == '2,1' then
            table.remove(Possible,4)
            print("_ _ _")
            print("_ _ _")
            print("_ X _")
        end
        if AImove == '2,2' then
            table.remove(Possible,5)
            print("_ _ _")
            print("_ X _")
            print("_ _ _")
        end
        if AImove == '2,3' then
            table.remove(Possible,6)
            print("_ X _")
            print("_ _ _")
            print("_ _ _")
        end
        if AImove == '3,1' then
            table.remove(Possible,7)
            print("_ _ _")
            print("_ _ _")
            print("_ _ X")
        end
        if AImove == '3,2' then
            table.remove(Possible,8)
            print("_ _ _")
            print("_ _ X")
            print("_ _ _")
        end
        if AImove == '3,3' then
            table.remove(Possible,9)
            print("_ _ X")
            print("_ _ _")
            print("_ _ _")
        end
    end
    moves()

回答:

你需要一个能够存储游戏状态的东西。我给你一个非常简单且快速的例子。一个名为 board 的表,用于存储 9 个字段,以及一些允许我们操作和显示棋盘的函数。

这应该能给你一个大致的思路,如何开始。

-- 我们的棋盘
local board = {}

-- 检查棋盘上的一个字段
function board:check(row, col, str)
  board[row][col] = str
  self:print()
end

-- 将棋盘打印到屏幕上
function board:print()
  print("---------")
  for i,v in ipairs(self) do
    print("| " .. table.concat(v, " ") .. " |")
  end
  print("---------")
end

-- 初始化/清空棋盘
function board:init()
  for row = 1, 3 do
    board[row] = {}
    for col = 1, 3 do
      board[row][col] = " "
    end
  end
  board:print()
end

board:init()
board:check(2,2,"X")
board:check(1,3,"O")

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注