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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* ksame 0.4 - simple Game
* Copyright (C) 1997,1998 Marcus Kreutzberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "StoneField.h"
#include <kdebug.h>
#include <assert.h>
StoneFieldState::StoneFieldState(const StoneField &stonefield)
{
field=new unsigned char[stonefield.maxstone];
for (int i=0;i<stonefield.maxstone;i++)
field[i]=stonefield.field[i].color;
colors=stonefield.colors;
board=stonefield.board;
score=stonefield.score;
gameover=stonefield.gameover;
}
StoneFieldState::~StoneFieldState() {
delete[] field;
}
void
StoneFieldState::restore(StoneField &stonefield) const {
for (int i=0;i<stonefield.maxstone;i++) {
stonefield.field[i].color=field[i];
stonefield.field[i].changed=true;
stonefield.field[i].marked=false;
}
stonefield.colors=colors;
stonefield.board=board;
stonefield.score=score;
stonefield.marked=0;
stonefield.gameover=gameover;
}
StoneField::StoneField(int width, int height,
int colors, unsigned int board,
bool undoenabled)
{
Q_ASSERT(width>0);
Q_ASSERT(height>0);
if (undoenabled) undolist=new TQPtrList<StoneFieldState>;
else undolist=0;
sizex=width;
sizey=height;
maxstone=sizex*sizey;
field=new Stone[maxstone];
newGame(board,colors);
m_gotBonus= false;
}
StoneField::~StoneField() {
delete[] field;
delete undolist;
kdDebug() << "~StoneField\n" << endl;
}
int StoneField::count(int color){
int c = 0;
Stone *stone=field;
for (int i=0;i<maxstone;i++,stone++) {
if(stone->color == color)
c++;
}
return c;
}
int
StoneField::width() const {
return sizex;
}
int
StoneField::height() const {
return sizey;
}
void
StoneField::newGame(unsigned int board,int colors) {
kdDebug() << "StoneField::newgame board "
<< board << " colors " << colors << endl;
if (colors<1) colors=3;
if (colors>7) colors=7;
this->colors=colors;
this->board=board;
reset();
}
void
StoneField::reset() {
random.setSeed(board);
Stone *stone=field;
for (int i=0;i<maxstone;i++,stone++) {
stone->color=1+random.getLong(colors);
stone->marked=false;
stone->changed=true;
}
gameover=-1;
score=0;
marked=0;
if (undolist) {
undolist->setAutoDelete(true);
undolist->clear();
}
int c[7];
int j;
for (j=0;j<7;j++) c[j]=0;
for (j=0,stone=field;j<maxstone;j++,stone++) {
c[stone->color]++;
}
kdDebug() << "red " << c[1] << endl;
kdDebug() << "blue " << c[2] << endl;
kdDebug() << "yellow " << c[3] << endl;
kdDebug() << "green " << c[4] << endl;
}
int
StoneField::map(int x,int y) {
assert (!(x<0||y<0||x>=sizex||y>=sizey));
return x+y*sizex;
}
int
StoneField::mark(int x,int y,bool force) {
int index=map(x,y);
if (index<0) {
unmark();
return 0;
}
if (field[index].marked) return -1;
unmark();
mark(index,field[index].color);
if (marked==1&&!force) {
field[index].marked=false;
marked=0;
}
return marked;
}
void
StoneField::mark(int index,unsigned char color) {
if ( index<0 || index>=maxstone ) return;
Stone &stone=field[index];
if (stone.marked) return;
if (!stone.color || stone.color!=color) return;
stone.changed=true;
stone.marked=true;
marked++;
// mark left
if ((index%sizex)!=0) mark(index-1,color);
// mark right
if (((index+1)%sizex)!=0) mark(index+1,color);
// mark upward
if (index>=sizex) mark(index-sizex,color);
// mark downward
if (index<(sizex-1)*sizey) mark(index+sizex,color);
}
void
StoneField::unmark() {
if (!marked) return;
Stone *stone=field;
for (int i=0;i<maxstone;i++,stone++) {
stone->marked=false;
stone->changed=true;
}
marked=0;
}
int
StoneField::remove(int x,int y,bool force) {
int index=map(x,y);
if (index<0) return 0;
if (!field[index].marked) {
mark(x,y,force);
}
if (!marked) return 0;
// remove a single stone??
if (marked==1&&!force) return 0;
// add current field to undolist
if (undolist)
undolist->append(new StoneFieldState(*this));
// increase score
if (marked>2)
score+=(marked-2)*(marked-2);
// remove marked stones
Stone *stone=field;
for (int i=0;i<maxstone;i++,stone++) {
if (stone->marked) {
stone->color=0;
stone->changed=true;
stone->marked=false;
}
}
int removed=marked;
marked=0;
for (int col=0;col<sizex;col++) {
int i1=col+maxstone-sizex;
while ( i1>=0 && field[i1].color ) i1-=sizex;
int i2=i1;
while (i2>=0) {
while ( i2>=0 && !field[i2].color ) i2-=sizex;
while ( i2>=0 && field[i2].color ) {
field[i1].color=field[i2].color;
field[i1].changed=true;
field[i2].color=0;
field[i2].changed=true;
i1-=sizex;
i2-=sizex;
}
}
}
// find the last column that has something
int lastcol = sizex;
while (lastcol > 0 && !field[map(lastcol-1, sizey-1)].color) {
lastcol--;
}
for (int col=0;col<lastcol-1;) {
bool empty = true;
for (int row = 0; row < sizey; row++)
if (field[map(col, row)].color) {
empty = false;
break;
}
if (!empty) {
col++;
continue;
}
int nextfullcol = col + 1;
while (nextfullcol < sizex &&
!field[map(nextfullcol, sizey - 1)].color)
nextfullcol++;
if (nextfullcol > sizex - 1)
break; // we're ready
for (int row=0; row < sizey; row++) {
int source = map(nextfullcol, row);
int dest = map(col, row);
field[dest].color=field[source].color;
field[dest].changed=true;
field[source].color=0;
field[source].changed=true;
}
}
// add a bonus, if field is empty
if (!field[map(0, sizey-1)].color) {
score+=1000;
m_gotBonus= true;
}
// gameover is undefined
gameover=-1;
return removed;
}
bool StoneField::undoPossible() const {
return !(!undolist||undolist->isEmpty());
}
int
StoneField::undo(int count) {
if (!undoPossible())
return 0;
if (count <= 0)
return 0;
int undocount=1;
StoneFieldState *state=0;
undolist->setAutoDelete(true);
while (--count>0) {
if (undolist->count()==1) break;
undolist->removeLast();
undocount++;
}
state=undolist->getLast();
Q_ASSERT(state);
state->restore(*this);
undolist->removeLast();
return undocount;
}
bool
StoneField::isGameover() const {
int i=maxstone-1;;
unsigned char color;
if (gameover>=0) return (bool)gameover;
// kdDebug() << "-->gameover" << endl;
while (i>=0) {
// kdDebug() << i << " " << field[i].color << endl;
// ignore empty fields
while ( i>=0 && field[i].color==0 ) i--;
// Wenn Stein gefunden,
// dann die Nachbarn auf gleiche Farbe pruefen.
while ( i>=0 && (color=field[i].color) ) {
// check left
if ( (i%sizex)!=0 && field[i-1].color==color)
goto check_gameover;
// check upward
if ( i>=sizex && field[i-sizex].color==color)
goto check_gameover;
i--;
}
}
check_gameover:
gameover=(i<0);
// kdDebug() << "<--gameover" << endl;
return (bool)gameover;
}
bool StoneField::gotBonus() const {
return m_gotBonus;
}
int
StoneField::getBoard() const {
return board;
}
int
StoneField::getScore() const {
return score;
}
int
StoneField::getColors() const {
return colors;
}
int
StoneField::getMarked() const {
return marked;
}
int
StoneField::getFieldSize() const {
return maxstone;
}
struct Stone *
StoneField::getField() const {
return field;
}
|