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 && (w!=w_new) && ((h_below >= PF::FIELD_HEIGHT) || get(w,h_below)!=PF::CELL_EMPTY));
73 cont|=checkGlobalFall();
78 if(totalUndo<PF::MAX_UNDO) totalUndo++;
79 copy(temp,undos[currentUndo]);
80 undoMade[currentUndo]=movesTemp;
81 currentUndo=(currentUndo + 1) % PF::MAX_UNDO;
85 bool PlayField::moveBlock(int w, int h, int dw, int dh)
90 if(wn<0 || wn >= PF::FIELD_WIDTH || hn < 0 || hn >= PF::FIELD_HEIGHT)
93 if(get(wn,hn)!=PF::CELL_EMPTY)
95 set(wn, hn, get(w,h));
96 set(w,h,PF::CELL_EMPTY);
97 emit cellMoved(w,h,wn,hn);
100 bool PlayField::checkGlobalFall()
107 for(int w=0;w<PF::FIELD_WIDTH;w++)
108 for(int h=0;h<PF::FIELD_HEIGHT;h++)
111 if(moveBlock(w,h,0,1))
121 bool PlayField::checkFall(int w, int h)
124 return (cell!=PF::CELL_EMPTY) &&
125 (cell!=PF::CELL_WALL) &&
126 (h!=(PF::FIELD_HEIGHT-1)) &&
127 (get(w,h+1)==PF::CELL_EMPTY);
129 bool PlayField::checkTouch()
131 int toHide[PF::FIELD_WIDTH][PF::FIELD_HEIGHT];
133 for (int w = 0; w < PF::FIELD_WIDTH; w++) {
134 for (int h = 0; h < PF::FIELD_HEIGHT; h++) {
135 toHide[w][h] = PF::CELL_EMPTY;
136 if ((get(w,h) != PF::CELL_EMPTY) && (get(w,h)!=PF::CELL_WALL)) {
138 toHide[w][h] |= (get(w,h-1) == get(w,h));
140 if (h != PF::FIELD_HEIGHT - 1) {
141 toHide[w][h] |= (get(w,h+1) == get(w,h));
144 toHide[w][h] |= (get(w-1,h) == get(w,h));
146 if (w != PF::FIELD_WIDTH - 1) {
147 toHide[w][h] |= (get(w+1,h) == get(w,h));
154 for (int w = 0; w < PF::FIELD_WIDTH; w++) {
155 for (int h = 0; h < PF::FIELD_HEIGHT; h++) {
158 set(w,h,PF::CELL_EMPTY);
167 bool PlayField::checkSolved()
169 for(int w=0;w<PF::FIELD_WIDTH-1;w++)
170 for(int h=0;h<PF::FIELD_HEIGHT-1;h++)
173 if((cell!=PF::CELL_EMPTY) && (cell!=PF::CELL_WALL)) return false;
178 void PlayField::undo()
183 currentUndo=(currentUndo - 1 + PF::MAX_UNDO) % PF::MAX_UNDO;
184 copy(undos[currentUndo],field);
185 moves=undoMade[currentUndo];