3 PlayField::PlayField(const QString &_title, const QString &_board, const QString &_solution):title(_title),solution(_solution)
5 for(int w=0;w<PF::FIELD_WIDTH;w++)
6 for(int h=0;h<PF::FIELD_HEIGHT;h++)
7 field[w][h]=PF::CELL_WALL;
8 QStringList rows=_board.split("/");
9 QListIterator<QString> rowI(rows);
13 QString row=rowI.next();
15 for(int i=0;i<row.size();i++)
17 char ch=row[i].toAscii();
19 while(ch>='0' && ch<='9')
22 ch=row[++i].toAscii();
23 if(i>=row.size()) break;
28 field[w++][h]=PF::CELL_EMPTY;
30 if(ch>='a' && ch <= 'h')
32 field[w++][h]=ch-'a'+2;
39 void PlayField::move(int w, int h, int w_new)
42 (w_new >= PF::FIELD_WIDTH) || w == w_new)
46 if(get(w,h)==PF::CELL_EMPTY ||
47 get(w,h)==PF::CELL_WALL)
63 cont=moveBlock(w,h,d,0);
68 }//if coordination has changed, inc the moves
69 } while(cont && // still moving
70 (w!=w_new) && // didn't arrived
71 ((h_below >= PF::FIELD_HEIGHT) || (get(w,h_below)!=PF::CELL_EMPTY && get(w,h_below) != get(w,h))) // there is something below
76 cont|=checkGlobalFall();
81 if(totalUndo<PF::MAX_UNDO) totalUndo++;
82 copy(temp,undos[currentUndo]);
83 undoMade[currentUndo]=movesTemp;
84 currentUndo=(currentUndo + 1) % PF::MAX_UNDO;
88 bool PlayField::moveBlock(int w, int h, int dw, int dh)
93 if(wn<0 || wn >= PF::FIELD_WIDTH || hn < 0 || hn >= PF::FIELD_HEIGHT)
96 if(get(wn,hn)!=PF::CELL_EMPTY)
98 set(wn, hn, get(w,h));
99 set(w,h,PF::CELL_EMPTY);
100 emit cellMoved(w,h,wn,hn);
103 bool PlayField::checkGlobalFall()
110 for(int w=0;w<PF::FIELD_WIDTH;w++)
111 for(int h=0;h<PF::FIELD_HEIGHT;h++)
114 if(moveBlock(w,h,0,1))
124 bool PlayField::checkFall(int w, int h)
127 return (cell!=PF::CELL_EMPTY) &&
128 (cell!=PF::CELL_WALL) &&
129 (h!=(PF::FIELD_HEIGHT-1)) &&
130 (get(w,h+1)==PF::CELL_EMPTY);
132 bool PlayField::checkTouch()
134 int toHide[PF::FIELD_WIDTH][PF::FIELD_HEIGHT];
136 for (int w = 0; w < PF::FIELD_WIDTH; w++) {
137 for (int h = 0; h < PF::FIELD_HEIGHT; h++) {
138 toHide[w][h] = PF::CELL_EMPTY;
139 if ((get(w,h) != PF::CELL_EMPTY) && (get(w,h)!=PF::CELL_WALL)) {
141 toHide[w][h] |= (get(w,h-1) == get(w,h));
143 if (h != PF::FIELD_HEIGHT - 1) {
144 toHide[w][h] |= (get(w,h+1) == get(w,h));
147 toHide[w][h] |= (get(w-1,h) == get(w,h));
149 if (w != PF::FIELD_WIDTH - 1) {
150 toHide[w][h] |= (get(w+1,h) == get(w,h));
157 for (int w = 0; w < PF::FIELD_WIDTH; w++) {
158 for (int h = 0; h < PF::FIELD_HEIGHT; h++) {
161 set(w,h,PF::CELL_EMPTY);
170 bool PlayField::checkSolved()
172 for(int w=0;w<PF::FIELD_WIDTH-1;w++)
173 for(int h=0;h<PF::FIELD_HEIGHT-1;h++)
176 if((cell!=PF::CELL_EMPTY) && (cell!=PF::CELL_WALL)) return false;
181 void PlayField::undo()
186 currentUndo=(currentUndo - 1 + PF::MAX_UNDO) % PF::MAX_UNDO;
187 copy(undos[currentUndo],field);
188 moves=undoMade[currentUndo];