- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在为类似于国际象棋的游戏编写 AI。棋盘为 10x10,每面 15 block 都有象棋相似的走法。
游戏中的一切都组织在对象中。瓷砖[][] 瓷砖; 10x10,每个 Tile 都有一个 piece-pointer 指向一个 piece 或 null。
到目前为止,我已经实现了一个没有剪枝的 MinMax 算法。每轮可能的走法平均是国际象棋比赛的两倍。
该算法有效,但速度非常慢。平均它可以检查40000 次移动/每秒所以深度为 4 我的游戏使用大约 4-5 秒来计算所有可能的 Action 。我稍后会实现修剪,但可能需要首先是对我的实现的一些反馈。
问题:我是否必须转换为 char-arrays/bit-board 或类似的来加速计算还是我做错了什么?
实现:(sudo)为了避免大量的双 for 循环,我跟踪 myPieces 和 opternalPieces在瓷砖列表中。董事会评估也进行一次,然后仅更新并且只能通过增加和减少移动的值来更新。在我的 minMax 算法的实现中,我使用了一个 GameState 类持有当前的游戏状态。
GameState {
Tile[][] board
List<Tile> myPieces;
List<Tile> otherPieces;
int[][] evaluatedBoard
int evaluatedValue
Piece moveFrom, moveTo //used to save pointer when applying move
int moveFromValue, moveToValue //used to save evaluationValue when applying move
void applyMove(Tile from, Tile to)
void undoMove(Tile from, Tile to)
GameState createCopy()
}
ApplyMove只更新evaluatedValue,不经过 整个 array.myPieces 和 otherPieces 也通过 apply/undo 更新。
最小最大:
maxMove(GameState state, int depth) {
for(Tile from : state.getMyPieces().clone()) { //list will be changed
for(Tile to : from.getPiece().getPossibleMoves()) {
if(depth == 0)
//find best move and so forth
else {
state.applyMove(from, to);
minMove(state.createCopy(), depth - 1) //similar to maxMove except it uses otherPieces
state.undoMove(from, to)
}
}
}
//return best move
}
编辑:添加了有关 applyMove 和 Profiler 的信息
Profiler: instructions
applyMove() 3200ms 11 430 000
undoMove() 1800ms 11 430 000
evaluateTile() 1400ms 22 400 000
minMove() 1700ms 315 493
applyMove 和 undoMove 只存储/反转指向旧的指针重视 from 和 to 并用来自的新值替换它们此举。然后调用 evaluateTile 返回 1-10 之间的数字取决于一个枚举类型。
最佳答案
您对表示的选择会让您付出很大的代价——您考虑的每一步都需要您复制大量状态。在我看来,您有两种选择:
(1) 使您的状态表示非常小(在国际象棋中,您可以使用 64 x 4 位或 .NET 中的 4 Int64 来实现),因此复制它非常便宜;或
(2) 使用增量使您的状态表示不可变,因此创建更新状态的成本很低。
我会先尝试选项 (1),看看你如何进行。
以下是一些您可能会觉得有用的链接: http://en.wikipedia.org/wiki/Board_representation_(chess ) http://www.cis.uab.edu/hyatt/boardrep.html
关于java - 国际象棋 AI : MinMax on object implementation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464731/
我这里有 2 个关于网格的问题。首先是单元格中的填充。二是grid-gap固定值。 我正在尝试创建 4 列网格。第一个和第二个单元格(蓝绿色、红色)在调整大小时应该消失,并且没有空间容纳它们。这应该用
在这篇文章的底部( How the minmax() Function Works )它说 minmax(auto, auto) : If used as a maximum, the auto va
我有这段 CSS 来设置我的网格的列: grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); 这很好用,但我想扩展它,这样我就可以
我尝试用Java编写用于四连体游戏的MinMax程序,但该程序也应适用于其他游戏。但是,我遇到了一个问题,几天之内无法解决。节点的值设置不正确。我正在共享我的代码,该代码负责生成树。 也许您会注意到我
我对某些特征应用了线性回归,以通过 10 倍交叉验证来预测目标。 MinMax 比例应用于特征和目标。 然后对特征进行标准化。 当我运行模型时,r2 等于 0.65,MSE 为 0.02。 但是当我使
我尝试为 grid-template-rows 设置 minmax(),有趣的是,结果是 grid-rows 扩展到 的最大值>minmax() 而不是最小值。 我们如何才能使网格行保持最小声明大小,
我尝试为 grid-template-rows 设置 minmax(),有趣的是,结果是 grid-rows 扩展到 的最大值>minmax() 而不是最小值。 我们如何才能使网格行保持最小声明大小,
如果我减小容器的宽度,为什么单元格占用的宽度小于 200 像素? 我认为 minmax 必须保持宽度不小于第一个参数 (200px)。 如果删除.a、.k 和.g 的样式,一切正常。 https://
我有一个 vector QPointF我需要找到最小值和最大值 y值,因为我需要知道 vector 中数据的幅度是多少。 我使用 QPointF,但为了添加每个新元素,我总结了 x vector 中其
我尝试为 grid-template-rows 设置 minmax(),有趣的是,结果是 grid-rows 扩展到 的最大值>minmax() 而不是最小值。 我们如何才能使网格行保持最小声明大小,
我正在研究国际象棋引擎,现在正在尝试实现 Minimax 算法。目前我已经整理了一个 mimimax 代码,但它并没有真正正常工作。鉴于我不是一个好的棋手,我在几分钟内就击败了引擎。 我希望有人好心地
这是我的 minmax 算法的代码: private static void minmax(){ Move move = max(4, true, null); //System.ou
我正在构建一个井字游戏(Free Code Camp 上的一个项目),并实现了一个 minmax 算法来决定计算机玩家接下来应该选择哪个方 block 。 在我测试过的所有情况下,它都按预期工作,但以
我想在对称网格中显示可变数量的相同高度和大小的图像。使用 CSS 网格和 auto-fill和 minmax我希望能够为我的轨迹定义最小和最大像素宽度,这样我就可以防止图像被缩放得太小,同时在用户视口
我遇到了与 CSS 网格布局相关的 minmax() 函数的一些问题。 这是我的代码: * { box-sizing: border-box; } .grid { display: grid;
我正在自学 minimax 算法,我只是有几个问题希望有人能回答。 首先在第 05 行 - := 是什么意思? 同样在第 08/14 行,我注意到方法 max 或 min 被调用时带有两个参数,这个方
有了 C++ 的所有新特性(我认为 C++11 就足够了),是什么阻止了 std::minmax 函数返回一对引用。 这样一来,如果一个人提供了两个可修改的引用,它们就可以被修改。这是在开 jar 头
我正在尝试为九人莫里斯创建“AI”,但我在 minMax 算法上遇到了困难。总结一下,我花了10多个小时试图找到问题,但没有成功。 (调试这个递归很糟糕,或者我做得很糟糕,或者两者兼而有之) 自从我开
您好,我正在尝试创建具有该行为的响应列。列的宽度可以在 335 像素或 540 像素之间。 但是当我使用 grid-template-columns: repeat(auto-fit, minmax(
我一直在尝试创建一个 3 列布局,其中包含宽度为 1、2 或 3 列的卡片。为此,我使用了 CSS Grid grid-template-columns: repeat(auto-fit, minma
我是一名优秀的程序员,十分优秀!