Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

wordgame.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #ifndef WORDGAME_H
00021 #define WORDGAME_H
00022 
00023 #include "newgamebase.h"
00024 #include "rulesbase.h"
00025 
00026 #include <qpe/qdawg.h>
00027 #include <qpe/applnk.h>
00028 
00029 #include <qmainwindow.h>
00030 #include <qcanvas.h>
00031 #include <qlabel.h>
00032 
00033 class QVBox;
00034 class QLabel;
00035 class QWidgetStack;
00036 class QToolButton;
00037 class Config;
00038 
00039 class Tile {
00040 public:
00041     Tile() {}
00042 
00043     Tile(const Tile& t)
00044     {
00045         txt = t.txt;
00046         val = t.val;
00047         blank = t.blank;
00048     }
00049 
00050     Tile(QString text, int value)
00051     {
00052         txt = text;
00053         val = value;
00054         blank = txt.isEmpty();
00055     }
00056 
00057     Tile(const QString& key);
00058 
00059     int value() const { return val; }
00060     bool isBlank() const { return blank; }
00061     QString text() const { return txt; }
00062     void setText(const QString& t)
00063     {
00064         txt = t;
00065     }
00066 
00067     int operator==(const Tile& o) const
00068         { return o.txt == txt && o.val == val && o.blank == blank; }
00069     int operator!=(const Tile& o) const
00070         { return !operator==(o); }
00071     Tile& operator=(const Tile& o)
00072         { txt=o.txt; val=o.val; blank=o.blank; return *this; }
00073 
00074     QString key() const;
00075 
00076 private:
00077     QString txt;
00078     int val;
00079     bool blank;
00080 };
00081 
00082 class Bag {
00083 public:
00084     Bag();
00085 
00086     void readConfig(Config&);
00087     void writeConfig(Config&);
00088 
00089     void add(const Tile&);
00090     bool isEmpty() const { return tiles.isEmpty(); }
00091     Tile takeRandom();
00092 private:
00093     QList<Tile> tiles;
00094 };
00095 
00096 class TileItem : public QCanvasRectangle {
00097 public:
00098     TileItem(const Tile& tile, bool b, QCanvas* c) :
00099         QCanvasRectangle(0,0,
00100             b?bigWidth():smallWidth(),
00101             b?bigHeight():smallHeight(),c),
00102         t(tile), big(b), s(Firm)
00103     {
00104     }
00105 
00106     static int smallWidth();
00107     static int smallHeight();
00108     static int bigWidth();
00109     static int bigHeight();
00110 
00111     enum State { Firm, Floating };
00112     void setState( State state );
00113     State state() const { return s; }
00114     const Tile& tile() const { return t; }
00115     void setTile(const Tile&);
00116     void setBig(bool);
00117 
00118 protected:
00119     void drawShape(QPainter&);
00120 
00121 private:
00122     Tile t;
00123     bool big;
00124     State s;
00125 };
00126 
00127 class Rack : public QCanvasView {
00128 public:
00129     Rack(int ntiles, QWidget* parent);
00130     ~Rack();
00131 
00132     void readConfig(Config&);
00133     void writeConfig(Config&);
00134 
00135     bool isFull() const { return count()==max(); }
00136     int max() const { return item.count(); }
00137     int count() const { return n; }
00138     void addTile(const Tile& t);
00139     Tile tile(int i) const { return item[i]->tile(); }
00140     const Tile* tileRef(int i) const { return &item[i]->tile(); }
00141     void remove(int i);
00142     void remove(Tile);
00143     bool arrangeTiles(const Tile** s, int sn);
00144     void setBlanks(const Tile*);
00145 
00146     void setPlayerName(const QString& name) { nm = name; }
00147     QString playerName() const { return nm; }
00148     void setComputerization(int level) { cpu=level; }
00149     bool computerized() const { return cpu>0; }
00150 
00151     QSize sizeHint() const;
00152 
00153 protected:
00154     void resizeEvent(QResizeEvent*e);
00155     void contentsMousePressEvent(QMouseEvent*);
00156     void contentsMouseMoveEvent(QMouseEvent*);
00157     void contentsMouseReleaseEvent(QMouseEvent*);
00158 
00159 private:
00160     void clear();
00161     void layoutTiles();
00162     int n;
00163     QArray<TileItem*> item;
00164     int dragging_adj;
00165     QPoint dragstart;
00166     QCanvasItem* dragging;
00167     QString nm;
00168     int cpu;
00169 };
00170 
00171 class Board : public QCanvasView {
00172     Q_OBJECT
00173 public:
00174     Board(QPixmap bgshapes, int w, int h, QWidget* parent);
00175     ~Board();
00176 
00177     void readConfig(Config&);
00178     void writeConfig(Config&);
00179 
00180     int xTiles() const { return canvas()->tilesHorizontally(); }
00181     int yTiles() const { return canvas()->tilesVertically(); }
00182 
00183     bool contains(const QPoint& p) const
00184         { return p.x() >= 0 && p.y() >= 0
00185               && p.x() < canvas()->tilesHorizontally()
00186               && p.y() < canvas()->tilesVertically(); }
00187     const Tile* tile(const QPoint& p) const
00188         { TileItem* it=item(p); return it ? &it->tile() : 0; }
00189 
00190     void setRules(const QString& shapes, const int* effects);
00191 
00192     void clear();
00193     void unsetTile(const QPoint& p);
00194     void setTile(const QPoint& p, const Tile& t);
00195 
00196     void setTileState(const QPoint& p, TileItem::State s)
00197     {
00198         TileItem* it=item(p);
00199         if (it) it->setState(s);
00200     }
00201 
00202     void setCurrentRack(Rack*);
00203     void resetRack();
00204     void finalizeTurn();
00205     void showTurn();
00206     void scoreTurn(const QPoint& at, int n, const QPoint& d);
00207     bool checkTurn();
00208     int score(QPoint at, const Tile** tiles, int n,
00209         const Tile* blankvalue,
00210         const QPoint& d, bool ignoredict, QStringList* words) const;
00211     int bonussedValue(const QPoint& at, int base, int& all_mult) const;
00212     bool isStart(const QPoint& at) const;
00213 
00214     int turnScore() const { return turn_score; }
00215 
00216     QSize sizeHint() const;
00217 
00218 signals:
00219     void temporaryScore(int);
00220 
00221 protected:
00222     void contentsMousePressEvent(QMouseEvent*);
00223     void contentsMouseMoveEvent(QMouseEvent*);
00224     void contentsMouseReleaseEvent(QMouseEvent*);
00225 
00226 private:
00227     int idx(const QPoint& p) const
00228         { return p.x()+p.y()*canvas()->tilesHorizontally(); }
00229     TileItem*& item(const QPoint& p) const
00230         { return grid[idx(p)]; }
00231     TileItem **grid;
00232     QString rule_shape;
00233     const int* rule_effect;
00234     int rack_tiles_bonus;
00235     Rack* current_rack;
00236     QPoint boardPos(const QPoint&) const;
00237     QPoint dragstart;
00238     QPoint shown_at;
00239     int shown_n;
00240     QPoint shown_step;
00241     void unshowTurn();
00242     int turn_score;
00243 };
00244 
00245 class ComputerPlayer
00246 {
00247     Board* board;
00248     Rack* rack;
00249 
00250     bool across;
00251     int dict;
00252     QPoint current;
00253 
00254     const Tile** best;
00255     int best_n;
00256     Tile* best_blankvalues;
00257     int best_blused;
00258     int best_score;
00259     QPoint best_dir;
00260     QPoint best_start;
00261 
00262 public:
00263     ComputerPlayer(Board* b, Rack* r);
00264     ~ComputerPlayer();
00265 
00266     bool step();
00267 
00268 private:
00269     void findBest(QPoint at, const QPoint& d, const QDawg::Node* node, ulong used, uchar *nletter, const Tile** tiles, int n, Tile* blankvalues, int blused);
00270     void noteChoice(const Tile** tiles, int n, const QPoint& d, const Tile* blankvalues, int blused);
00271 };
00272 
00273 class ScoreInfo : public QLabel {
00274     Q_OBJECT
00275 public:
00276     ScoreInfo( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
00277     ~ScoreInfo();
00278 
00279     void init(const QStringList&);
00280     void addScore(int player, int change);
00281     int playerScore(int player) const { return score[player]; }
00282     void setShowWinner(bool);
00283     void setBoldOne(int);
00284 
00285     void readConfig(Config&);
00286     void writeConfig(Config&);
00287 
00288 protected:
00289     QSize sizeHint() const;
00290 
00291 public slots:
00292     void showTemporaryScore(int amount);
00293 
00294 private slots:
00295     void showScores();
00296 
00297 private:
00298     QStringList names;
00299     int *score;
00300     QTimer* msgtimer;
00301     bool showwinner;
00302     int boldone;
00303 };
00304 
00305 class NewGame;
00306 
00307 class WordGame : public QMainWindow {
00308     Q_OBJECT
00309 public:
00310     WordGame( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
00311     ~WordGame();
00312     static QString appName() { return QString::fromLatin1("wordgame"); }
00313 
00314 private slots:
00315     void endTurn();
00316     void resetTurn();
00317     void passTurn();
00318     void think();
00319     void endGame();
00320     void startGame();
00321 
00322 private:
00323     void writeConfig();
00324     void readConfig();
00325 
00326     void startGame(const QStringList& pnames);
00327     bool mayEndGame();
00328     void openGameSelector(const QStringList& initnames);
00329     bool loadRules(const QString& filename);
00330     void addPlayer(const QString& name);
00331     void addPlayer(const QString& name, int cpu);
00332     void nextPlayer();
00333     bool refillRack(int i);
00334     void readyRack(int i);
00335     Rack* rack(int i) const;
00336 
00337     QWidgetStack *racks;
00338     QToolBar* toolbar;
00339     QWidget *vbox;
00340     Board *board;
00341     Bag *bag;
00342     ScoreInfo *scoreinfo;
00343     QToolButton *done;
00344     QToolButton *reset;
00345     QTimer* aiheart;
00346     ComputerPlayer *cpu;
00347     int player;
00348     int nplayers;
00349     QStringList namelist;
00350     bool gameover;
00351     QString rules;
00352     NewGame* newgame;
00353 };
00354 
00355 class NewGame : public NewGameBase {
00356     Q_OBJECT
00357 public:
00358     NewGame(QWidget* parent);
00359     QStringList ruleslist;
00360 
00361 public slots:
00362     void updateRuleSets();
00363 };
00364 
00365 class Rules : public RulesBase {
00366     Q_OBJECT
00367 
00368 public:
00369     Rules(QWidget* parent);
00370 
00371 signals:
00372     void rulesChanged();
00373 
00374 public slots:
00375     void editRules();
00376 
00377 private:
00378     void deleteRuleSet();
00379 };
00380 
00381 #endif // WORDGAME_H

Generated on Sat Nov 5 16:17:28 2005 for OPIE by  doxygen 1.4.2