你会在棋盘上做什么? (件位置)
创建 3 个坐标列表,
- 您会为空位置
- 黑色位置
- 白色位置
还是在需要时循环遍历数组并每次都使用结果?
什么是最好的? (速度方面)
would you create 3 list(of coordinate) for
- empty position
- black position
- white position
or just looping though the array when needed and play with the result every time?
what would be best? (speed wise)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的两个主要选择是速度和代码清晰度之间。
如果速度是您的首要任务,那么您必须为棋盘上的每组棋子使用 64 位数据类型(例如白棋子、黑皇后、过路棋子)。 然后,您可以在生成移动和测试移动合法性时利用本机按位运算。
如果代码的清晰度是优先考虑的,那么就忘记位改组,并像其他人已经建议的那样采用良好抽象的数据类型。 请记住,如果您这样做,您可能会达到性能上限。
首先,请查看 Crafty (C) 和 SharpChess (C#)。
(最初发布
Your two main choices are between speed and code clarity.
If speed is your priority then you must use a 64 bit data type for each set of pieces on the board (e.g. white pawns, black queens, en passant pawns). You can then take advantage of native bitwise operations when generating moves and testing move legality.
If clarity of code is priority then forget bit shuffling and go for nicely abstracted data types like others have already suggested. Just remember that if you go this way you will probably hit a performance ceiling.
To start you off, look at the code for Crafty (C) and SharpChess (C#).
(Originally posted here)
您正在寻找的是董事会代表。 国际象棋编程维基有一个关于该主题的非常详细的部分(如果你是认真的,绝对值得一读关于编写人工智能),而维基百科提供了关于该主题的很好的概述。
在选择合适的棋盘表示时要深思熟虑,这一点很重要 - 它们都有自己独特的优势(和陷阱) - 很大程度上与某些操作的速度/执行有关,例如执行移动和评估棋盘状态(通常范围从 O( 1) 时间复杂度为 O(n),具体取决于方法和任务)。 据我所知,对于“最佳”董事会代表仍然没有达成共识,尽管现在有些代表通常比其他代表更受欢迎(
What you're looking for is a board representation. The Chess Programming Wiki has a very detailed section on the topic (definitely worth reading through if you're serious about writing an AI), while Wikipedia offers a good overview on the subject.
It's important to be very thoughtful when choosing the appropiate board representation - they all offer their own unique advantages (and pitfalls) - largely to do with speed/perform of certain operations such as performing moves and evaluating the board state (typically ranging from O(1) to O(n) time complexity depending on the method and task). As far as I know, there is still no consensus on the "best" board representation, though some are generally preferred over others nowadays (bitboards are almost a must-have for example). This is why it is common for most strong chess AIs to use several (up to 4 or 5 even) different board representations when searching for moves.
我建议使用 64 个项目的数组,例如:
这样您只需要用一个字节来表示棋盘位置,速度要快得多。
在处理单个索引来参考棋盘位置时,必须了解某些事情才能使事情变得更轻松。 例如,您如何知道两个位置都在同一行或同一列? 有一个简单的技巧可以解决这个问题。
行
要计算某个位置的行,请将位置除以 8 并取结果的整数部分。 例如,位置 63 除以 8 为 7.875,等于第 7 行。位置 3 除以 8 为 0.375,因此为 0。在 C# 中,通过转换为整数,您将始终只得到数字的整数部分,因此:
Column< /strong>
要计算位置的列,您可以通过执行位置模 8 来使用模运算符。例如,位置 24 模 8 是列 0。位置 15 模 8 是 7,因此
有了这两个概念,我们可以转换任何位置在我们的 64 方板上排列成一列和一排。
如果您想了解有关创建自己的国际象棋引擎的更多信息,请查看 http://www.chessbin.com
I would suggest an array of 64 items such as:
This way you only need to represent a chess board position by a single byte, it’s much faster.
When dealing with a single index to reference chess board positions there are certain things that one must know to make life easier. For example how do you know that two positions are both on the same row or column? There is an easy trick to figure that out.
Row
To figure out the row of a position you divide the position by 8 and take the integer portion of the result. For example position 63 divided by 8 is 7.875 which equals row 7. Position 3 divided by 8 is 0.375 so 0. In C# by casting to an integer you will always get just the integer portion of the number, hence:
Column
To figure out the column of a position you use the modulus operator by performing position modulus 8. For example position 24 modulus 8 is column 0. Position 15 modulus 8 is 7, hence
Armed with these two concepts we can convert any position on our 64 square board to a column and row.
If you would like to learn more about creating your own chess engine have a look at http://www.chessbin.com