Cleaned up CrazyhouseMove.
[tagua/yd.git] / src / xboardengine.cpp
blob083510b675e5502da1b987f3bdda3c593c248974
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "xboardengine.h"
12 #include "enginenotifier.h"
14 #include <QFileInfo>
15 #include <QRegExp>
16 #include <iostream>
18 using namespace boost;
20 QRegExp XBoardEngine::m_move_pattern("My move is: (.*)");
22 XBoardEngine::XBoardEngine(const QString& path, const QStringList& arguments)
23 : Engine(path, arguments)
24 , m_analysing(false) {
25 // set default features
26 m_features.ping = false;
27 m_features.setboard = false;
28 m_features.playother = false;
29 m_features.san = false;
30 m_features.usermove = false;
31 m_features.time = true;
32 m_features.draw = true;
33 m_features.sigint = true;
34 m_features.sigterm = true;
35 m_features.reuse = true;
36 m_features.analyze = true;
37 m_features.colors = true;
38 m_features.ics = false;
39 m_features.name = false;
40 m_features.pause = false;
42 m_features.myname = QFileInfo(m_path).baseName();
44 connect(this, SIGNAL(receivedCommand(const QString&)),
45 this, SLOT(processCommand(const QString&)));
48 XBoardEngine::~XBoardEngine() {
49 std::cout << "[debug] destroying engine" << std::endl;
50 stop();
51 m_engine.terminate();
54 void XBoardEngine::initializeEngine() {
55 // set up in xboard mode
56 sendCommand("xboard");
58 // we're using xboard protocol version 2
59 sendCommand("protover 2");
61 // don't flood me!
62 sendCommand("nopost");
65 void XBoardEngine::reset(int side) {
66 sendCommand("force");
67 sendCommand("new");
68 if (side == 0 && !m_analysing) sendCommand("go");
71 void XBoardEngine::stop() {
72 sendCommand("quit");
73 if (m_features.sigterm)
74 m_engine.kill();
77 void XBoardEngine::processCommand(const QString& command) {
78 QStringList arg_list = command.split(QRegExp("\\s+"));
79 QString cmd = arg_list.takeFirst();
81 if (cmd == "feature") {
82 QRegExp feature("(\\S+)=(\\S+)");
83 foreach (QString arg, arg_list) {
84 if (feature.indexIn(arg) == 0) {
85 bool rejected = false;
87 QString key = feature.cap(1);
88 QString value = feature.cap(2);
90 if (key == "ping")
91 m_features.ping = value == "1";
92 else if (key == "setboard")
93 m_features.setboard = value == "1";
94 else if (key == "playother")
95 m_features.playother = value == "1";
96 else if (key == "san")
97 m_features.san = value == "1";
98 else if (key == "usermove")
99 m_features.usermove = value == "1";
100 else if (key == "time")
101 m_features.time = value == "1";
102 else if (key == "draw")
103 m_features.draw = value == "1";
104 else if (key == "sigint")
105 m_features.sigint = value == "1";
106 else if (key == "sigterm")
107 m_features.sigterm = value == "1";
108 else if (key == "reuse")
109 m_features.reuse = value == "1";
110 else if (key == "analyze")
111 m_features.analyze = value == "1";
112 else if (key == "myname")
113 m_features.myname = value.mid(1, value.length() - 2);
114 else if (key == "variants")
115 m_features.variants = value.mid(1, value.length() - 2);
116 else if (key == "colors")
117 m_features.colors = value == "1";
118 else if (key == "ics")
119 m_features.ics = value == "1";
120 else if (key == "name")
121 m_features.name = value == "1";
122 else if (key == "pause")
123 m_features.pause = value == "1";
124 else if (key == "done")
125 m_features.done = value == "1";
126 else
127 rejected = true;
129 if (rejected)
130 sendCommand("rejected " + key);
131 else
132 sendCommand("accepted " + key);
136 else if (cmd == "move") {
137 if (shared_ptr<EngineNotifier> notifier = m_notifier.lock())
138 notifier->notifyEngineMove(arg_list[0]);
140 else if (m_move_pattern.indexIn(command) == 0) {
141 if (shared_ptr<EngineNotifier> notifier = m_notifier.lock())
142 notifier->notifyEngineMove(m_move_pattern.cap(1));
146 void XBoardEngine::sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref) {
147 QString move_str = m_features.san ? move->SAN(ref) : move->toString(ref);
148 if (m_features.usermove)
149 move_str = "usermove " + move_str;
150 sendCommand(move_str);
153 void XBoardEngine::backUp(AbstractPosition::Ptr) {
154 sendCommand("undo");
157 void XBoardEngine::startAnalysis() {
158 sendCommand("post");
159 sendCommand("analyze");
160 m_analysing = true;
163 void XBoardEngine::stopAnalysis() {
164 sendCommand("exit");
165 m_analysing = false;
168 void XBoardEngine::setBoard(AbstractPosition::Ptr pos, int halfmove, int fullmove) {
169 if (m_features.setboard) {
170 sendCommand(QString("setboard %1").arg(pos->fen(halfmove, fullmove)));
172 else {
173 // this is pretty meaningless for generic variants
174 #if 0
175 if (pos->turn() != 0) {
176 sendCommand("new");
177 sendCommand("a2a3");
180 sendCommand("edit");
181 sendCommand("#");
182 Point size = pos->size();
183 for (int i = 0; i < size.x; i++) {
184 for (int j = 0; j < size.y; j++) {
185 Point p(i, j);
186 AbstractPiece::Ptr piece = pos->get(p);
187 if (piece) sendCommand(QString("%1%2")
188 .arg(piece->type())
189 .arg(p.toString(8)));
192 sendCommand(".");
193 #endif