Replaced all std::cout with kDebug.
[tagua/yd.git] / src / gnushogiengine.cpp
blob148319573857d13295852e909d05945ad1f166f9
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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 "gnushogiengine.h"
13 #include <QFileInfo>
14 #include <QRegExp>
15 #include <KDebug>
16 #include "enginenotifier.h"
17 #include "foreach.h"
19 using namespace boost;
21 QRegExp GNUShogiEngine::m_move_pattern("[0-9]*\\. \\.\\.\\. ([^ ]*) .*");
23 GNUShogiEngine::GNUShogiEngine(const QString& path, const QStringList& arguments)
24 : Engine(path, arguments)
25 , m_analysing(false) {
26 // set default features
27 m_features.ping = false;
28 m_features.setboard = false;
29 m_features.playother = false;
30 m_features.san = false;
31 m_features.usermove = false;
32 m_features.time = true;
33 m_features.draw = true;
34 m_features.sigint = true;
35 m_features.sigterm = true;
36 m_features.reuse = true;
37 m_features.analyze = false;
38 m_features.colors = true;
39 m_features.ics = false;
40 m_features.name = false;
41 m_features.pause = false;
43 m_features.myname = QFileInfo(m_path).baseName();
45 connect(this, SIGNAL(receivedCommand(const QString&)),
46 this, SLOT(processCommand(const QString&)));
49 GNUShogiEngine::~GNUShogiEngine() {
50 kDebug() << "[debug] destroying engine";
51 stop();
52 m_engine.kill();
55 void GNUShogiEngine::initializeEngine() {
56 // nothing special to do
59 void GNUShogiEngine::reset() {
60 sendCommand("new"); // start a new game
61 sendCommand("force"); // do not think
64 void GNUShogiEngine::play() {
65 sendCommand("go"); // start playing
68 void GNUShogiEngine::stop() {
69 sendCommand("quit");
70 if (m_features.sigterm)
71 m_engine.kill();
74 void GNUShogiEngine::processCommand(const QString& command) {
75 //QStringList arg_list = command.split(QRegExp("\\s+"));
76 //QString cmd = arg_list.takeFirst();
78 if (m_move_pattern.indexIn(command) == 0) {
79 if (shared_ptr<EngineNotifier> notifier = m_notifier.lock())
80 notifier->notifyEngineMove(m_move_pattern.cap(1));
84 void GNUShogiEngine::sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref) {
85 QString move_str = move->toString("simple", ref);
86 sendCommand(move_str);
89 void GNUShogiEngine::backUp(AbstractPosition::Ptr) {
90 sendCommand("undo");
93 void GNUShogiEngine::startAnalysis() {
94 sendCommand("post");
95 sendCommand("analyze");
96 m_analysing = true;
99 void GNUShogiEngine::stopAnalysis() {
100 sendCommand("exit");
101 m_analysing = false;
104 void GNUShogiEngine::setBoard(AbstractPosition::Ptr, int, int) {
105 #if 0
106 if (m_features.setboard) {
107 sendCommand(QString("setboard %1").arg(pos->fen(halfmove, fullmove)));
109 else {
110 // this is pretty meaningless for generic variants
111 if (pos->turn() != 0) {
112 sendCommand("new");
113 sendCommand("a2a3");
116 sendCommand("edit");
117 sendCommand("#");
118 Point size = pos->size();
119 for (int i = 0; i < size.x; i++) {
120 for (int j = 0; j < size.y; j++) {
121 Point p(i, j);
122 AbstractPiece::Ptr piece = pos->get(p);
123 if (piece) sendCommand(QString("%1%2")
124 .arg(piece->type())
125 .arg(p.toString(8)));
128 sendCommand(".");
130 #endif