Tentative Randomless-Entropy variant.
[tagua/yd.git] / src / gnushogiengine.cpp
blob4f5d151e9c653d22574b6e3411ca5947f316c2c1
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>
16 #include <core/moveserializer.h>
17 #include "components.h"
18 #include <KDebug>
19 #include "enginenotifier.h"
20 #include "foreach.h"
22 using namespace boost;
24 QRegExp GNUShogiEngine::m_move_pattern("[0-9]*\\. \\.\\.\\. ([^ ]*) .*");
26 GNUShogiEngine::GNUShogiEngine(Components* components,
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 = components->moveSerializer("simple");
56 GNUShogiEngine::~GNUShogiEngine() {
57 kDebug() << "[debug] destroying engine";
58 stop();
59 m_engine.kill();
62 void GNUShogiEngine::initializeEngine() {
63 // nothing special to do
66 void GNUShogiEngine::reset() {
67 sendCommand("new"); // start a new game
68 sendCommand("force"); // do not think
71 void GNUShogiEngine::play() {
72 sendCommand("go"); // start playing
75 void GNUShogiEngine::stop() {
76 sendCommand("quit");
77 if (m_features.sigterm)
78 m_engine.kill();
81 void GNUShogiEngine::processCommand(const QString& command) {
82 //QStringList arg_list = command.split(QRegExp("\\s+"));
83 //QString cmd = arg_list.takeFirst();
85 if (m_move_pattern.indexIn(command) == 0) {
86 if (shared_ptr<EngineNotifier> notifier = m_notifier.lock())
87 notifier->notifyEngineMove(m_move_pattern.cap(1));
91 void GNUShogiEngine::sendMove(const Move& move, const StatePtr& ref) {
92 QString move_str = m_serializer->serialize(move, ref.get());
93 sendCommand(move_str);
96 void GNUShogiEngine::backUp(const StatePtr&) {
97 sendCommand("undo");
100 void GNUShogiEngine::startAnalysis() {
101 sendCommand("post");
102 sendCommand("analyze");
103 m_analysing = true;
106 void GNUShogiEngine::stopAnalysis() {
107 sendCommand("exit");
108 m_analysing = false;
111 void GNUShogiEngine::setBoard(const StatePtr&) {
112 #if 0
113 if (m_features.setboard) {
114 sendCommand(QString("setboard %1").arg(pos->fen(halfmove, fullmove)));
116 else {
117 // this is pretty meaningless for generic variants
118 if (pos->turn() != 0) {
119 sendCommand("new");
120 sendCommand("a2a3");
123 sendCommand("edit");
124 sendCommand("#");
125 Point size = pos->size();
126 for (int i = 0; i < size.x; i++) {
127 for (int j = 0; j < size.y; j++) {
128 Point p(i, j);
129 AbstractPiece::Ptr piece = pos->get(p);
130 if (piece) sendCommand(QString("%1%2")
131 .arg(piece->type())
132 .arg(p.toString(8)));
135 sendCommand(".");
137 #endif