在国际象棋编程中很好地利用了递归?

发布于 2024-09-03 08:17:05 字数 79 浏览 6 评论 0原文

作为家庭作业的一部分,我必须用 Java 编写一个简单的国际象棋游戏。我正在考虑借此机会尝试递归,我想知道国际象棋中是否有明显的递归代码候选者?

As part of a homework assignment, I have to program a simple chess game in Java. I was thinking of taking the opportunity to experiment with recursion, and I was wondering if there's an obvious candidate in chess for recursive code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

饮湿 2024-09-10 08:17:05

对我来说,最明显的候选者是用于搜索最佳动作的递归极小极大例程。这也涉及到搜索算法背后的许多理论,并且实现起来会非常酷。

示例:

http://www.devshed.com/ c/a/实践/用递归解决问题/6/

The most obvious candidate to me would be a recursive minimax routine for searching for the best moves. This also gets into a lot of the theory behind search algorithms and would be pretty cool to implement.

Example:

http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/6/

十二 2024-09-10 08:17:05

是的,有。如果你有一些函数可以评估白色玩家某个位置的“力量”。您可以移动一个棋子并递归调用它来评估移动的价值并选择最佳移动。

您应该为黑人玩家调用相同的函数,交换黑人和白人的角色,从而评估对手移动的“危险”。

然后再次对于白人等。

请注意,您不应该在递归级别上走得太深,否则它将永远持续下去。

Yes there is. If you have some function that evaluate "force" of some position for say player white. You can move a piece and call it recursively to evaluate the value of a move and choose the best move.

You should call the same function for player black, exchanging roles for blacks and whites, thus evaluating "danger" of an opponent move.

Then again for the whites, etc.

Just be aware you should not go too deep in recursion levels or it will take forever.

↘人皮目录ツ 2024-09-10 08:17:05

深度优先搜索是递归的主要候选者。因此,如果你正在为家庭作业编写人工智能程序,那么人工智能的前瞻算法(尝试找出最佳下一步行动)将是一个不错的选择。

但要小心 - 你可能很快就会耗尽内存。您可能想限制人工智能可以查看的深度移动数量。

Depth-first search is a prime candidate for recursion. so if you're programming an AI for the homework assignment, then the AI's lookhead algorithm to try to figure out a best next move would be a good candidate.

Be careful though - you can run out of memory quick. You probably want to limit the number of moves deep the AI can look.

彼岸花似海 2024-09-10 08:17:05

注意动态编程,因为你有多种组合会导致同一个棋盘,所以你应该记住缓存移动以避免重复计算

如果您检测到递归只会将您带到您去过的地方,只需中断该调用即可。这称为回溯

Mind dynamic programming, as you have multiple combinations which lead to the same board, you should remember to cache the moves in order to avoid repeating calculations

If you detect a recursion just lead you to a place where you have been, just break that call. This is known as backtracking

凉栀 2024-09-10 08:17:05

不是国际象棋,而是国际象棋图形的经典谜题: http://en.wikipedia.org/wiki/Eight_queens_puzzle

Not chess, but a classic puzzle with chess figueres: http://en.wikipedia.org/wiki/Eight_queens_puzzle

微凉徒眸意 2024-09-10 08:17:05

您正在考虑回溯

You are thinking about backtracking

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文