2
* noughtsandcrossesplugin.cpp - Psi plugin to play noughts and crosses
3
* Copyright (C) 2006 Kevin Smith
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* You can also redistribute and/or modify this program under the
11
* terms of the Psi License, specified in the accompanied COPYING
12
* file, as published by the Psi Project; either dated January 1st,
13
* 2005, or (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
#include "psiplugin.h"
32
class NoughtsAndCrossesPlugin : public PsiPlugin
35
Q_INTERFACES(PsiPlugin)
38
NoughtsAndCrossesPlugin();
39
virtual QString name() const;
40
virtual void message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay);
41
virtual QString shortName() const;
42
virtual QString version() const;
46
void myTurn(int space);
47
void theirTurn(int space);
48
void gameOver(TicTacGameBoard::State state);
51
void startGame(QString jid, int size, bool meFirst, const PsiAccount* account);
60
Q_EXPORT_PLUGIN(NoughtsAndCrossesPlugin);
62
NoughtsAndCrossesPlugin::NoughtsAndCrossesPlugin() : PsiPlugin()
67
QString NoughtsAndCrossesPlugin::name() const
69
return "NoughtsAndCrosses Plugin";
72
QString NoughtsAndCrossesPlugin::shortName() const
74
return "noughtsandcrosses";
77
QString NoughtsAndCrossesPlugin::version() const
82
void NoughtsAndCrossesPlugin::message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay)
85
qDebug("naughtsandcrosses message");
86
if (!message.startsWith("noughtsandcrosses"))
88
qDebug("message for us in noughtsandcrosses");
89
if (game && fromJid != playingWith)
91
reply=QString("<message to=\"%1\" type=\"chat\"><body>already playing with %2, sorry</body></message>").arg(fromJid).arg(playingWith);
92
emit sendStanza(account, reply);
95
QString command = QString(message);
97
qDebug() << (qPrintable(QString("noughtsandcrosses command string %1").arg(command)));
98
if (command == QString("start"))
102
qWarning(qPrintable(QString("Received message '%1', launching nac with %2").arg(message).arg(fromDisplay)));
104
reply=QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses starting</body></message>").arg(fromJid);
105
emit sendStanza(account, reply);
106
startGame(fromJid, 3, false, account);
108
else if (command == QString("starting"))
112
qWarning(qPrintable(QString("Received message '%1', launching nac with %2").arg(message).arg(fromDisplay)));
114
reply=QString("<message to=\"%1\" type=\"chat\"><body>starting noughts and crosses, I go first :)</body></message>").arg(fromJid);
115
emit sendStanza(account, reply);
116
startGame(fromJid, 3, true, account);
122
else if (command.startsWith("move"))
126
int space=command.toInt();
127
qDebug() << (qPrintable(QString("noughtsandcrosses move to space %1").arg(space)));
132
void NoughtsAndCrossesPlugin::startGame(QString jid, int size, bool meFirst, const PsiAccount* account)
134
game = new TicTacToe( meFirst, size );
135
game->setCaption(QString("Noughts and Crosses with %1").arg(jid));
138
account_=(PsiAccount*)account;
139
connect(game, SIGNAL(closing()), this, SLOT(stopGame()));
140
connect(game, SIGNAL(myMove(int)), this, SLOT(myTurn(int)));
141
connect(game, SIGNAL(gameOverSignal(TicTacGameBoard::State)), this, SLOT(gameOver(TicTacGameBoard::State)));
144
void NoughtsAndCrossesPlugin::stopGame()
150
void NoughtsAndCrossesPlugin::gameOver(TicTacGameBoard::State state)
156
case TicTacGameBoard::HumanWon:
159
case TicTacGameBoard::ComputerWon:
162
case TicTacGameBoard::NobodyWon:
163
winner="It was a draw, no-one";
168
reply=QString("<message to=\"%1\" type=\"chat\"><body>%2 won. Good game.</body></message>").arg(playingWith).arg(winner);
169
emit sendStanza(account_, reply);
172
void NoughtsAndCrossesPlugin::myTurn(int space)
174
qDebug() << (qPrintable(QString("my turn: %1").arg(space)));
178
reply=QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses move %2</body></message>").arg(playingWith).arg(space);
179
emit sendStanza(account_, reply);
182
void NoughtsAndCrossesPlugin::theirTurn(int space)
184
qDebug() << (qPrintable(QString("their turn: %1").arg(space)));
187
game->theirMove(space);
191
#include "noughtsandcrossesplugin.moc"