author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/chess/chess.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
-rw-r--r-- | noncore/games/chess/chess.cpp | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/noncore/games/chess/chess.cpp b/noncore/games/chess/chess.cpp new file mode 100644 index 0000000..96a838a --- a/dev/null +++ b/noncore/games/chess/chess.cpp | |||
@@ -0,0 +1,358 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | |||
22 | // human is not offered a promotion yet | ||
23 | |||
24 | #include <qcanvas.h> | ||
25 | #include <qmainwindow.h> | ||
26 | #include <qlist.h> | ||
27 | #include <qimage.h> | ||
28 | #include <qpainter.h> | ||
29 | #include <qmessagebox.h> | ||
30 | #include <qregexp.h> | ||
31 | |||
32 | #include <qpe/config.h> | ||
33 | #include <qpe/resource.h> | ||
34 | |||
35 | #include "chess.h" | ||
36 | |||
37 | #define CHESS_DEBUG | ||
38 | |||
39 | int pieceSize = 40; | ||
40 | static QVector < QImage > imgList; | ||
41 | int timeMoves, timeTime; | ||
42 | |||
43 | int BoardView::convertToRank(int r) | ||
44 | { | ||
45 | r = r / pieceSize; | ||
46 | if (humanSide == sideWhite) | ||
47 | r = 8 - r; | ||
48 | else | ||
49 | r++; | ||
50 | return r; | ||
51 | } | ||
52 | |||
53 | char BoardView::convertToFile(int f) | ||
54 | { | ||
55 | f = f / pieceSize; | ||
56 | if (humanSide == sideWhite) | ||
57 | return f + 'a'; | ||
58 | else | ||
59 | return 'h' - f; | ||
60 | } | ||
61 | |||
62 | int BoardView::convertFromFile(char f) | ||
63 | { | ||
64 | if (humanSide == sideWhite) | ||
65 | f = f - 'a'; | ||
66 | else | ||
67 | f = 'h' - f; | ||
68 | return f * pieceSize; | ||
69 | } | ||
70 | |||
71 | int BoardView::convertFromRank(int r) | ||
72 | { | ||
73 | if (humanSide == sideWhite) | ||
74 | r = 8 - r; | ||
75 | else | ||
76 | r--; | ||
77 | return r * pieceSize; | ||
78 | } | ||
79 | |||
80 | // Pieces | ||
81 | Piece::Piece(QCanvas * canvas, int t):QCanvasRectangle(canvas) | ||
82 | { | ||
83 | type = t; | ||
84 | setSize(pieceSize, pieceSize); | ||
85 | show(); | ||
86 | } | ||
87 | |||
88 | Piece *BoardView::newPiece(int t, char f, int r) | ||
89 | { | ||
90 | Piece *tmpPiece = new Piece(canvas(), t); | ||
91 | tmpPiece->move(convertFromFile(f), convertFromRank(r)); | ||
92 | list.append(tmpPiece); | ||
93 | return tmpPiece; | ||
94 | } | ||
95 | |||
96 | void BoardView::deletePiece(Piece * p) | ||
97 | { | ||
98 | list.remove(p); | ||
99 | canvas()->update(); | ||
100 | } | ||
101 | |||
102 | void Piece::drawShape(QPainter & p) | ||
103 | { | ||
104 | p.drawImage(int (x()), int (y()), *(imgList[type])); | ||
105 | } | ||
106 | |||
107 | void BoardView::buildImages(QImage theme) | ||
108 | { | ||
109 | imgList.resize(12); | ||
110 | int x; | ||
111 | int y = 0; | ||
112 | |||
113 | for (int j = 0; j < 2; j++) { | ||
114 | x = 0; | ||
115 | for (int i = 0; i < 6; i++) { | ||
116 | imgList.insert(i + (j * 6), | ||
117 | new QImage(theme. | ||
118 | copy(x, y, pieceSize, pieceSize))); | ||
119 | x += pieceSize; | ||
120 | } | ||
121 | y += pieceSize; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | void BoardView::readStdout() | ||
126 | { | ||
127 | QString input( crafty->readStdout() ); | ||
128 | #ifdef CHESS_DEBUG | ||
129 | qDebug("received this string from crafty->\n%s\n", input.latin1()); | ||
130 | #endif | ||
131 | |||
132 | int startPosition = input.find("setboard"); | ||
133 | if (startPosition != -1) | ||
134 | decodePosition(input.remove(0, startPosition + 9)); | ||
135 | |||
136 | if (input.contains("Black mates")) { | ||
137 | playingGame = FALSE; | ||
138 | emit(showMessage("Black mates")); | ||
139 | } else if (input.contains("White mates")) { | ||
140 | playingGame = FALSE; | ||
141 | emit(showMessage("White mates")); | ||
142 | } else if (input.contains(" resigns")) { | ||
143 | playingGame = FALSE; | ||
144 | emit(showMessage("Computer resigns")); | ||
145 | } else if (input.contains("Draw")) { | ||
146 | playingGame = FALSE; | ||
147 | emit(showMessage("Draw")); | ||
148 | } | ||
149 | } | ||
150 | |||
151 | // this is pretty close to getting done right | ||
152 | // maybe dont use sprites and just draw a picture | ||
153 | // there'll be lots of drawing done anyway | ||
154 | // eg creating pictures for the webpages, | ||
155 | // and presenting options for promotions | ||
156 | void BoardView::decodePosition(const QString & t) | ||
157 | { | ||
158 | qDebug("decode copped %s \n", t.latin1()); | ||
159 | |||
160 | int count = 0; | ||
161 | int stringPos = 0; | ||
162 | for (int file = 0; file < 8; file++) { | ||
163 | for (int rank = 0; rank < 8; rank++) { | ||
164 | if (count) | ||
165 | count--; | ||
166 | else { | ||
167 | if (t.at(stringPos).isNumber()) | ||
168 | count = t.at(stringPos).digitValue(); | ||
169 | else { | ||
170 | newPiece(t.at(stringPos).latin1(), 'a' + file, | ||
171 | rank + 1); | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | } | ||
177 | |||
178 | void BoardView::undo() | ||
179 | { | ||
180 | crafty->writeToStdin("undo\n"); | ||
181 | crafty->writeToStdin("savepos\nclock\n"); | ||
182 | } | ||
183 | |||
184 | void BoardView::emitErrorMessage() | ||
185 | { | ||
186 | if (activeSide != humanSide) | ||
187 | emit(showMessage("Not your move")); | ||
188 | else | ||
189 | emit(showMessage("You are not playing a game")); | ||
190 | } | ||
191 | |||
192 | void BoardView::annotateGame() | ||
193 | { | ||
194 | crafty-> | ||
195 | writeToStdin | ||
196 | ("savegame game.save\nannotateh game.save bw 0 1.0 1\n"); | ||
197 | emit(showMessage("Annotating game")); | ||
198 | } | ||
199 | |||
200 | Piece *BoardView::findPiece(char f, int r) | ||
201 | { | ||
202 | QListIterator < Piece > it(list); | ||
203 | Piece *tmpPiece; | ||
204 | for (; it.current(); ++it) { | ||
205 | tmpPiece = it.current(); | ||
206 | if (convertToRank(tmpPiece->x()) == r | ||
207 | && convertToFile(tmpPiece->y()) == f) | ||
208 | return tmpPiece; | ||
209 | } | ||
210 | return 0; | ||
211 | } | ||
212 | |||
213 | void BoardView::newGame() | ||
214 | { | ||
215 | activeSide = sideWhite; | ||
216 | emit(showMessage("New game")); | ||
217 | crafty->writeToStdin("new\n"); | ||
218 | crafty->writeToStdin("savepos\n"); | ||
219 | crafty->writeToStdin("time " + | ||
220 | QString::number(timeMoves) + | ||
221 | "/" + QString::number(timeTime) + "\n"); | ||
222 | activeSide = sideWhite; | ||
223 | if (humanSide == sideBlack) | ||
224 | crafty->writeToStdin("go\n"); | ||
225 | } | ||
226 | |||
227 | void BoardView::setTheme(QString filename) | ||
228 | { | ||
229 | QImage theme = Resource::loadImage(QString("chess/") + filename); | ||
230 | pieceSize = theme.height() / 2; | ||
231 | setFrameStyle(QFrame::Plain); | ||
232 | setFixedSize(8 * pieceSize, 8 * pieceSize); | ||
233 | canvas()->setBackgroundColor(Qt::red); | ||
234 | canvas()->resize(8 * pieceSize, 8 * pieceSize); | ||
235 | whiteSquare = theme.copy(6 * pieceSize, 0, pieceSize, pieceSize); | ||
236 | activeWhiteSquare = theme.copy(7 * pieceSize, 0, pieceSize, pieceSize); | ||
237 | blackSquare = | ||
238 | theme.copy(6 * pieceSize, pieceSize, pieceSize, pieceSize); | ||
239 | activeBlackSquare = | ||
240 | theme.copy(7 * pieceSize, pieceSize, pieceSize, pieceSize); | ||
241 | buildImages(theme); | ||
242 | drawBackgroundImage(QPoint(-1, -1)); | ||
243 | } | ||
244 | |||
245 | |||
246 | // sets the bg to the default background image for the current theme | ||
247 | // also resposible for drawing the "active" marker | ||
248 | void BoardView::drawBackgroundImage(QPoint activeSquare) | ||
249 | { | ||
250 | bg = QPixmap(8 * pieceSize, 8 * pieceSize); | ||
251 | QPainter p(&bg); | ||
252 | bool col = FALSE; | ||
253 | for (int i = 0; i < 8; i++) { | ||
254 | for (int j = 0; j < 8; j++) { | ||
255 | QPoint point(i * pieceSize, j * pieceSize); | ||
256 | if (col) { | ||
257 | if (point.x() == activeSquare.x() | ||
258 | && point.y() == activeSquare.y()) | ||
259 | p.drawImage(point, activeBlackSquare); | ||
260 | else | ||
261 | p.drawImage(point, blackSquare); | ||
262 | col = FALSE; | ||
263 | } else { | ||
264 | if (point.x() == activeSquare.x() | ||
265 | && point.y() == activeSquare.y()) | ||
266 | p.drawImage(point, activeWhiteSquare); | ||
267 | else | ||
268 | p.drawImage(point, whiteSquare); | ||
269 | col = TRUE; | ||
270 | } | ||
271 | } | ||
272 | col = !col; | ||
273 | } | ||
274 | canvas()->setBackgroundPixmap(bg); | ||
275 | canvas()->update(); | ||
276 | } | ||
277 | |||
278 | |||
279 | // Board view widget | ||
280 | void BoardView::contentsMousePressEvent(QMouseEvent * e) | ||
281 | { | ||
282 | QCanvasItemList cList = canvas()->collisions(e->pos()); | ||
283 | if (activeSide == humanSide && playingGame) { | ||
284 | if (!activePiece) { | ||
285 | if (cList.count()) { | ||
286 | activePiece = (Piece *) (*(cList.at(0))); | ||
287 | drawBackgroundImage(QPoint | ||
288 | (activePiece->x(), activePiece->y())); | ||
289 | } | ||
290 | } else { | ||
291 | if (!(activePiece == (Piece *) (*(cList.at(0))))) { | ||
292 | char fromFile = convertToFile(activePiece->x()); | ||
293 | int fromRank = convertToRank(activePiece->y()); | ||
294 | char toFile = convertToFile(e->pos().x()); | ||
295 | int toRank = convertToRank(e->pos().y()); | ||
296 | QString moveS; | ||
297 | moveS.append(fromFile); | ||
298 | moveS.append(moveS.number(fromRank)); | ||
299 | moveS.append(toFile); | ||
300 | moveS.append(moveS.number(toRank)); | ||
301 | if ((activePiece->type == wPawn | ||
302 | && fromRank == 7 && toRank == 8) | ||
303 | || (activePiece->type == bPawn | ||
304 | && fromRank == 2 && toRank == 1)) { | ||
305 | // offer a promotion | ||
306 | emit(showMessage | ||
307 | ("you are meant to be offered a promotion here")); | ||
308 | char promoteTo = wQueen;// doesnt matter for now | ||
309 | moveS.append(promoteTo); | ||
310 | moveS.append("\n"); | ||
311 | crafty->writeToStdin(moveS.latin1()); | ||
312 | } | ||
313 | } | ||
314 | activePiece = 0; | ||
315 | drawBackgroundImage(QPoint(-1, -1)); | ||
316 | } | ||
317 | } | ||
318 | |||
319 | else { | ||
320 | emitErrorMessage(); | ||
321 | } | ||
322 | } | ||
323 | |||
324 | void BoardView::swapSides() | ||
325 | { | ||
326 | if (activeSide == humanSide && playingGame) { | ||
327 | humanSide = !humanSide; | ||
328 | crafty->writeToStdin("savepos\ngo\n"); | ||
329 | } else | ||
330 | emitErrorMessage(); | ||
331 | } | ||
332 | |||
333 | BoardView::BoardView(QCanvas *c, QWidget *w, const char *name) | ||
334 | : QCanvasView(c, w, name) { | ||
335 | humanSide = sideWhite; | ||
336 | activeSide = sideWhite; | ||
337 | playingGame = TRUE; | ||
338 | activePiece = 0; | ||
339 | list.setAutoDelete(TRUE); | ||
340 | setCanvas(new QCanvas()); | ||
341 | Config c("Chess", Config::User); | ||
342 | c.setGroup("Theme"); | ||
343 | QString theme = c.readEntry("imagefile", "simple-28"); | ||
344 | setTheme(theme); | ||
345 | crafty = new CraftyProcess(this); | ||
346 | crafty->addArgument("crafty"); | ||
347 | if (!crafty->start()) { | ||
348 | QMessageBox::critical(0, | ||
349 | tr("Could not find crafty chess engine"), | ||
350 | tr("Quit")); | ||
351 | exit(-1); | ||
352 | } | ||
353 | |||
354 | connect(crafty, SIGNAL(readyReadStdout()), this, SLOT(readStdout())); | ||
355 | connect(crafty, SIGNAL(processExited()), this, SLOT(craftyDied())); | ||
356 | // crafty->writeToStdin("xboard\nics\nkibitz=2\n"); | ||
357 | newGame(); | ||
358 | } | ||