summaryrefslogtreecommitdiff
path: root/noncore/games/wordgame/wordgame.h
blob: 020a4b5759112f7ec4452b1aebed6c92bdfc72ba (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#ifndef WORDGAME_H
#define WORDGAME_H

#include "newgamebase.h"
#include "rulesbase.h"

#include <qpe/qdawg.h>
#include <qpe/applnk.h>

#include <qmainwindow.h>
#include <qcanvas.h>
#include <qlabel.h>

class QVBox;
class QLabel;
class QWidgetStack;
class QToolButton;
class Config;

class Tile {
public:
    Tile() {}

    Tile(const Tile& t)
    {
	txt = t.txt;
	val = t.val;
	blank = t.blank;
    }

    Tile(QString text, int value)
    {
	txt = text;
	val = value;
	blank = txt.isEmpty();
    }

    Tile(const QString& key);

    int value() const { return val; }
    bool isBlank() const { return blank; }
    QString text() const { return txt; }
    void setText(const QString& t)
    {
	txt = t;
    }

    int operator==(const Tile& o) const
	{ return o.txt == txt && o.val == val && o.blank == blank; }
    int operator!=(const Tile& o) const
	{ return !operator==(o); }
    Tile& operator=(const Tile& o)
	{ txt=o.txt; val=o.val; blank=o.blank; return *this; }

    QString key() const;

private:
    QString txt;
    int val;
    bool blank;
};

class Bag {
public:
    Bag();

    void readConfig(Config&);
    void writeConfig(Config&);

    void add(const Tile&);
    bool isEmpty() const { return tiles.isEmpty(); }
    Tile takeRandom();
private:
    QList<Tile> tiles;
};

class TileItem : public QCanvasRectangle {
public:
    TileItem(const Tile& tile, bool b, QCanvas* c) :
	QCanvasRectangle(0,0,
	    b?bigWidth():smallWidth(),
	    b?bigHeight():smallHeight(),c),
	t(tile), big(b), s(Firm)
    {
    }

    static int smallWidth();
    static int smallHeight();
    static int bigWidth();
    static int bigHeight();

    enum State { Firm, Floating };
    void setState( State state );
    State state() const { return s; }
    const Tile& tile() const { return t; }
    void setTile(const Tile&);
    void setBig(bool);

protected:
    void drawShape(QPainter&);

private:
    Tile t;
    bool big;
    State s;
};

class Rack : public QCanvasView {
public:
    Rack(int ntiles, QWidget* parent);
    ~Rack();

    void readConfig(Config&);
    void writeConfig(Config&);

    bool isFull() const { return count()==max(); }
    int max() const { return item.count(); }
    int count() const { return n; }
    void addTile(const Tile& t);
    Tile tile(int i) const { return item[i]->tile(); }
    const Tile* tileRef(int i) const { return &item[i]->tile(); }
    void remove(int i);
    void remove(Tile);
    bool arrangeTiles(const Tile** s, int sn);
    void setBlanks(const Tile*);

    void setPlayerName(const QString& name) { nm = name; }
    QString playerName() const { return nm; }
    void setComputerization(int level) { cpu=level; }
    bool computerized() const { return cpu>0; }

    QSize sizeHint() const;

protected:
    void resizeEvent(QResizeEvent*e);
    void contentsMousePressEvent(QMouseEvent*);
    void contentsMouseMoveEvent(QMouseEvent*);
    void contentsMouseReleaseEvent(QMouseEvent*);

private:
    void clear();
    void layoutTiles();
    int n;
    QArray<TileItem*> item;
    int dragging_adj;
    QPoint dragstart;
    QCanvasItem* dragging;
    QString nm;
    int cpu;
};

class Board : public QCanvasView {
    Q_OBJECT
public:
    Board(QPixmap bgshapes, int w, int h, QWidget* parent);
    ~Board();

    void readConfig(Config&);
    void writeConfig(Config&);

    int xTiles() const { return canvas()->tilesHorizontally(); }
    int yTiles() const { return canvas()->tilesVertically(); }

    bool contains(const QPoint& p) const
	{ return p.x() >= 0 && p.y() >= 0
	      && p.x() < canvas()->tilesHorizontally()
	      && p.y() < canvas()->tilesVertically(); }
    const Tile* tile(const QPoint& p) const
	{ TileItem* it=item(p); return it ? &it->tile() : 0; }

    void setRules(const QString& shapes, const int* effects);

    void clear();
    void unsetTile(const QPoint& p);
    void setTile(const QPoint& p, const Tile& t);

    void setTileState(const QPoint& p, TileItem::State s)
    {
	TileItem* it=item(p);
	if (it) it->setState(s);
    }

    void setCurrentRack(Rack*);
    void resetRack();
    void finalizeTurn();
    void showTurn();
    void scoreTurn(const QPoint& at, int n, const QPoint& d);
    bool checkTurn();
    int score(QPoint at, const Tile** tiles, int n,
	const Tile* blankvalue,
	const QPoint& d, bool ignoredict, QStringList* words) const;
    int bonussedValue(const QPoint& at, int base, int& all_mult) const;
    bool isStart(const QPoint& at) const;

    int turnScore() const { return turn_score; }

    QSize sizeHint() const;

signals:
    void temporaryScore(int);

protected:
    void contentsMousePressEvent(QMouseEvent*);
    void contentsMouseMoveEvent(QMouseEvent*);
    void contentsMouseReleaseEvent(QMouseEvent*);

private:
    int idx(const QPoint& p) const
	{ return p.x()+p.y()*canvas()->tilesHorizontally(); }
    TileItem*& item(const QPoint& p) const
	{ return grid[idx(p)]; }
    TileItem **grid;
    QString rule_shape;
    const int* rule_effect;
    int rack_tiles_bonus;
    Rack* current_rack;
    QPoint boardPos(const QPoint&) const;
    QPoint dragstart;
    QPoint shown_at;
    int shown_n;
    QPoint shown_step;
    void unshowTurn();
    int turn_score;
};

class ComputerPlayer
{
    Board* board;
    Rack* rack;

    bool across;
    int dict;
    QPoint current;

    const Tile** best;
    int best_n;
    Tile* best_blankvalues;
    int best_blused;
    int best_score;
    QPoint best_dir;
    QPoint best_start;

public:
    ComputerPlayer(Board* b, Rack* r);
    ~ComputerPlayer();

    bool step();

private:
    void findBest(QPoint at, const QPoint& d, const QDawg::Node* node, ulong used, uchar *nletter, const Tile** tiles, int n, Tile* blankvalues, int blused);
    void noteChoice(const Tile** tiles, int n, const QPoint& d, const Tile* blankvalues, int blused);
};

class ScoreInfo : public QLabel {
    Q_OBJECT
public:
    ScoreInfo( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
    ~ScoreInfo();

    void init(const QStringList&);
    void addScore(int player, int change);
    int playerScore(int player) const { return score[player]; }
    void setShowWinner(bool);
    void setBoldOne(int);

    void readConfig(Config&);
    void writeConfig(Config&);

protected:
    QSize sizeHint() const;

public slots:
    void showTemporaryScore(int amount);

private slots:
    void showScores();

private:
    QStringList names;
    int *score;
    QTimer* msgtimer;
    bool showwinner;
    int boldone;
};

class NewGame;

class WordGame : public QMainWindow {
    Q_OBJECT
public:
    WordGame( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
    ~WordGame();
    static QString appName() { return QString::fromLatin1("wordgame"); }

private slots:
    void endTurn();
    void resetTurn();
    void passTurn();
    void think();
    void endGame();
    void startGame();

private:
    void writeConfig();
    void readConfig();

    void startGame(const QStringList& pnames);
    bool mayEndGame();
    void openGameSelector(const QStringList& initnames);
    bool loadRules(const QString& filename);
    void addPlayer(const QString& name);
    void addPlayer(const QString& name, int cpu);
    void nextPlayer();
    bool refillRack(int i);
    void readyRack(int i);
    Rack* rack(int i) const;

    QWidgetStack *racks;
    QToolBar* toolbar;
    QWidget *vbox;
    Board *board;
    Bag *bag;
    ScoreInfo *scoreinfo;
    QToolButton *done;
    QToolButton *reset;
    QTimer* aiheart;
    ComputerPlayer *cpu;
    int player;
    int nplayers;
    QStringList namelist;
    bool gameover;
    QString rules;
    NewGame* newgame;
};

class NewGame : public NewGameBase {
    Q_OBJECT
public:
    NewGame(QWidget* parent);
    QStringList ruleslist;

public slots:
    void updateRuleSets();
};

class Rules : public RulesBase {
    Q_OBJECT

public:
    Rules(QWidget* parent);

signals:
    void rulesChanged();

public slots:
    void editRules();

private:
    void deleteRuleSet();
};

#endif // WORDGAME_H