/**************************************************************************** Copyright (C) 2002-2006 Gilles Debunne (Gilles.Debunne@imag.fr) This file is part of the QGLViewer library. Version 2.2.4-1, released on December 12, 2006. http://artis.imag.fr/Members/Gilles.Debunne/QGLViewer libQGLViewer is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. libQGLViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with libQGLViewer; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ #include "board.h" #include bool Board::canBeSelected(int i) { QPoint p = pointFromInt(i); return (isValid(p) && (stateOf(p) == Board::blueColor(bluePlays())) && (pieceCanMove(p))); } bool Board::isValid(const QPoint& p) const { return ((p.x() >= 0) && (p.y() >= 0) && (p.x() < sizeX_) && (p.y() < sizeY_) && (stateOf(p) != HOLE)); } void Board::play(const Move& m) { if (!m.isValid(this)) { QMessageBox::critical(NULL, "Invalid move", "Invalid move"); return; } if (!m.isClose()) setStateOf(m.start(), Board::EMPTY); setStateOf(m.end(), Board::blueColor(bluePlays())); for (int i=-1; i<=1; ++i) for (int j=-1; j<=1; ++j) { const QPoint p(m.end().x()+i, m.end().y()+j); if (isValid(p) && stateOf(p) == Board::blueColor(!bluePlays())) setStateOf(p, Board::blueColor(bluePlays())); } undo_.addState(stateString()); bluePlays_ = !bluePlays_; checkPlayerCanPlay(); } void Board::checkPlayerCanPlay() { for (int i=0; i Board::possibleMoves(bool bluePlays) const { QValueVector res; #else QVector Board::possibleMoves(bool bluePlays) const { QVector res; #endif for (int i=0; i>(std::istream& in, Board& b) { int sx, sy; in >> sx >> sy; b.resize(sx, sy); char c; QString s; for (int i=0; i> c; s.append(c); } b.initFromStateString(s); in >> b.undo_; if (b.undo_.isEmpty()) b.undo_.addState(s); b.bluePlays_ = (b.undo_.nbMoves() % 2 == 1); return in; } void Board::resize(int sizeX, int sizeY) { if (board_) { for (int i=0; i moves = possibleMoves(bluePlays()); #else QVector moves = possibleMoves(bluePlays()); #endif for (int i=0; i bestValue) { bestValue = value; res = moves[i]; } } return res; } QString Board::statusMessage() const { const QString score = "Blue : " + QString::number(nbBlue_) + " - Red : " + QString::number(nbRed_); if (gameIsOver()) if (nbBlue_ == nbRed_) return "Deuce !\n" + score; else return QString("The ") + ((nbBlue_>nbRed_)?"Blue":"Red") + " won !\n" + score; else return score + " - " + (bluePlays_?"Blue":"Red") + " plays"; }