summaryrefslogtreecommitdiff
path: root/noncore/games/chess/chess.cpp
blob: 96a838ac649f322a63c91744ed3dff3dcd77c295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/


// human is not offered a promotion yet

#include <qcanvas.h>
#include <qmainwindow.h>
#include <qlist.h>
#include <qimage.h>
#include <qpainter.h>
#include <qmessagebox.h>
#include <qregexp.h>

#include <qpe/config.h>
#include <qpe/resource.h>

#include "chess.h"

#define CHESS_DEBUG

int pieceSize = 40;
static QVector < QImage > imgList;
int timeMoves, timeTime;

int BoardView::convertToRank(int r)
{
    r = r / pieceSize;
    if (humanSide == sideWhite)
	r = 8 - r;
    else
	r++;
    return r;
}

char BoardView::convertToFile(int f)
{
    f = f / pieceSize;
    if (humanSide == sideWhite)
	return f + 'a';
    else
	return 'h' - f;
}

int BoardView::convertFromFile(char f)
{
    if (humanSide == sideWhite)
	f = f - 'a';
    else
	f = 'h' - f;
    return f * pieceSize;
}

int BoardView::convertFromRank(int r)
{
    if (humanSide == sideWhite)
	r = 8 - r;
    else
	r--;
    return r * pieceSize;
}

// Pieces
Piece::Piece(QCanvas * canvas, int t):QCanvasRectangle(canvas)
{
    type = t;
    setSize(pieceSize, pieceSize);
    show();
}

Piece *BoardView::newPiece(int t, char f, int r)
{
    Piece *tmpPiece = new Piece(canvas(), t);
    tmpPiece->move(convertFromFile(f), convertFromRank(r));
    list.append(tmpPiece);
    return tmpPiece;
}

void BoardView::deletePiece(Piece * p)
{
    list.remove(p);
    canvas()->update();
}

void Piece::drawShape(QPainter & p)
{
    p.drawImage(int (x()), int (y()), *(imgList[type]));
}

void BoardView::buildImages(QImage theme)
{
    imgList.resize(12);
    int x;
    int y = 0;

    for (int j = 0; j < 2; j++) {
	x = 0;
	for (int i = 0; i < 6; i++) {
	    imgList.insert(i + (j * 6),
			   new QImage(theme.
				      copy(x, y, pieceSize, pieceSize)));
	    x += pieceSize;
	}
	y += pieceSize;
    }
}

void BoardView::readStdout()
{
    QString input( crafty->readStdout() );
#ifdef CHESS_DEBUG
    qDebug("received this string from crafty->\n%s\n", input.latin1());
#endif

    int startPosition = input.find("setboard");
    if (startPosition != -1)
	decodePosition(input.remove(0, startPosition + 9));

    if (input.contains("Black mates")) {
	playingGame = FALSE;
	emit(showMessage("Black mates"));
    } else if (input.contains("White mates")) {
	playingGame = FALSE;
	emit(showMessage("White mates"));
    } else if (input.contains(" resigns")) {
	playingGame = FALSE;
	emit(showMessage("Computer resigns"));
    } else if (input.contains("Draw")) {
	playingGame = FALSE;
	emit(showMessage("Draw"));
    }
}

// this is pretty close to getting done right
// maybe dont use sprites and just draw a picture
// there'll be lots of drawing done anyway
// eg creating pictures for the webpages,
// and presenting options for promotions
void BoardView::decodePosition(const QString & t)
{
    qDebug("decode copped %s \n", t.latin1());

    int count = 0;
    int stringPos = 0;
    for (int file = 0; file < 8; file++) {
	for (int rank = 0; rank < 8; rank++) {
	    if (count)
		count--;
	    else {
		if (t.at(stringPos).isNumber())
		    count = t.at(stringPos).digitValue();
		else {
		    newPiece(t.at(stringPos).latin1(), 'a' + file,
			     rank + 1);
		}
	    }
	}
    }
}

void BoardView::undo()
{
    crafty->writeToStdin("undo\n");
    crafty->writeToStdin("savepos\nclock\n");
}

void BoardView::emitErrorMessage()
{
    if (activeSide != humanSide)
	emit(showMessage("Not your move"));
    else
	emit(showMessage("You are not playing a game"));
}

void BoardView::annotateGame()
{
    crafty->
	writeToStdin
	("savegame game.save\nannotateh game.save bw 0 1.0 1\n");
    emit(showMessage("Annotating game"));
}

Piece *BoardView::findPiece(char f, int r)
{
    QListIterator < Piece > it(list);
    Piece *tmpPiece;
    for (; it.current(); ++it) {
	tmpPiece = it.current();
	if (convertToRank(tmpPiece->x()) == r
	    && convertToFile(tmpPiece->y()) == f)
	    return tmpPiece;
    }
    return 0;
}

