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
|
/*
* gamemodel.h - Battleship game plugin
* Copyright (C) 2014 Aleksey Andreev
*
* This program 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.
*
* This program 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef GAMEMODEL_H
#define GAMEMODEL_H
#include <QObject>
#include <QStringList>
#include <QRect>
class GameShip : public QObject
{
Q_OBJECT
public:
enum ShipType { ShipDirUnknown, ShipHorizontal, ShipVertical };
GameShip(int len, const QString &digest = QString(), QObject *parent = 0);
int length() const { return length_; }
int position() const { return firstPos_; }
ShipType direction() const { return direction_; }
QString digest() const { return digest_; }
bool isDestroyed() const { return destroyed_ ; }
void setDirection(ShipType dir);
void setPosition(int pos);
void setDigest(const QString &digest);
void setDestroyed(bool destr);
int nextPosition(int prev);
private:
int length_;
ShipType direction_;
int firstPos_;
bool destroyed_;
QString digest_;
};
class GameBoard : public QObject
{
Q_OBJECT
public:
enum CellStatus
{
CellFree, CellOccupied,
CellUnknown, CellMiss, CellHit, CellMargin
};
struct GameCell
{
CellStatus status;
int ship;
QString digest;
QString seed;
GameCell(CellStatus s) : status(s), ship(-1) {}
};
GameBoard(QObject *parent = 0);
void init(CellStatus s, bool genseed);
void makeShipRandomPosition();
void calculateCellsHash();
bool updateCellDigest(int pos, const QString &digest);
bool updateCell(int pos, CellStatus cs, const QString &seed);
bool updateShipDigest(int length, const QString &digest);
void shot(int pos);
GameShip::ShipType shipDirection(int pos);
int findAndInitShip(int pos);
QRect shipRect(int snum, bool margin) const;
void setShipDestroy(int n, bool margin);
const GameCell &cell(int pos) const;
QStringList toStringList(bool covered) const;
bool isAllDestroyed() const;
private:
static QString genSeed(int len);
GameShip *findShip(int length, const QString &digest);
bool isShipPositionLegal(int shipNum);
void fillShipMargin(int n);
private:
QList<GameCell> cells_;
QList<GameShip *> ships_;
signals:
void shipDestroyed(int snum);
};
class GameModel : public QObject
{
Q_OBJECT
public:
enum GameStatus
{
StatusNone, StatusError, StatusBoardInit,
StatusMyTurn, StatusWaitingTurnAccept, StatusWaitingOpponent,
StatusWin, StatusLose, StatusDraw
};
GameModel(QObject *parent = 0);
void init();
GameStatus status() const { return status_; }
void setStatus(GameStatus s);
void setError();
bool initOpponentBoard(const QStringList &data);
bool uncoverOpponentBoard(const QStringList &data);
const GameBoard &myBoard() const { return myBoard_; }
const GameBoard &oppBoard() const { return opBoard_; }
void setOpponentDraw(bool draw);
bool isOpponentDraw() const { return oppDraw_; }
void setOpponentAcceptedDraw(bool accept);
void opponentResign();
void opponentTurn(int pos);
bool handleResult();
bool handleTurnResult(const QString &res, const QString &seed);
QString lastShotResult() const;
QString lastShotSeed() const;
QStringList getUncoveredBoard() const;
private:
GameStatus status_;
GameBoard myBoard_;
GameBoard opBoard_;
int lastShot_;
bool draw_;
bool oppDraw_;
bool myAccept_;
bool oppResign_;
bool myResign_;
bool destroyed_;
private slots:
void myShipDestroyed();
public slots:
void sendCoveredBoard();
void localTurn(int pos);
void setLocalDraw(bool draw);
void localAccept();
void localResign();
signals:
void acceptDraw();
void statusChanged();
void myBoardUpdated(int x, int y, int width, int height);
void oppBoardUpdated(int x, int y, int width, int height);
void gameEvent(QString data);
};
#endif // GAMEMODEL_H
|