Variants are not singletons anymore.
[tagua/yd.git] / src / gnushogiengine.cpp
bloba48b7636075645574339e2c758376bca20b65420
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 "gnushogiengine.h"
13 #include <QFileInfo>
14 #include <QRegExp>
15 #include <iostream>
17 #include "enginenotifier.h"
18 #include "foreach.h"
20 using namespace boost;
22 QRegExp GNUShogiEngine::m_move_pattern("[0-9]*\\. \\.\\.\\. ([^ ]*) .*");
24 GNUShogiEngine::GNUShogiEngine(const QString& path, const QStringList& arguments)
25 : Engine(path, arguments)
26 , m_analysing(false) {
27 // set default features
28 m_features.ping = false;
29 m_features.setboard = false;
30 m_features.playother = false;
31 m_features.san = false;
32 m_features.usermove = false;
33 m_features.time = true;
34 m_features.draw = true;
35 m_features.sigint = true;
36 m_features.sigterm = true;
37 m_features.reuse = true;
38 m_features.analyze = false;
39 m_features.colors = true;
40 m_features.ics = false;
41 m_features.name = false;
42 m_features.pause = false;
44 m_features.myname = QFileInfo(m_path).baseName();
46 connect(this, SIGNAL(receivedCommand(const QString&)),
47 this, SLOT(processCommand(const QString&)));
50 GNUShogiEngine::~GNUShogiEngine() {
51 std::cout << "[debug] destroying engine" << std::endl;
52 stop();
53 m_engine.kill();
56 void GNUShogiEngine::initializeEngine() {
57 // nothing special to do
60 void GNUShogiEngine::reset() {
61 sendCommand("new"); // start a new game
62 sendCommand("force"); // do not think
65 void GNUShogiEngine::play() {
66 sendCommand("go"); // start playing
69 void GNUShogiEngine::stop() {
70 sendCommand("quit");
71 if (m_features.sigterm)
72 m_engine.kill();
75 void GNUShogiEngine::processCommand(const QString& command) {
76 //QStringList arg_list = command.split(QRegExp("\\s+"));
77 //QString cmd = arg_list.takeFirst();
79 if (m_move_pattern.indexIn(command) == 0) {
80 if (shared_ptr<EngineNotifier> notifier = m_notifier.lock())
81 notifier->notifyEngineMove(m_move_pattern.cap(1));
85 void GNUShogiEngine::sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref) {
86 QString move_str = move->toString(ref);
87 sendCommand(move_str);
90 void GNUShogiEngine::backUp(AbstractPosition::Ptr) {
91 sendCommand("undo");
94 void GNUShogiEngine::startAnalysis() {
95 sendCommand("post");
96 sendCommand("analyze");
97 m_analysing = true;
100 void GNUShogiEngine::stopAnalysis() {
101 sendCommand("exit");
102 m_analysing = false;
105 void GNUShogiEngine::setBoard(AbstractPosition::Ptr pos, int halfmove, int fullmove) {
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 0
112 if (pos->turn() != 0) {
113 sendCommand("new");
114 sendCommand("a2a3");
117 sendCommand("edit");
118 sendCommand("#");
119 Point size = pos->size();
120 for (int i = 0; i < size.x; i++) {
121 for (int j = 0; j < size.y; j++) {
122 Point p(i, j);
123 AbstractPiece::Ptr piece = pos->get(p);
124 if (piece) sendCommand(QString("%1%2")
125 .arg(piece->type())
126 .arg(p.toString(8)));
129 sendCommand(".");
130 #endif