Initial porting to the new component API.
[tagua/yd.git] / src / gnushogiengine.cpp
bloba0739c63ac29c8c439804561b5ece40d3ca81780
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 <iostream>
17 #include <core/variant.h>
18 #include <core/moveserializer.h>
19 #include "enginenotifier.h"
20 #include "foreach.h"
22 using namespace boost;
24 QRegExp GNUShogiEngine::m_move_pattern("[0-9]*\\. \\.\\.\\. ([^ ]*) .*");
26 GNUShogiEngine::GNUShogiEngine(Variant* variant,
27 const QString& path,
28 const QStringList& arguments)
29 : Engine(path, arguments)
30 , m_analysing(false) {
31 // set default features
32 m_features.ping = false;
33 m_features.setboard = false;
34 m_features.playother = false;
35 m_features.san = false;
36 m_features.usermove = false;
37 m_features.time = true;
38 m_features.draw = true;
39 m_features.sigint = true;
40 m_features.sigterm = true;
41 m_features.reuse = true;
42 m_features.analyze = false;
43 m_features.colors = true;
44 m_features.ics = false;
45 m_features.name = false;
46 m_features.pause = false;
48 m_features.myname = QFileInfo(m_path).baseName();
50 connect(this, SIGNAL(receivedCommand(const QString&)),
51 this, SLOT(processCommand(const QString&)));
53 m_serializer = requestComponent<IMoveSerializer>(
54 variant, "moveserializer/simple");
57 GNUShogiEngine::~GNUShogiEngine() {
58 std::cout << "[debug] destroying engine" << std::endl;
59 stop();
60 m_engine.kill();
63 void GNUShogiEngine::initializeEngine() {
64 // nothing special to do
67 void GNUShogiEngine::reset() {
68 sendCommand("new"); // start a new game
69 sendCommand("force"); // do not think
72 void GNUShogiEngine::play() {
73 sendCommand("go"); // start playing
76 void GNUShogiEngine::stop() {
77 sendCommand("quit");
78 if (m_features.sigterm)
79 m_engine.kill();
82 void GNUShogiEngine::processCommand(const QString& command) {
83 //QStringList arg_list = command.split(QRegExp("\\s+"));
84 //QString cmd = arg_list.takeFirst();
86 if (m_move_pattern.indexIn(command) == 0) {
87 if (shared_ptr<EngineNotifier> notifier = m_notifier.lock())
88 notifier->notifyEngineMove(m_move_pattern.cap(1));
92 void GNUShogiEngine::sendMove(const Move& move, const StatePtr& ref) {
93 QString move_str = m_serializer->serialize(move, ref.get());
94 sendCommand(move_str);
97 void GNUShogiEngine::backUp(const StatePtr&) {
98 sendCommand("undo");
101 void GNUShogiEngine::startAnalysis() {
102 sendCommand("post");
103 sendCommand("analyze");
104 m_analysing = true;
107 void GNUShogiEngine::stopAnalysis() {
108 sendCommand("exit");
109 m_analysing = false;
112 void GNUShogiEngine::setBoard(const StatePtr&) {
113 #if 0
114 if (m_features.setboard) {
115 sendCommand(QString("setboard %1").arg(pos->fen(halfmove, fullmove)));
117 else {
118 // this is pretty meaningless for generic variants
119 if (pos->turn() != 0) {
120 sendCommand("new");
121 sendCommand("a2a3");
124 sendCommand("edit");
125 sendCommand("#");
126 Point size = pos->size();
127 for (int i = 0; i < size.x; i++) {
128 for (int j = 0; j < size.y; j++) {
129 Point p(i, j);
130 AbstractPiece::Ptr piece = pos->get(p);
131 if (piece) sendCommand(QString("%1%2")
132 .arg(piece->type())
133 .arg(p.toString(8)));
136 sendCommand(".");
138 #endif