demos/declarative/minehunt/minehunt.cpp
changeset 37 758a864f9613
parent 30 5dc02b23752f
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include <stdlib.h>
    42 #include <stdlib.h>
    43 #include <qdeclarativeextensionplugin.h>
       
    44 #include <qdeclarativecontext.h>
       
    45 #include <qdeclarativeengine.h>
       
    46 #include <qdeclarative.h>
       
    47 
       
    48 #include <QTime>
    43 #include <QTime>
    49 #include <QTimer>
    44 #include <QTimer>
    50 
    45 
    51 class TileData : public QObject
    46 #include "minehunt.h"
    52 {
       
    53     Q_OBJECT
       
    54 public:
       
    55     TileData() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {}
       
    56 
       
    57     Q_PROPERTY(bool hasFlag READ hasFlag WRITE setHasFlag NOTIFY hasFlagChanged)
       
    58     bool hasFlag() const { return _hasFlag; }
       
    59 
       
    60     Q_PROPERTY(bool hasMine READ hasMine NOTIFY hasMineChanged)
       
    61     bool hasMine() const { return _hasMine; }
       
    62 
       
    63     Q_PROPERTY(int hint READ hint NOTIFY hintChanged)
       
    64     int hint() const { return _hint; }
       
    65 
       
    66     Q_PROPERTY(bool flipped READ flipped NOTIFY flippedChanged())
       
    67     bool flipped() const { return _flipped; }
       
    68 
       
    69     void setHasFlag(bool flag) {if(flag==_hasFlag) return; _hasFlag = flag; emit hasFlagChanged();}
       
    70     void setHasMine(bool mine) {if(mine==_hasMine) return; _hasMine = mine; emit hasMineChanged();}
       
    71     void setHint(int hint) { if(hint == _hint) return; _hint = hint; emit hintChanged(); }
       
    72     void flip() { if (_flipped) return; _flipped = true; emit flippedChanged(); }
       
    73     void unflip() { if(!_flipped) return; _flipped = false; emit flippedChanged(); }
       
    74 
       
    75 signals:
       
    76     void flippedChanged();
       
    77     void hasFlagChanged();
       
    78     void hintChanged();
       
    79     void hasMineChanged();
       
    80 
       
    81 private:
       
    82     bool _hasFlag;
       
    83     bool _hasMine;
       
    84     int _hint;
       
    85     bool _flipped;
       
    86 };
       
    87 
       
    88 class MinehuntGame : public QObject
       
    89 {
       
    90     Q_OBJECT
       
    91 public:
       
    92     MinehuntGame();
       
    93 
       
    94     Q_PROPERTY(QDeclarativeListProperty<TileData> tiles READ tiles CONSTANT)
       
    95     QDeclarativeListProperty<TileData> tiles();
       
    96 
       
    97     Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged)
       
    98     bool isPlaying() {return playing;}
       
    99 
       
   100     Q_PROPERTY(bool hasWon READ hasWon NOTIFY hasWonChanged)
       
   101     bool hasWon() {return won;}
       
   102 
       
   103     Q_PROPERTY(int numMines READ numMines NOTIFY numMinesChanged)
       
   104     int numMines() const{return nMines;}
       
   105 
       
   106     Q_PROPERTY(int numFlags READ numFlags NOTIFY numFlagsChanged)
       
   107     int numFlags() const{return nFlags;}
       
   108 
       
   109 public slots:
       
   110     Q_INVOKABLE bool flip(int row, int col);
       
   111     Q_INVOKABLE bool flag(int row, int col);
       
   112     void setBoard();
       
   113     void reset();
       
   114 
       
   115 signals:
       
   116     void isPlayingChanged();
       
   117     void hasWonChanged();
       
   118     void numMinesChanged();
       
   119     void numFlagsChanged();
       
   120 
       
   121 private:
       
   122     bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; }
       
   123     TileData *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; }
       
   124     int getHint(int row, int col);
       
   125     void setPlaying(bool b){if(b==playing) return; playing=b; emit isPlayingChanged();}
       
   126 
       
   127     QList<TileData *> _tiles;
       
   128     int numCols;
       
   129     int numRows;
       
   130     bool playing;
       
   131     bool won;
       
   132     int remaining;
       
   133     int nMines;
       
   134     int nFlags;
       
   135 };
       
   136 
    47 
   137 void tilesPropAppend(QDeclarativeListProperty<TileData>* prop, TileData* value)
    48 void tilesPropAppend(QDeclarativeListProperty<TileData>* prop, TileData* value)
   138 {
    49 {
   139     Q_UNUSED(prop);
    50     Q_UNUSED(prop);
   140     Q_UNUSED(value);
    51     Q_UNUSED(value);
   304     t->setHasFlag(!t->hasFlag());
   215     t->setHasFlag(!t->hasFlag());
   305     nFlags += (t->hasFlag()?1:-1);
   216     nFlags += (t->hasFlag()?1:-1);
   306     emit numFlagsChanged();
   217     emit numFlagsChanged();
   307     return true;
   218     return true;
   308 }
   219 }
   309 
       
   310 class MinehuntExtensionPlugin : public QDeclarativeExtensionPlugin
       
   311 {
       
   312     Q_OBJECT
       
   313 
       
   314     public:
       
   315     void registerTypes(const char *uri) {
       
   316         Q_UNUSED(uri);
       
   317         qmlRegisterType<TileData>();
       
   318     }
       
   319 
       
   320     void initializeEngine(QDeclarativeEngine *engine, const char *uri) {
       
   321         Q_UNUSED(uri);
       
   322 
       
   323         srand(QTime(0,0,0).secsTo(QTime::currentTime()));
       
   324 
       
   325         MinehuntGame* game = new MinehuntGame();
       
   326 
       
   327         engine->rootContext()->setContextObject(game);
       
   328     }
       
   329 };
       
   330 
       
   331 #include "minehunt.moc"
       
   332 
       
   333 Q_EXPORT_PLUGIN(MinehuntExtensionPlugin);
       
   334