Diffstat (limited to 'noncore/games/wordgame/wordgame.h') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/wordgame/wordgame.h | 376 |
1 files changed, 376 insertions, 0 deletions
diff --git a/noncore/games/wordgame/wordgame.h b/noncore/games/wordgame/wordgame.h new file mode 100644 index 0000000..0ffa56a --- a/dev/null +++ b/noncore/games/wordgame/wordgame.h | |||
@@ -0,0 +1,376 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #ifndef WORDGAME_H | ||
21 | #define WORDGAME_H | ||
22 | |||
23 | #include "newgamebase.h" | ||
24 | #include "rulesbase.h" | ||
25 | |||
26 | #include <qpe/qdawg.h> | ||
27 | #include <qpe/applnk.h> | ||
28 | |||
29 | #include <qmainwindow.h> | ||
30 | #include <qcanvas.h> | ||
31 | #include <qlabel.h> | ||
32 | |||
33 | class QVBox; | ||
34 | class QLabel; | ||
35 | class QWidgetStack; | ||
36 | class QToolButton; | ||
37 | class Config; | ||
38 | |||
39 | class Tile { | ||
40 | public: | ||
41 | Tile() {} | ||
42 | |||
43 | Tile(const Tile& t) | ||
44 | { | ||
45 | txt = t.txt; | ||
46 | val = t.val; | ||
47 | blank = t.blank; | ||
48 | } | ||
49 | |||
50 | Tile(QString text, int value) | ||
51 | { | ||
52 | txt = text; | ||
53 | val = value; | ||
54 | blank = txt.isEmpty(); | ||
55 | } | ||
56 | |||
57 | Tile(const QString& key); | ||
58 | |||
59 | int value() const { return val; } | ||
60 | bool isBlank() const { return blank; } | ||
61 | QString text() const { return txt; } | ||
62 | void setText(const QString& t) | ||
63 | { | ||
64 | txt = t; | ||
65 | } | ||
66 | |||
67 | int operator==(const Tile& o) const | ||
68 | { return o.txt == txt && o.val == val && o.blank == blank; } | ||
69 | int operator!=(const Tile& o) const | ||
70 | { return !operator==(o); } | ||
71 | Tile& operator=(const Tile& o) | ||
72 | { txt=o.txt; val=o.val; blank=o.blank; return *this; } | ||
73 | |||
74 | QString key() const; | ||
75 | |||
76 | private: | ||
77 | QString txt; | ||
78 | int val; | ||
79 | bool blank; | ||
80 | }; | ||
81 | |||
82 | class Bag { | ||
83 | public: | ||
84 | Bag(); | ||
85 | |||
86 | void readConfig(Config&); | ||
87 | void writeConfig(Config&); | ||
88 | |||
89 | void add(const Tile&); | ||
90 | bool isEmpty() const { return tiles.isEmpty(); } | ||
91 | Tile takeRandom(); | ||
92 | private: | ||
93 | QList<Tile> tiles; | ||
94 | }; | ||
95 | |||
96 | class TileItem : public QCanvasRectangle { | ||
97 | public: | ||
98 | TileItem(const Tile& tile, bool b, QCanvas* c) : | ||
99 | QCanvasRectangle(0,0, | ||
100 | b?bigWidth():smallWidth(), | ||
101 | b?bigHeight():smallHeight(),c), | ||
102 | t(tile), big(b), s(Firm) | ||
103 | { | ||
104 | } | ||
105 | |||
106 | static int smallWidth(); | ||
107 | static int smallHeight(); | ||
108 | static int bigWidth(); | ||
109 | static int bigHeight(); | ||
110 | |||
111 | enum State { Firm, Floating }; | ||
112 | void setState( State state ); | ||
113 | State state() const { return s; } | ||
114 | const Tile& tile() const { return t; } | ||
115 | void setTile(const Tile&); | ||
116 | void setBig(bool); | ||
117 | |||
118 | protected: | ||
119 | void drawShape(QPainter&); | ||
120 | |||
121 | private: | ||
122 | Tile t; | ||
123 | bool big; | ||
124 | State s; | ||
125 | }; | ||
126 | |||
127 | class Rack : public QCanvasView { | ||
128 | public: | ||
129 | Rack(int ntiles, QWidget* parent); | ||
130 | ~Rack(); | ||
131 | |||
132 | void readConfig(Config&); | ||
133 | void writeConfig(Config&); | ||
134 | |||
135 | bool isFull() const { return count()==max(); } | ||
136 | int max() const { return item.count(); } | ||
137 | int count() const { return n; } | ||
138 | void addTile(const Tile& t); | ||
139 | Tile tile(int i) const { return item[i]->tile(); } | ||
140 | const Tile* tileRef(int i) const { return &item[i]->tile(); } | ||
141 | void remove(int i); | ||
142 | void remove(Tile); | ||
143 | bool arrangeTiles(const Tile** s, int sn); | ||
144 | void setBlanks(const Tile*); | ||
145 | |||
146 | void setPlayerName(const QString& name) { nm = name; } | ||
147 | QString playerName() const { return nm; } | ||
148 | void setComputerization(int level) { cpu=level; } | ||
149 | bool computerized() const { return cpu>0; } | ||
150 | |||
151 | protected: | ||
152 | void resizeEvent(QResizeEvent*e); | ||
153 | void contentsMousePressEvent(QMouseEvent*); | ||
154 | void contentsMouseMoveEvent(QMouseEvent*); | ||
155 | void contentsMouseReleaseEvent(QMouseEvent*); | ||
156 | |||
157 | private: | ||
158 | void clear(); | ||
159 | void layoutTiles(); | ||
160 | int n; | ||
161 | QArray<TileItem*> item; | ||
162 | int dragging_adj; | ||
163 | QPoint dragstart; | ||
164 | QCanvasItem* dragging; | ||
165 | QString nm; | ||
166 | int cpu; | ||
167 | }; | ||
168 | |||
169 | class Board : public QCanvasView { | ||
170 | Q_OBJECT | ||
171 | public: | ||
172 | Board(QPixmap bgshapes, int w, int h, QWidget* parent); | ||
173 | ~Board(); | ||
174 | |||
175 | void readConfig(Config&); | ||
176 | void writeConfig(Config&); | ||
177 | |||
178 | int xTiles() const { return canvas()->tilesHorizontally(); } | ||
179 | int yTiles() const { return canvas()->tilesVertically(); } | ||
180 | |||
181 | bool contains(const QPoint& p) const | ||
182 | { return p.x() >= 0 && p.y() >= 0 | ||
183 | && p.x() < canvas()->tilesHorizontally() | ||
184 | && p.y() < canvas()->tilesVertically(); } | ||
185 | const Tile* tile(const QPoint& p) const | ||
186 | { TileItem* it=item(p); return it ? &it->tile() : 0; } | ||
187 | |||
188 | void setRules(const QString& shapes, const int* effects); | ||
189 | |||
190 | void clear(); | ||
191 | void unsetTile(const QPoint& p); | ||
192 | void setTile(const QPoint& p, const Tile& t); | ||
193 | |||
194 | void setTileState(const QPoint& p, TileItem::State s) | ||
195 | { | ||
196 | TileItem* it=item(p); | ||
197 | if (it) it->setState(s); | ||
198 | } | ||
199 | |||
200 | void setCurrentRack(Rack*); | ||
201 | void resetRack(); | ||
202 | void finalizeTurn(); | ||
203 | void showTurn(); | ||
204 | void scoreTurn(const QPoint& at, int n, const QPoint& d); | ||
205 | bool checkTurn(); | ||
206 | int score(QPoint at, const Tile** tiles, int n, | ||
207 | const Tile* blankvalue, | ||
208 | const QPoint& d, bool ignoredict, QStringList* words) const; | ||
209 | int bonussedValue(const QPoint& at, int base, int& all_mult) const; | ||
210 | bool isStart(const QPoint& at) const; | ||
211 | |||
212 | int turnScore() const { return turn_score; } | ||
213 | |||
214 | signals: | ||
215 | void temporaryScore(int); | ||
216 | |||
217 | protected: | ||
218 | void contentsMousePressEvent(QMouseEvent*); | ||
219 | void contentsMouseMoveEvent(QMouseEvent*); | ||
220 | void contentsMouseReleaseEvent(QMouseEvent*); | ||
221 | |||
222 | private: | ||
223 | int idx(const QPoint& p) const | ||
224 | { return p.x()+p.y()*canvas()->tilesHorizontally(); } | ||
225 | TileItem*& item(const QPoint& p) const | ||
226 | { return grid[idx(p)]; } | ||
227 | TileItem **grid; | ||
228 | QString rule_shape; | ||
229 | const int* rule_effect; | ||
230 | int rack_tiles_bonus; | ||
231 | Rack* current_rack; | ||
232 | QPoint boardPos(const QPoint&) const; | ||
233 | QPoint dragstart; | ||
234 | QPoint shown_at; | ||
235 | int shown_n; | ||
236 | QPoint shown_step; | ||
237 | void unshowTurn(); | ||
238 | int turn_score; | ||
239 | }; | ||
240 | |||
241 | class ComputerPlayer | ||
242 | { | ||
243 | Board* board; | ||
244 | Rack* rack; | ||
245 | |||
246 | bool across; | ||
247 | int dict; | ||
248 | QPoint current; | ||
249 | |||
250 | const Tile** best; | ||
251 | int best_n; | ||
252 | Tile* best_blankvalues; | ||
253 | int best_blused; | ||
254 | int best_score; | ||
255 | QPoint best_dir; | ||
256 | QPoint best_start; | ||
257 | |||
258 | public: | ||
259 | ComputerPlayer(Board* b, Rack* r); | ||
260 | ~ComputerPlayer(); | ||
261 | |||
262 | bool step(); | ||
263 | |||
264 | private: | ||
265 | void findBest(QPoint at, const QPoint& d, const QDawg::Node* node, ulong used, uchar *nletter, const Tile** tiles, int n, Tile* blankvalues, int blused); | ||
266 | void noteChoice(const Tile** tiles, int n, const QPoint& d, const Tile* blankvalues, int blused); | ||
267 | }; | ||
268 | |||
269 | class ScoreInfo : public QLabel { | ||
270 | Q_OBJECT | ||
271 | public: | ||
272 | ScoreInfo( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | ||
273 | ~ScoreInfo(); | ||
274 | |||
275 | void init(const QStringList&); | ||
276 | void addScore(int player, int change); | ||
277 | int playerScore(int player) const { return score[player]; } | ||
278 | void setShowWinner(bool); | ||
279 | void setBoldOne(int); | ||
280 | |||
281 | void readConfig(Config&); | ||
282 | void writeConfig(Config&); | ||
283 | |||
284 | protected: | ||
285 | QSize sizeHint() const; | ||
286 | |||
287 | public slots: | ||
288 | void showTemporaryScore(int amount); | ||
289 | |||
290 | private slots: | ||
291 | void showScores(); | ||
292 | |||
293 | private: | ||
294 | QStringList names; | ||
295 | int *score; | ||
296 | QTimer* msgtimer; | ||
297 | bool showwinner; | ||
298 | int boldone; | ||
299 | }; | ||
300 | |||
301 | class NewGame; | ||
302 | |||
303 | class WordGame : public QMainWindow { | ||
304 | Q_OBJECT | ||
305 | public: | ||
306 | WordGame( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | ||
307 | ~WordGame(); | ||
308 | |||
309 | private slots: | ||
310 | void endTurn(); | ||
311 | void resetTurn(); | ||
312 | void passTurn(); | ||
313 | void think(); | ||
314 | void endGame(); | ||
315 | void startGame(); | ||
316 | |||
317 | private: | ||
318 | void writeConfig(); | ||
319 | void readConfig(); | ||
320 | |||
321 | void startGame(const QStringList& pnames); | ||
322 | bool mayEndGame(); | ||
323 | void openGameSelector(const QStringList& initnames); | ||
324 | bool loadRules(const QString& filename); | ||
325 | void addPlayer(const QString& name); | ||
326 | void addPlayer(const QString& name, int cpu); | ||
327 | void nextPlayer(); | ||
328 | bool refillRack(int i); | ||
329 | void readyRack(int i); | ||
330 | Rack* rack(int i) const; | ||
331 | |||
332 | QWidgetStack *racks; | ||
333 | QToolBar* toolbar; | ||
334 | QVBox *vbox; | ||
335 | Board *board; | ||
336 | Bag *bag; | ||
337 | ScoreInfo *scoreinfo; | ||
338 | QToolButton *done; | ||
339 | QToolButton *reset; | ||
340 | QTimer* aiheart; | ||
341 | ComputerPlayer *cpu; | ||
342 | int player; | ||
343 | int nplayers; | ||
344 | QStringList namelist; | ||
345 | bool gameover; | ||
346 | QString rules; | ||
347 | NewGame* newgame; | ||
348 | }; | ||
349 | |||
350 | class NewGame : public NewGameBase { | ||
351 | Q_OBJECT | ||
352 | public: | ||
353 | NewGame(QWidget* parent); | ||
354 | QStringList ruleslist; | ||
355 | |||
356 | public slots: | ||
357 | void updateRuleSets(); | ||
358 | }; | ||
359 | |||
360 | class Rules : public RulesBase { | ||
361 | Q_OBJECT | ||
362 | |||
363 | public: | ||
364 | Rules(QWidget* parent); | ||
365 | |||
366 | signals: | ||
367 | void rulesChanged(); | ||
368 | |||
369 | public slots: | ||
370 | void editRules(); | ||
371 | |||
372 | private: | ||
373 | void deleteRuleSet(); | ||
374 | }; | ||
375 | |||
376 | #endif // WORDGAME_H | ||