-rw-r--r-- | noncore/games/chess/chess.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/noncore/games/chess/chess.cpp b/noncore/games/chess/chess.cpp index 96a838a..29c96bb 100644 --- a/noncore/games/chess/chess.cpp +++ b/noncore/games/chess/chess.cpp @@ -1,119 +1,121 @@ /********************************************************************** ** 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 <stdlib.h> + #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; @@ -245,114 +247,114 @@ void BoardView::setTheme(QString filename) // 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"); + Config conf("Chess", Config::User); + conf.setGroup("Theme"); + QString theme = conf.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(); } |