void BoardView::newGame()
{
    activeSide = sideWhite;
    emit(showMessage("New game"));
    crafty->writeToStdin("new\n");
    crafty->writeToStdin("savepos\n");
    crafty->writeToStdin("time " +
			 QString::number(timeMoves) +
			 "/" + QString::number(timeTime) + "\n");
    activeSide = sideWhite;
    if (humanSide == sideBlack)
	crafty->writeToStdin("go\n");
}

void BoardView::setTheme(QString filename)
{
    QImage theme = Resource::loadImage(QString("chess/") + filename);
    pieceSize = theme.height() / 2;
    setFrameStyle(QFrame::Plain);
    setFixedSize(8 * pieceSize, 8 * pieceSize);
    canvas()->setBackgroundColor(Qt::red);
    canvas()->resize(8 * pieceSize, 8 * pieceSize);
    whiteSquare = theme.copy(6 * pieceSize, 0, pieceSize, pieceSize);
    activeWhiteSquare = theme.copy(7 * pieceSize, 0, pieceSize, pieceSize);
    blackSquare =
	theme.copy(6 * pieceSize, pieceSize, pieceSize, pieceSize);
    activeBlackSquare =
	theme.copy(7 * pieceSize, pieceSize, pieceSize, pieceSize);
    buildImages(theme);
    drawBackgroundImage(QPoint(-1, -1));
}


// sets the bg to the default background image for the current theme
// also resposible for drawing the "active" marker
void BoardView::drawBackgroundImage(QPoint activeSquare)
{
    bg = QPixmap(8 * pieceSize, 8 * pieceSize);
    QPainter p(&bg);
    bool col = FALSE;
    for (int i = 0; i < 8; i++) {
	for (int j = 0; j < 8; j++) {
	    QPoint point(i * pieceSize, j * pieceSize);
	    if (col) {
		if (point.x() == activeSquare.x()
		    && point.y() == activeSquare.y())
		    p.drawImage(point, activeBlackSquare);
		else
		    p.drawImage(point, blackSquare);
		col = FALSE;
	    } else {
		if (point.x() == activeSquare.x()
		    && point.y() == activeSquare.y())
		    p.drawImage(point, activeWhiteSquare);
		else
		    p.drawImage(point, whiteSquare);
		col = TRUE;
	    }
	}
	col = !col;
    }
    canvas()->setBackgroundPixmap(bg);
    canvas()->update();
}


// Board view widget
void BoardView::contentsMousePressEvent(QMouseEvent * e)
{
    QCanvasItemList cList = canvas()->collisions(e->pos());
    if (activeSide == humanSide && playingGame) {
	if (!activePiece) {
	    if (cList.count()) {
		activePiece = (Piece *) (*(cList.at(0)));
		drawBackgroundImage(QPoint
				    (activePiece->x(), activePiece->y()));
	    }
	} else {
	    if (!(activePiece == (Piece *) (*(cList.at(0))))) {
		char fromFile = convertToFile(activePiece->x());
		int fromRank = convertToRank(activePiece->y());
		char toFile = convertToFile(e->pos().x());
		int toRank = convertToRank(e->pos().y());
		QString moveS;
		moveS.append(fromFile);
		moveS.append(moveS.number(fromRank));
		moveS.append(toFile);
		moveS.append(moveS.number(toRank));
		if ((activePiece->type == wPawn
		     && fromRank == 7 && toRank == 8)
		    || (activePiece->type == bPawn
			&& fromRank == 2 && toRank == 1)) {
		    // offer a promotion
		    emit(showMessage
			 ("you are meant to be offered a promotion here"));
		    char promoteTo = wQueen;	// doesnt matter for now
		    moveS.append(promoteTo);
		    moveS.append("\n");
		    crafty->writeToStdin(moveS.latin1());
		}
	    }
	    activePiece = 0;
	    drawBackgroundImage(QPoint(-1, -1));
	}
    }

    else {
	emitErrorMessage();
    }
}

void BoardView::swapSides()
{
    if (activeSide == humanSide && playingGame) {
	humanSide = !humanSide;
	crafty->writeToStdin("savepos\ngo\n");
    } else
	emitErrorMessage();
}

BoardView::BoardView(QCanvas *c, QWidget *w, const char *name)
    : QCanvasView(c, w, name) {
    humanSide = sideWhite;
    activeSide = sideWhite;
    playingGame = TRUE;
    activePiece = 0;
    list.setAutoDelete(TRUE);
    setCanvas(new QCanvas());
    Config c("Chess", Config::User);
    c.setGroup("Theme");
    QString theme = c.readEntry("imagefile", "simple-28");
    setTheme(theme);
    crafty = new CraftyProcess(this);
    crafty->addArgument("crafty");
    if (!crafty->start()) {
	QMessageBox::critical(0,
			      tr("Could not find crafty chess engine"),
			      tr("Quit"));
	exit(-1);
    }

    connect(crafty, SIGNAL(readyReadStdout()), this, SLOT(readStdout()));
    connect(crafty, SIGNAL(processExited()), this, SLOT(craftyDied()));
//    crafty->writeToStdin("xboard\nics\nkibitz=2\n");
    newGame();
}