如何将国际象棋 AI 移植到 iPhone
我想将国际象棋人工智能移植到 iPhone,但我不知道该怎么做。显然 iPhone 不支持多线程,所以你不能单独编译 AI,而必须以某种方式将其合并到代码中。
我有 sjeng 引擎实现的 GPL 副本,但我不知道他们是如何做到的,因为它是用 c 和 c++ 编写的,而我只知道 apple objc。
有人对如何做到这一点有任何建议吗?我需要为独立程序制作某种包装。
包含 代码 的文件,我会尽可能长时间地保留该文件。
I would like to port a chess AI to iPhone, but I can't figure out how to do it. Apparently iPhone doesn't support multi threading so you can't just seperately compile the AI, but have to somehow merge it into the code.
I have a GPL copy of a implementation of the sjeng engine, but I can't figure out how they did it because it's written in c and c++ and all I know is apple objc.
Does anyone have any recommendations on how to do this? I need to make a wrapper of some kind for what is a standalone program.
file with code which I will leave up for as long as I can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已成功将国际象棋引擎移植到 iPhone 上。移植起来并不困难。大多数流行的国际象棋引擎都采用通用国际象棋接口协议或国际象棋引擎通信协议。阅读维基百科以获取有关每个协议的更多详细信息。
现在,假设您采用开源 UCI 国际象棋引擎之一,它可以在 C 或 C++ 上编译。 XCode 本身支持 C 和 C++,因此您只需将源代码复制到 XCode 即可编译。
下一阶段是将引擎连接到您的界面。再说一次,这并不困难。您需要将协议命令发送到引擎,引擎将在标准输出上返回结果。您需要使用 UNIX 的 pipeline() 对结果进行管道传输。阅读我的其他主题
在 iPhone 上使用 Fork() 了解更多详细信息。
示例:
假设 engine_loop 是您引擎的游戏循环(所有引擎都必须有一个循环)。
该代码片段向您展示了如何将结果从引擎发送到您的界面。现在,您需要采取相反的方法。这与上面的代码非常相似。在实际场景中,一旦建立连接,您将需要发送UCI命令,我举个例子:
请仔细阅读UCI国际象棋引擎协议文档。你会需要它。
I have successfully ported a chess engine to iPhone. It's not difficult for the porting. Most of the popular chess engines adopt Universal Chess Interface Protocol or Chess Engine Communication Protocol. Read Wikipedia for more details on each protocol.
Now, say you take one of the open sources UCI chess engine and it compiles on C or C++. XCode supports C and C++ natively, so all you will need to do is copy the sources to XCode and they will compile.
The next phase would be connecting the engine to your interface. Again, this is not difficult. You will need to send protocol commands to the engine, the engine would give you back the results on standard output. You would need to pipe results using UNIX's pipe(). Read my other thread
Fork() on iPhone for more details.
Example:
Assume engine_loop is the game loop for your engine (all engines must have a loop).
The code fragment shows you how to send results from an engine to your interface. Now, you will need to do the other way around. This is very similar to the code above. In a real scenario, once your connection is established, you will need to send UCI commands, I will give you an example:
Please read the UCI chess engine protocol documentation carefully. You will need it.
你的问题实际上比需要的更复杂,因为我认为你遇到的基本问题与专门为 iPhone 进行编译无关。
如果你说你已经有了一些国际象棋人工智能代码,那么其中的某个地方会调用一个评估函数,该函数获取游戏状态(棋盘位置和要移动的玩家)并返回一个移动。这就是您需要深入研究并找到的内容,因为它本质上是驱动您的应用程序的“引擎”,无论您正在为哪个平台进行编译。
现在,我的猜测是,这个国际象棋人工智能假设走法搜索在自己的线程中运行,这可能是一个设计决策,可以轻松地随时“中断”搜索并让它发挥自己的走法。您当然可以在 iPhone 上的单独线程中运行代码,因此您面临的问题是弄清楚如何梳理该代码,以使其摆脱现有的平台依赖关系。
它可能会帮助您首先解决这个问题,就好像您正在用 C 编写一个命令行实用程序以在 Mac OS X 上运行一样。这将使您摆脱许多依赖项并简化您的情况。我的猜测是,一旦您完成了此操作,您将立即了解如何使其在(Cocoa)iPhone 环境中工作。
Your question is actually more complicated than it needs to be, because I think the basic problem you have is unrelated to compiling specifically for the iPhone.
If you say you already have some chess AI code, then somewhere in there is a call to an evaluation function that takes a game state (board position and player to move) and will give back a move. That's what you need to drill down and find, because that essentially is the "engine" that will drive your app, regardless of what platform you're compiling for.
Now, my guess is that this chess AI assumes that move search is run in its own thread, likely a design decision to make it easy to "interrupt" the search at any time and have it play its own move. You can certainly run code in separate threads on the iPhone, so the problem for you is to figure out how to tease out that code from to free it from whatever existing platform dependencies it has.
It may help you to first approach this problem as if you were writing a command-line utility, in C, to run on Mac OS X. That will free you from a lot of dependencies and simplify the situation for you. My guess is once you've done that, you'll immediately have a good idea of how to make it work within a (Cocoa) iPhone environment.
Objective C 是 C 的超集,因此如果该库是用 C 编写的,您可以使用其余的 Objective-C 代码对其进行编译,而无需任何包装器。
Objective C is a superset of C, so if the library is written in C, you can just compile it with the rest of your Objective-C code without any wrappers.
iPhone OS确实支持多线程;请参阅 NSThread 类。不过,你不会从中获得任何额外的性能——iPhone CPU 是单核的,而且几乎没有多任务处理。
是的,您可以从 Objective C 代码轻松调用 C 和 C++ 代码,反之亦然。对于 C++ 交互,请将源文件重命名为 .mm;然后它们被编译为 Objective C++。
我有丰富的将 C++ 代码移植到 iPhone 的经验。就像魅力一样。
iPhone OS does support multithreading; see NSThread class. You won't get any extra performance from it, though - the iPhone CPU is single-core and there's almost no multitasking.
And yes, from the Objective C code you can easily call into C and C++ code, and vice versa. For C++ interaction, rename your sources to .mm; then they're compiled as Objective C++.
I have ample experience porting C++ code to iPhone. Works like a charm.