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