-rw-r--r-- | noncore/games/wordgame/wordgame.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/noncore/games/wordgame/wordgame.cpp b/noncore/games/wordgame/wordgame.cpp index 47d6725..52e2be2 100644 --- a/noncore/games/wordgame/wordgame.cpp +++ b/noncore/games/wordgame/wordgame.cpp | |||
@@ -1,404 +1,412 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 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 | 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 | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 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. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | 21 | ||
22 | #include "wordgame.h" | 22 | #include "wordgame.h" |
23 | 23 | ||
24 | #include <opie2/oresource.h> | ||
25 | |||
24 | #include <qpe/global.h> | 26 | #include <qpe/global.h> |
25 | #include <qpe/resource.h> | ||
26 | #include <qpe/config.h> | 27 | #include <qpe/config.h> |
27 | 28 | ||
28 | #include <qapplication.h> | 29 | #include <qapplication.h> |
29 | #include <qmessagebox.h> | 30 | #include <qmessagebox.h> |
30 | #include <qcombobox.h> | 31 | #include <qcombobox.h> |
31 | #include <qdir.h> | 32 | #include <qdir.h> |
32 | #include <qlineedit.h> | 33 | #include <qlineedit.h> |
33 | #include <qpushbutton.h> | 34 | #include <qpushbutton.h> |
34 | #include <qtextstream.h> | 35 | #include <qtextstream.h> |
35 | #include <qtimer.h> | 36 | #include <qtimer.h> |
36 | #include <qtoolbar.h> | 37 | #include <qtoolbar.h> |
37 | #include <qtoolbutton.h> | 38 | #include <qtoolbutton.h> |
38 | #include <qvbox.h> | 39 | #include <qvbox.h> |
39 | #include <qwidgetstack.h> | 40 | #include <qwidgetstack.h> |
40 | #include <qlayout.h> | 41 | #include <qlayout.h> |
41 | 42 | ||
42 | #include <stdlib.h> | 43 | #include <stdlib.h> |
43 | #include <unistd.h> | 44 | #include <unistd.h> |
44 | #include <pwd.h> | 45 | #include <pwd.h> |
45 | #include <sys/types.h> | 46 | #include <sys/types.h> |
46 | 47 | ||
47 | enum RuleEffects { | 48 | enum RuleEffects { |
48 | Multiplier=15, | 49 | Multiplier=15, |
49 | MultiplyAll=64, | 50 | MultiplyAll=64, |
50 | Start=128 | 51 | Start=128 |
51 | }; | 52 | }; |
52 | 53 | ||
53 | static int tile_smallw = 16; | 54 | static int tile_smallw = 16; |
54 | static int tile_smallh = 16; | 55 | static int tile_smallh = 16; |
55 | static int tile_bigw = 22; | 56 | static int tile_bigw = 22; |
56 | static int tile_bigh = 22; | 57 | static int tile_bigh = 22; |
57 | static int tile_stweak = -2; | 58 | static int tile_stweak = -2; |
58 | static int tile_btweak = -1; | 59 | static int tile_btweak = -1; |
59 | 60 | ||
60 | static const int rack_tiles=7; | 61 | static const int rack_tiles=7; |
61 | 62 | ||
62 | const char* sampleWGR= | 63 | const char* sampleWGR= |
63 | "wordgame_shapes\n" | 64 | "wordgame_shapes\n" |
64 | "15 15\n" | 65 | "15 15\n" |
65 | "400001040100004\n" | 66 | "400001040100004\n" |
66 | "030000000000030\n" | 67 | "030000000000030\n" |
67 | "002002000200200\n" | 68 | "002002000200200\n" |
68 | "000300020003000\n" | 69 | "000300020003000\n" |
69 | "000020000020000\n" | 70 | "000020000020000\n" |
70 | "102001000100201\n" | 71 | "102001000100201\n" |
71 | "000000202000000\n" | 72 | "000000202000000\n" |
72 | "400200050002004\n" | 73 | "400200050002004\n" |
73 | "000000202000000\n" | 74 | "000000202000000\n" |
74 | "102001000100201\n" | 75 | "102001000100201\n" |
75 | "000020000020000\n" | 76 | "000020000020000\n" |
76 | "000300020003000\n" | 77 | "000300020003000\n" |
77 | "002002000200200\n" | 78 | "002002000200200\n" |
78 | "030000000000030\n" | 79 | "030000000000030\n" |
79 | "400001040100004\n" | 80 | "400001040100004\n" |
80 | "1 2 3 66 67 194 100 0\n" | 81 | "1 2 3 66 67 194 100 0\n" |
81 | "1 j 8\n" | 82 | "1 j 8\n" |
82 | "1 q 7\n" | 83 | "1 q 7\n" |
83 | "1 x 6\n" | 84 | "1 x 6\n" |
84 | "1 z 6\n" | 85 | "1 z 6\n" |
85 | "1 w 4\n" | 86 | "1 w 4\n" |
86 | "1 k 4\n" | 87 | "1 k 4\n" |
87 | "1 v 3\n" | 88 | "1 v 3\n" |
88 | "1 f 3\n" | 89 | "1 f 3\n" |
89 | "2 y 3\n" | 90 | "2 y 3\n" |
90 | "2 h 2\n" | 91 | "2 h 2\n" |
91 | "2 b 2\n" | 92 | "2 b 2\n" |
92 | "2 m 2\n" | 93 | "2 m 2\n" |
93 | "3 p 2\n" | 94 | "3 p 2\n" |
94 | "3 g 2\n" | 95 | "3 g 2\n" |
95 | "3 u 2\n" | 96 | "3 u 2\n" |
96 | "4 d 2\n" | 97 | "4 d 2\n" |
97 | "4 c 2\n" | 98 | "4 c 2\n" |
98 | "5 l 1\n" | 99 | "5 l 1\n" |
99 | "5 o 1\n" | 100 | "5 o 1\n" |
100 | "7 t 1\n" | 101 | "7 t 1\n" |
101 | "7 n 1\n" | 102 | "7 n 1\n" |
102 | "7 a 1\n" | 103 | "7 a 1\n" |
103 | "7 r 1\n" | 104 | "7 r 1\n" |
104 | "8 s 1\n" | 105 | "8 s 1\n" |
105 | "8 i 1\n" | 106 | "8 i 1\n" |
106 | "11 e 1\n" | 107 | "11 e 1\n" |
107 | "0\n"; | 108 | "0\n"; |
108 | 109 | ||
109 | WordGame::WordGame( QWidget* parent, const char* name, WFlags fl ) : | 110 | WordGame::WordGame( QWidget* parent, const char* name, WFlags fl ) : |
110 | QMainWindow(parent, name, fl) | 111 | QMainWindow(parent, name, fl) |
111 | { | 112 | { |
112 | if ( qApp->desktop()->width() < 240 ) { | 113 | if ( qApp->desktop()->width() < 240 ) { |
113 | tile_smallw = 10; | 114 | tile_smallw = 10; |
114 | tile_smallh = 10; | 115 | tile_smallh = 10; |
115 | tile_bigw = 16; | 116 | tile_bigw = 16; |
116 | tile_bigh = 16; | 117 | tile_bigh = 16; |
117 | tile_stweak = 0; | 118 | tile_stweak = 0; |
118 | tile_btweak = 0; | 119 | tile_btweak = 0; |
119 | } | 120 | } |
120 | 121 | ||
121 | setIcon( Resource::loadPixmap( "wordgame/WordGame.png" ) ); | 122 | setIcon( Opie::Core::OResource::loadPixmap( "wordgame/WordGame" ) ); |
122 | setCaption( tr("Word Game") ); | 123 | setCaption( tr("Word Game") ); |
123 | 124 | ||
124 | setToolBarsMovable( FALSE ); | 125 | setToolBarsMovable( FALSE ); |
125 | vbox = new QVBox(this); | 126 | vbox = new QVBox(this); |
126 | 127 | ||
127 | setCentralWidget(vbox); | 128 | setCentralWidget(vbox); |
128 | toolbar = new QToolBar(this); | 129 | toolbar = new QToolBar(this); |
129 | addToolBar(toolbar, Bottom); | 130 | addToolBar(toolbar, Bottom); |
130 | reset = new QToolButton(Resource::loadPixmap("back"), tr("Back"), "", this, SLOT(resetTurn()), toolbar); | 131 | bool useBigIcon = qApp->desktop()->size().width() > 330; |
131 | done = new QToolButton(Resource::loadPixmap("done"), tr("Done"), "", this, SLOT(endTurn()), toolbar); | 132 | reset = new QToolButton(Opie::Core::OResource::loadPixmap("back", Opie::Core::OResource::SmallIcon), |
133 | tr("Back"), "", this, SLOT(resetTurn()), toolbar); | ||
134 | reset->setUsesBigPixmap( useBigIcon ); | ||
135 | done = new QToolButton(Opie::Core::OResource::loadPixmap("done", Opie::Core::OResource::SmallIcon), | ||
136 | tr("Done"), "", this, SLOT(endTurn()), toolbar); | ||
137 | done->setUsesBigPixmap( useBigIcon ); | ||
132 | scoreinfo = new ScoreInfo(toolbar); | 138 | scoreinfo = new ScoreInfo(toolbar); |
133 | scoreinfo->setFont(QFont("Helvetica",10)); | 139 | scoreinfo->setFont(QFont("Helvetica",10)); |
134 | new QToolButton(Resource::loadPixmap("finish"), tr("Close"), "", this, SLOT(endGame()), toolbar); | 140 | QToolButton *btn = new QToolButton(Opie::Core::OResource::loadPixmap("finish", Opie::Core::OResource::SmallIcon), |
141 | tr("Close"), "", this, SLOT(endGame()), toolbar); | ||
142 | btn->setUsesBigPixmap( useBigIcon ); | ||
135 | toolbar->setStretchableWidget(scoreinfo); | 143 | toolbar->setStretchableWidget(scoreinfo); |
136 | 144 | ||
137 | cpu = 0; | 145 | cpu = 0; |
138 | board = 0; | 146 | board = 0; |
139 | bag = 0; | 147 | bag = 0; |
140 | racks = 0; | 148 | racks = 0; |
141 | 149 | ||
142 | aiheart = new QTimer(this); | 150 | aiheart = new QTimer(this); |
143 | connect(aiheart, SIGNAL(timeout()), this, SLOT(think())); | 151 | connect(aiheart, SIGNAL(timeout()), this, SLOT(think())); |
144 | 152 | ||
145 | readConfig(); | 153 | readConfig(); |
146 | } | 154 | } |
147 | 155 | ||
148 | WordGame::~WordGame() | 156 | WordGame::~WordGame() |
149 | { | 157 | { |
150 | writeConfig(); | 158 | writeConfig(); |
151 | } | 159 | } |
152 | 160 | ||
153 | void WordGame::writeConfig() | 161 | void WordGame::writeConfig() |
154 | { | 162 | { |
155 | Config cfg("WordGame"); | 163 | Config cfg("WordGame"); |
156 | cfg.setGroup("Game"); | 164 | cfg.setGroup("Game"); |
157 | cfg.writeEntry("NameList",namelist,';'); | 165 | cfg.writeEntry("NameList",namelist,';'); |
158 | cfg.writeEntry("CurrentPlayer",gameover ? 0 : player+1); | 166 | cfg.writeEntry("CurrentPlayer",gameover ? 0 : player+1); |
159 | if ( !gameover ) { | 167 | if ( !gameover ) { |
160 | cfg.writeEntry("Rules",rules); | 168 | cfg.writeEntry("Rules",rules); |
161 | bag->writeConfig(cfg); | 169 | bag->writeConfig(cfg); |
162 | board->writeConfig(cfg); | 170 | board->writeConfig(cfg); |
163 | scoreinfo->writeConfig(cfg); | 171 | scoreinfo->writeConfig(cfg); |
164 | } | 172 | } |
165 | for (int p=0; p<nplayers; p++) { | 173 | for (int p=0; p<nplayers; p++) { |
166 | cfg.setGroup("Player"+QString::number(p+1)); | 174 | cfg.setGroup("Player"+QString::number(p+1)); |
167 | if ( gameover ) cfg.clearGroup(); else rack(p)->writeConfig(cfg); | 175 | if ( gameover ) cfg.clearGroup(); else rack(p)->writeConfig(cfg); |
168 | } | 176 | } |
169 | } | 177 | } |
170 | 178 | ||
171 | void WordGame::readConfig() | 179 | void WordGame::readConfig() |
172 | { | 180 | { |
173 | Config cfg("WordGame"); | 181 | Config cfg("WordGame"); |
174 | cfg.setGroup("Game"); | 182 | cfg.setGroup("Game"); |
175 | int currentplayer = cfg.readNumEntry("CurrentPlayer",0); | 183 | int currentplayer = cfg.readNumEntry("CurrentPlayer",0); |
176 | QStringList pnames = cfg.readListEntry("NameList",';'); | 184 | QStringList pnames = cfg.readListEntry("NameList",';'); |
177 | if ( currentplayer ) { | 185 | if ( currentplayer ) { |
178 | gameover = FALSE; | 186 | gameover = FALSE; |
179 | rules = cfg.readEntry("Rules"); | 187 | rules = cfg.readEntry("Rules"); |
180 | if ( rules.find("x-wordgamerules") >= 0 ) { | 188 | if ( rules.find("x-wordgamerules") >= 0 ) { |
181 | // rules files moved | 189 | // rules files moved |
182 | rules = "Sample.rules"; | 190 | rules = "Sample.rules"; |
183 | } | 191 | } |
184 | if ( loadRules(rules) ) { | 192 | if ( loadRules(rules) ) { |
185 | startGame(pnames); | 193 | startGame(pnames); |
186 | bag->readConfig(cfg); | 194 | bag->readConfig(cfg); |
187 | board->readConfig(cfg); | 195 | board->readConfig(cfg); |
188 | scoreinfo->readConfig(cfg); | 196 | scoreinfo->readConfig(cfg); |
189 | for (int p=0; p<nplayers; p++) { | 197 | for (int p=0; p<nplayers; p++) { |
190 | cfg.setGroup("Player"+QString::number(p+1)); | 198 | cfg.setGroup("Player"+QString::number(p+1)); |
191 | rack(p)->readConfig(cfg); | 199 | rack(p)->readConfig(cfg); |
192 | } | 200 | } |
193 | player=currentplayer-1; | 201 | player=currentplayer-1; |
194 | readyRack(player); | 202 | readyRack(player); |
195 | return; | 203 | return; |
196 | } | 204 | } |
197 | } | 205 | } |
198 | // fall-back | 206 | // fall-back |
199 | openGameSelector(pnames); | 207 | openGameSelector(pnames); |
200 | } | 208 | } |
201 | 209 | ||
202 | void WordGame::openGameSelector(const QStringList& initnames) | 210 | void WordGame::openGameSelector(const QStringList& initnames) |
203 | { | 211 | { |
204 | toolbar->hide(); | 212 | toolbar->hide(); |
205 | gameover = FALSE; | 213 | gameover = FALSE; |
206 | 214 | ||
207 | delete board; | 215 | delete board; |
208 | board = 0; | 216 | board = 0; |
209 | delete racks; | 217 | delete racks; |
210 | racks = 0; | 218 | racks = 0; |
211 | 219 | ||
212 | delete cpu; | 220 | delete cpu; |
213 | cpu = 0; | 221 | cpu = 0; |
214 | 222 | ||
215 | newgame = new NewGame(vbox); | 223 | newgame = new NewGame(vbox); |
216 | 224 | ||
217 | //Rules rules(this); | 225 | //Rules rules(this); |
218 | //connect(game.editrules, SIGNAL(clicked()), &rules, SLOT(editRules())); | 226 | //connect(game.editrules, SIGNAL(clicked()), &rules, SLOT(editRules())); |
219 | //connect(&rules, SIGNAL(rulesChanged()), &game, SLOT(updateRuleSets())); | 227 | //connect(&rules, SIGNAL(rulesChanged()), &game, SLOT(updateRuleSets())); |
220 | struct passwd* n = getpwuid(getuid()); | 228 | struct passwd* n = getpwuid(getuid()); |
221 | QString playername = n ? n->pw_name : ""; | 229 | QString playername = n ? n->pw_name : ""; |
222 | if ( playername.isEmpty() ) { | 230 | if ( playername.isEmpty() ) { |
223 | playername = "Player"; | 231 | playername = "Player"; |
224 | } | 232 | } |
225 | newgame->player0->changeItem(playername,0); | 233 | newgame->player0->changeItem(playername,0); |
226 | newgame->player1->setCurrentItem(1); | 234 | newgame->player1->setCurrentItem(1); |
227 | newgame->updateRuleSets(); | 235 | newgame->updateRuleSets(); |
228 | newgame->show(); | 236 | newgame->show(); |
229 | 237 | ||
230 | connect(newgame->buttonOk, SIGNAL(clicked()), this, SLOT(startGame())); | 238 | connect(newgame->buttonOk, SIGNAL(clicked()), this, SLOT(startGame())); |
231 | } | 239 | } |
232 | 240 | ||
233 | void WordGame::startGame() | 241 | void WordGame::startGame() |
234 | { | 242 | { |
235 | rules = newgame->ruleslist[newgame->rules->currentItem()]; | 243 | rules = newgame->ruleslist[newgame->rules->currentItem()]; |
236 | if ( loadRules(rules) ) { | 244 | if ( loadRules(rules) ) { |
237 | QStringList names; | 245 | QStringList names; |
238 | names.append(newgame->player0->currentText()); | 246 | names.append(newgame->player0->currentText()); |
239 | names.append(newgame->player1->currentText()); | 247 | names.append(newgame->player1->currentText()); |
240 | names.append(newgame->player2->currentText()); | 248 | names.append(newgame->player2->currentText()); |
241 | names.append(newgame->player3->currentText()); | 249 | names.append(newgame->player3->currentText()); |
242 | names.append(newgame->player4->currentText()); | 250 | names.append(newgame->player4->currentText()); |
243 | names.append(newgame->player5->currentText()); | 251 | names.append(newgame->player5->currentText()); |
244 | delete newgame; | 252 | delete newgame; |
245 | startGame(names); | 253 | startGame(names); |
246 | } else { | 254 | } else { |
247 | // error... | 255 | // error... |
248 | delete newgame; | 256 | delete newgame; |
249 | close(); | 257 | close(); |
250 | } | 258 | } |
251 | } | 259 | } |
252 | 260 | ||
253 | void WordGame::startGame(const QStringList& playerlist) | 261 | void WordGame::startGame(const QStringList& playerlist) |
254 | { | 262 | { |
255 | toolbar->show(); | 263 | toolbar->show(); |
256 | racks = new QWidgetStack(vbox); | 264 | racks = new QWidgetStack(vbox); |
257 | racks->setFixedHeight(TileItem::bigHeight()+2); | 265 | racks->setFixedHeight(TileItem::bigHeight()+2); |
258 | namelist.clear(); | 266 | namelist.clear(); |
259 | nplayers=0; | 267 | nplayers=0; |
260 | for (QStringList::ConstIterator it=playerlist.begin(); it!=playerlist.end(); ++it) | 268 | for (QStringList::ConstIterator it=playerlist.begin(); it!=playerlist.end(); ++it) |
261 | addPlayer(*it); | 269 | addPlayer(*it); |
262 | scoreinfo->init(namelist); | 270 | scoreinfo->init(namelist); |
263 | 271 | ||
264 | if ( nplayers ) { | 272 | if ( nplayers ) { |
265 | player=0; | 273 | player=0; |
266 | readyRack(player); | 274 | readyRack(player); |
267 | } | 275 | } |
268 | 276 | ||
269 | board->show(); | 277 | board->show(); |
270 | racks->show(); | 278 | racks->show(); |
271 | } | 279 | } |
272 | 280 | ||
273 | bool WordGame::loadRules(const QString &name) | 281 | bool WordGame::loadRules(const QString &name) |
274 | { | 282 | { |
275 | QString filename = Global::applicationFileName( "wordgame", name ); | 283 | QString filename = Global::applicationFileName( "wordgame", name ); |
276 | QFile file( filename ); | 284 | QFile file( filename ); |
277 | if ( !file.open( IO_ReadOnly ) ) | 285 | if ( !file.open( IO_ReadOnly ) ) |
278 | return FALSE; | 286 | return FALSE; |
279 | 287 | ||
280 | QTextStream ts( &file ); | 288 | QTextStream ts( &file ); |
281 | 289 | ||
282 | QString title = name; | 290 | QString title = name; |
283 | title.truncate( title.length() - 6 ); | 291 | title.truncate( title.length() - 6 ); |
284 | //setCaption( title ); | 292 | //setCaption( title ); |
285 | 293 | ||
286 | QString shapepixmap; | 294 | QString shapepixmap; |
287 | ts >> shapepixmap; | 295 | ts >> shapepixmap; |
288 | int htiles,vtiles; | 296 | int htiles,vtiles; |
289 | ts >> htiles >> vtiles; | 297 | ts >> htiles >> vtiles; |
290 | 298 | ||
291 | if ( htiles < 3 || vtiles < 3 ) | 299 | if ( htiles < 3 || vtiles < 3 ) |
292 | return FALSE; | 300 | return FALSE; |
293 | 301 | ||
294 | QString rule_shapes; | 302 | QString rule_shapes; |
295 | for (int i=0; i<vtiles; i++) { | 303 | for (int i=0; i<vtiles; i++) { |
296 | QString line; | 304 | QString line; |
297 | ts >> line; | 305 | ts >> line; |
298 | rule_shapes += line; | 306 | rule_shapes += line; |
299 | } | 307 | } |
300 | static int rule_effects[12]; | 308 | static int rule_effects[12]; |
301 | int re=0,e; | 309 | int re=0,e; |
302 | ts >> e; | 310 | ts >> e; |
303 | while ( e && re < 10 ) { | 311 | while ( e && re < 10 ) { |
304 | rule_effects[re] = e; | 312 | rule_effects[re] = e; |
305 | if ( re++ < 10 ) ts >> e; | 313 | if ( re++ < 10 ) ts >> e; |
306 | } | 314 | } |
307 | 315 | ||
308 | QImage shim = Resource::loadImage("wordgame/wordgame_shapes"); | 316 | QImage shim = Opie::Core::OResource::loadImage("wordgame/wordgame_shapes"); |
309 | shim = shim.smoothScale((re-1)*TileItem::smallWidth(),TileItem::smallHeight()); | 317 | shim = shim.smoothScale((re-1)*TileItem::smallWidth(),TileItem::smallHeight()); |
310 | QPixmap bgshapes; | 318 | QPixmap bgshapes; |
311 | bgshapes.convertFromImage(shim); | 319 | bgshapes.convertFromImage(shim); |
312 | 320 | ||
313 | rule_effects[re++] = 100; // default bonus | 321 | rule_effects[re++] = 100; // default bonus |
314 | board = new Board(bgshapes, htiles, vtiles, vbox); | 322 | board = new Board(bgshapes, htiles, vtiles, vbox); |
315 | board->setRules(rule_shapes, rule_effects); | 323 | board->setRules(rule_shapes, rule_effects); |
316 | connect(board, SIGNAL(temporaryScore(int)), scoreinfo, SLOT(showTemporaryScore(int))); | 324 | connect(board, SIGNAL(temporaryScore(int)), scoreinfo, SLOT(showTemporaryScore(int))); |
317 | 325 | ||
318 | bag = new Bag; | 326 | bag = new Bag; |
319 | 327 | ||
320 | int count; | 328 | int count; |
321 | ts >> count; | 329 | ts >> count; |
322 | while ( count ) { | 330 | while ( count ) { |
323 | QString text; | 331 | QString text; |
324 | int value; | 332 | int value; |
325 | ts >> text >> value; | 333 | ts >> text >> value; |
326 | if ( text == "_" ) | 334 | if ( text == "_" ) |
327 | text = ""; | 335 | text = ""; |
328 | 336 | ||
329 | Tile t(text, value); | 337 | Tile t(text, value); |
330 | for (int n=count; n--; ) | 338 | for (int n=count; n--; ) |
331 | bag->add(t); | 339 | bag->add(t); |
332 | 340 | ||
333 | ts >> count; | 341 | ts >> count; |
334 | } | 342 | } |
335 | 343 | ||
336 | return TRUE; | 344 | return TRUE; |
337 | } | 345 | } |
338 | 346 | ||
339 | 347 | ||
340 | NewGame::NewGame(QWidget* parent) : | 348 | NewGame::NewGame(QWidget* parent) : |
341 | NewGameBase(parent) | 349 | NewGameBase(parent) |
342 | { | 350 | { |
343 | } | 351 | } |
344 | 352 | ||
345 | void NewGame::updateRuleSets() | 353 | void NewGame::updateRuleSets() |
346 | { | 354 | { |
347 | rules->clear(); | 355 | rules->clear(); |
348 | 356 | ||
349 | QString rulesDir = Global::applicationFileName( "wordgame", "" ); | 357 | QString rulesDir = Global::applicationFileName( "wordgame", "" ); |
350 | QDir dir( rulesDir, "*.rules" ); | 358 | QDir dir( rulesDir, "*.rules" ); |
351 | ruleslist = dir.entryList(); | 359 | ruleslist = dir.entryList(); |
352 | if ( ruleslist.isEmpty() ) { | 360 | if ( ruleslist.isEmpty() ) { |
353 | // Provide a sample | 361 | // Provide a sample |
354 | QFile file( rulesDir + "Sample.rules" ); | 362 | QFile file( rulesDir + "Sample.rules" ); |
355 | if ( file.open( IO_WriteOnly ) ) { | 363 | if ( file.open( IO_WriteOnly ) ) { |
356 | file.writeBlock( sampleWGR, strlen(sampleWGR) ); | 364 | file.writeBlock( sampleWGR, strlen(sampleWGR) ); |
357 | file.close(); | 365 | file.close(); |
358 | updateRuleSets(); | 366 | updateRuleSets(); |
359 | } | 367 | } |
360 | return; | 368 | return; |
361 | } | 369 | } |
362 | int newest=0; | 370 | int newest=0; |
363 | int newest_age=INT_MAX; | 371 | int newest_age=INT_MAX; |
364 | QDateTime now = QDateTime::currentDateTime(); | 372 | QDateTime now = QDateTime::currentDateTime(); |
365 | QStringList::Iterator it; | 373 | QStringList::Iterator it; |
366 | for ( it = ruleslist.begin(); it != ruleslist.end(); ++it ) { | 374 | for ( it = ruleslist.begin(); it != ruleslist.end(); ++it ) { |
367 | QFileInfo fi((*it)); | 375 | QFileInfo fi((*it)); |
368 | int age = fi.lastModified().secsTo(now); | 376 | int age = fi.lastModified().secsTo(now); |
369 | QString name = *it; | 377 | QString name = *it; |
370 | name.truncate( name.length()-6 ); // remove extension | 378 | name.truncate( name.length()-6 ); // remove extension |
371 | rules->insertItem( name ); | 379 | rules->insertItem( name ); |
372 | if ( age < newest_age ) { | 380 | if ( age < newest_age ) { |
373 | newest_age = age; | 381 | newest_age = age; |
374 | newest = rules->count()-1; | 382 | newest = rules->count()-1; |
375 | } | 383 | } |
376 | } | 384 | } |
377 | rules->setCurrentItem(newest); | 385 | rules->setCurrentItem(newest); |
378 | } | 386 | } |
379 | 387 | ||
380 | Rules::Rules(QWidget* parent) : | 388 | Rules::Rules(QWidget* parent) : |
381 | RulesBase(parent,0,TRUE) | 389 | RulesBase(parent,0,TRUE) |
382 | { | 390 | { |
383 | } | 391 | } |
384 | 392 | ||
385 | void Rules::editRules() | 393 | void Rules::editRules() |
386 | { | 394 | { |
387 | if ( exec() ) { | 395 | if ( exec() ) { |
388 | // ### create a new set of rules | 396 | // ### create a new set of rules |
389 | emit rulesChanged(); | 397 | emit rulesChanged(); |
390 | } | 398 | } |
391 | } | 399 | } |
392 | 400 | ||
393 | void Rules::deleteRuleSet() | 401 | void Rules::deleteRuleSet() |
394 | { | 402 | { |
395 | // ### delete existing rule set | 403 | // ### delete existing rule set |
396 | emit rulesChanged(); | 404 | emit rulesChanged(); |
397 | } | 405 | } |
398 | 406 | ||
399 | void WordGame::addPlayer(const QString& name) | 407 | void WordGame::addPlayer(const QString& name) |
400 | { | 408 | { |
401 | if ( !name.isEmpty() ) { | 409 | if ( !name.isEmpty() ) { |
402 | int colon = name.find(':'); | 410 | int colon = name.find(':'); |
403 | int cpu = (colon >=0 && name.left(2) == "AI") ? name.mid(2,1).toInt() : 0; | 411 | int cpu = (colon >=0 && name.left(2) == "AI") ? name.mid(2,1).toInt() : 0; |
404 | addPlayer(name,cpu); | 412 | addPlayer(name,cpu); |
@@ -1269,193 +1277,193 @@ void Rack::layoutTiles() | |||
1269 | canvas()->update(); | 1277 | canvas()->update(); |
1270 | } | 1278 | } |
1271 | 1279 | ||
1272 | void Rack::setBlanks(const Tile* bv) | 1280 | void Rack::setBlanks(const Tile* bv) |
1273 | { | 1281 | { |
1274 | for (int j=0; j<n; j++) { | 1282 | for (int j=0; j<n; j++) { |
1275 | Tile tt = item[j]->tile(); | 1283 | Tile tt = item[j]->tile(); |
1276 | if ( tt.isBlank() ) { | 1284 | if ( tt.isBlank() ) { |
1277 | tt.setText(bv->text()); | 1285 | tt.setText(bv->text()); |
1278 | item[j]->setTile(tt); | 1286 | item[j]->setTile(tt); |
1279 | bv++; | 1287 | bv++; |
1280 | } | 1288 | } |
1281 | } | 1289 | } |
1282 | } | 1290 | } |
1283 | 1291 | ||
1284 | bool Rack::arrangeTiles(const Tile** s, int sn) | 1292 | bool Rack::arrangeTiles(const Tile** s, int sn) |
1285 | { | 1293 | { |
1286 | bool could = TRUE; | 1294 | bool could = TRUE; |
1287 | for (int j=0; j<n; j++) { | 1295 | for (int j=0; j<n; j++) { |
1288 | Tile tt = item[j]->tile(); | 1296 | Tile tt = item[j]->tile(); |
1289 | int f=-1; | 1297 | int f=-1; |
1290 | for (int i=0; i<sn && f<0; i++) { | 1298 | for (int i=0; i<sn && f<0; i++) { |
1291 | if (s[i] && *s[i] == tt ) { | 1299 | if (s[i] && *s[i] == tt ) { |
1292 | s[i]=0; | 1300 | s[i]=0; |
1293 | f=i; | 1301 | f=i; |
1294 | } | 1302 | } |
1295 | } | 1303 | } |
1296 | if ( f >= 0 ) { | 1304 | if ( f >= 0 ) { |
1297 | item[j]->move(f-999,0); | 1305 | item[j]->move(f-999,0); |
1298 | } else { | 1306 | } else { |
1299 | could = FALSE; | 1307 | could = FALSE; |
1300 | } | 1308 | } |
1301 | } | 1309 | } |
1302 | layoutTiles(); | 1310 | layoutTiles(); |
1303 | return could; | 1311 | return could; |
1304 | } | 1312 | } |
1305 | 1313 | ||
1306 | void Rack::addTile(const Tile& t) | 1314 | void Rack::addTile(const Tile& t) |
1307 | { | 1315 | { |
1308 | TileItem *i = new TileItem(t,TRUE,canvas()); | 1316 | TileItem *i = new TileItem(t,TRUE,canvas()); |
1309 | i->show(); | 1317 | i->show(); |
1310 | item[n++] = i; | 1318 | item[n++] = i; |
1311 | layoutTiles(); | 1319 | layoutTiles(); |
1312 | } | 1320 | } |
1313 | 1321 | ||
1314 | void Rack::remove(Tile t) | 1322 | void Rack::remove(Tile t) |
1315 | { | 1323 | { |
1316 | for (int i=0; i<n ;i++) | 1324 | for (int i=0; i<n ;i++) |
1317 | if ( item[i]->tile() == t ) { | 1325 | if ( item[i]->tile() == t ) { |
1318 | remove(i); | 1326 | remove(i); |
1319 | return; | 1327 | return; |
1320 | } | 1328 | } |
1321 | } | 1329 | } |
1322 | 1330 | ||
1323 | void Rack::remove(int i) | 1331 | void Rack::remove(int i) |
1324 | { | 1332 | { |
1325 | delete item[i]; | 1333 | delete item[i]; |
1326 | n--; | 1334 | n--; |
1327 | for (;i<n;i++) | 1335 | for (;i<n;i++) |
1328 | item[i]=item[i+1]; | 1336 | item[i]=item[i+1]; |
1329 | layoutTiles(); | 1337 | layoutTiles(); |
1330 | } | 1338 | } |
1331 | 1339 | ||
1332 | void Rack::resizeEvent(QResizeEvent* e) | 1340 | void Rack::resizeEvent(QResizeEvent* e) |
1333 | { | 1341 | { |
1334 | canvas()->resize(width()-frameWidth()*2,height()-frameWidth()*2); | 1342 | canvas()->resize(width()-frameWidth()*2,height()-frameWidth()*2); |
1335 | QCanvasView::resizeEvent(e); | 1343 | QCanvasView::resizeEvent(e); |
1336 | } | 1344 | } |
1337 | 1345 | ||
1338 | void Rack::contentsMousePressEvent(QMouseEvent* e) | 1346 | void Rack::contentsMousePressEvent(QMouseEvent* e) |
1339 | { | 1347 | { |
1340 | if ( computerized() ) | 1348 | if ( computerized() ) |
1341 | return; | 1349 | return; |
1342 | QCanvasItemList list = canvas()->collisions(e->pos()); | 1350 | QCanvasItemList list = canvas()->collisions(e->pos()); |
1343 | if (list.count()) { | 1351 | if (list.count()) { |
1344 | dragging = list.first(); | 1352 | dragging = list.first(); |
1345 | dragstart = e->pos()-QPoint(int(dragging->x()),int(dragging->y())); | 1353 | dragstart = e->pos()-QPoint(int(dragging->x()),int(dragging->y())); |
1346 | } else { | 1354 | } else { |
1347 | dragging = 0; | 1355 | dragging = 0; |
1348 | } | 1356 | } |
1349 | } | 1357 | } |
1350 | 1358 | ||
1351 | void Rack::contentsMouseMoveEvent(QMouseEvent* e) | 1359 | void Rack::contentsMouseMoveEvent(QMouseEvent* e) |
1352 | { | 1360 | { |
1353 | if ( computerized() ) | 1361 | if ( computerized() ) |
1354 | return; | 1362 | return; |
1355 | //int w = TileItem::bigWidth()+2; | 1363 | //int w = TileItem::bigWidth()+2; |
1356 | if ( dragging ) { | 1364 | if ( dragging ) { |
1357 | dragging_adj = TileItem::bigWidth()/2; | 1365 | dragging_adj = TileItem::bigWidth()/2; |
1358 | if ( dragging->x() > e->x()-dragstart.x() ) | 1366 | if ( dragging->x() > e->x()-dragstart.x() ) |
1359 | dragging_adj = -dragging_adj; | 1367 | dragging_adj = -dragging_adj; |
1360 | dragging->move(e->x()-dragstart.x(),0); | 1368 | dragging->move(e->x()-dragstart.x(),0); |
1361 | layoutTiles(); | 1369 | layoutTiles(); |
1362 | } | 1370 | } |
1363 | } | 1371 | } |
1364 | 1372 | ||
1365 | void Rack::contentsMouseReleaseEvent(QMouseEvent* e) | 1373 | void Rack::contentsMouseReleaseEvent(QMouseEvent* /*e*/) |
1366 | { | 1374 | { |
1367 | if ( computerized() ) | 1375 | if ( computerized() ) |
1368 | return; | 1376 | return; |
1369 | if ( dragging ) { | 1377 | if ( dragging ) { |
1370 | dragging=0; | 1378 | dragging=0; |
1371 | layoutTiles(); | 1379 | layoutTiles(); |
1372 | } | 1380 | } |
1373 | } | 1381 | } |
1374 | 1382 | ||
1375 | Tile::Tile(const QString& key) | 1383 | Tile::Tile(const QString& key) |
1376 | { | 1384 | { |
1377 | int a=key.find('@'); | 1385 | int a=key.find('@'); |
1378 | txt = key.left(a); | 1386 | txt = key.left(a); |
1379 | val = key.mid(a+1).toInt(); | 1387 | val = key.mid(a+1).toInt(); |
1380 | blank = txt.isEmpty(); | 1388 | blank = txt.isEmpty(); |
1381 | } | 1389 | } |
1382 | 1390 | ||
1383 | QString Tile::key() const | 1391 | QString Tile::key() const |
1384 | { | 1392 | { |
1385 | return txt+"@"+QString::number(val); | 1393 | return txt+"@"+QString::number(val); |
1386 | } | 1394 | } |
1387 | 1395 | ||
1388 | Bag::Bag() | 1396 | Bag::Bag() |
1389 | { | 1397 | { |
1390 | tiles.setAutoDelete(TRUE); | 1398 | tiles.setAutoDelete(TRUE); |
1391 | } | 1399 | } |
1392 | 1400 | ||
1393 | void Bag::writeConfig(Config& cfg) | 1401 | void Bag::writeConfig(Config& cfg) |
1394 | { | 1402 | { |
1395 | QStringList t; | 1403 | QStringList t; |
1396 | for (QListIterator<Tile> it(tiles); it; ++it) | 1404 | for (QListIterator<Tile> it(tiles); it; ++it) |
1397 | t.append((*it)->key()); | 1405 | t.append((*it)->key()); |
1398 | cfg.writeEntry("Tiles",t,';'); | 1406 | cfg.writeEntry("Tiles",t,';'); |
1399 | } | 1407 | } |
1400 | 1408 | ||
1401 | void Bag::readConfig(Config& cfg) | 1409 | void Bag::readConfig(Config& cfg) |
1402 | { | 1410 | { |
1403 | tiles.clear(); | 1411 | tiles.clear(); |
1404 | QStringList t = cfg.readListEntry("Tiles",';'); | 1412 | QStringList t = cfg.readListEntry("Tiles",';'); |
1405 | for (QStringList::ConstIterator it=t.begin(); it!=t.end(); ++it ) | 1413 | for (QStringList::ConstIterator it=t.begin(); it!=t.end(); ++it ) |
1406 | add(Tile(*it)); | 1414 | add(Tile(*it)); |
1407 | } | 1415 | } |
1408 | 1416 | ||
1409 | void Bag::add(const Tile& t) | 1417 | void Bag::add(const Tile& t) |
1410 | { | 1418 | { |
1411 | tiles.append(new Tile(t)); | 1419 | tiles.append(new Tile(t)); |
1412 | } | 1420 | } |
1413 | 1421 | ||
1414 | Tile Bag::takeRandom() | 1422 | Tile Bag::takeRandom() |
1415 | { | 1423 | { |
1416 | Tile* rp = tiles.take(random()%tiles.count()); | 1424 | Tile* rp = tiles.take(random()%tiles.count()); |
1417 | Tile r=*rp; | 1425 | Tile r=*rp; |
1418 | return r; | 1426 | return r; |
1419 | } | 1427 | } |
1420 | 1428 | ||
1421 | ScoreInfo::ScoreInfo( QWidget* parent, const char* name, WFlags fl ) : | 1429 | ScoreInfo::ScoreInfo( QWidget* parent, const char* name, WFlags fl ) : |
1422 | QLabel("<P>",parent,name,fl) | 1430 | QLabel("<P>",parent,name,fl) |
1423 | { | 1431 | { |
1424 | score=0; | 1432 | score=0; |
1425 | msgtimer = new QTimer(this); | 1433 | msgtimer = new QTimer(this); |
1426 | connect(msgtimer, SIGNAL(timeout()), this, SLOT(showScores())); | 1434 | connect(msgtimer, SIGNAL(timeout()), this, SLOT(showScores())); |
1427 | setBackgroundMode( PaletteButton ); | 1435 | setBackgroundMode( PaletteButton ); |
1428 | } | 1436 | } |
1429 | 1437 | ||
1430 | ScoreInfo::~ScoreInfo() | 1438 | ScoreInfo::~ScoreInfo() |
1431 | { | 1439 | { |
1432 | if ( score ) delete [] score; | 1440 | if ( score ) delete [] score; |
1433 | } | 1441 | } |
1434 | 1442 | ||
1435 | void ScoreInfo::writeConfig(Config& cfg) | 1443 | void ScoreInfo::writeConfig(Config& cfg) |
1436 | { | 1444 | { |
1437 | QStringList l; | 1445 | QStringList l; |
1438 | for (int i=0; i<(int)names.count(); i++) | 1446 | for (int i=0; i<(int)names.count(); i++) |
1439 | l.append(QString::number(score[i])); | 1447 | l.append(QString::number(score[i])); |
1440 | cfg.writeEntry("Scores",l,';'); | 1448 | cfg.writeEntry("Scores",l,';'); |
1441 | } | 1449 | } |
1442 | 1450 | ||
1443 | void ScoreInfo::readConfig(Config& cfg) | 1451 | void ScoreInfo::readConfig(Config& cfg) |
1444 | { | 1452 | { |
1445 | QStringList l = cfg.readListEntry("Scores",';'); | 1453 | QStringList l = cfg.readListEntry("Scores",';'); |
1446 | int i=0; | 1454 | int i=0; |
1447 | for (QStringList::ConstIterator it=l.begin(); it!=l.end(); ++it ) | 1455 | for (QStringList::ConstIterator it=l.begin(); it!=l.end(); ++it ) |
1448 | score[i++]=(*it).toInt(); | 1456 | score[i++]=(*it).toInt(); |
1449 | showScores(); | 1457 | showScores(); |
1450 | } | 1458 | } |
1451 | 1459 | ||
1452 | 1460 | ||
1453 | QSize ScoreInfo::sizeHint() const | 1461 | QSize ScoreInfo::sizeHint() const |
1454 | { | 1462 | { |
1455 | return QSize(QLabel::sizeHint().width(),fontMetrics().height()); | 1463 | return QSize(QLabel::sizeHint().width(),fontMetrics().height()); |
1456 | } | 1464 | } |
1457 | 1465 | ||
1458 | void ScoreInfo::init(const QStringList& namelist) | 1466 | void ScoreInfo::init(const QStringList& namelist) |
1459 | { | 1467 | { |
1460 | names = namelist; | 1468 | names = namelist; |
1461 | if ( score ) delete [] score; | 1469 | if ( score ) delete [] score; |