-rw-r--r-- | noncore/games/zsame/StoneField.cpp | 4 | ||||
-rw-r--r-- | noncore/games/zsame/StoneField.h | 3 | ||||
-rw-r--r-- | noncore/games/zsame/StoneWidget.cpp | 4 | ||||
-rw-r--r-- | noncore/games/zsame/StoneWidget.h | 1 | ||||
-rw-r--r-- | noncore/games/zsame/ZSameWidget.cpp | 1 |
5 files changed, 12 insertions, 1 deletions
diff --git a/noncore/games/zsame/StoneField.cpp b/noncore/games/zsame/StoneField.cpp index 49d8eca..56e9dc5 100644 --- a/noncore/games/zsame/StoneField.cpp +++ b/noncore/games/zsame/StoneField.cpp @@ -259,132 +259,136 @@ StoneField::remove(int x,int y,bool force) { 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 { register int i=maxstone-1;; register 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; } +void StoneField::clearBonus() { + m_gotBonus = false; +} + 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; } diff --git a/noncore/games/zsame/StoneField.h b/noncore/games/zsame/StoneField.h index 80be73a..d32d78d 100644 --- a/noncore/games/zsame/StoneField.h +++ b/noncore/games/zsame/StoneField.h @@ -1,113 +1,114 @@ /* Yo Emacs, this is -*- C++ -*- */ /* * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #ifndef _STONEFIELD #define _STONEFIELD #include <krandomsequence.h> #include <qlist.h> struct Stone { unsigned char color; bool changed; bool marked; }; class StoneField; class StoneWidget; class StoneFieldState { private: unsigned char *field; int colors; unsigned int board; unsigned int score; int gameover; public: StoneFieldState(const StoneField &stonefield); ~StoneFieldState(); void restore(StoneField &stonefield) const; }; class StoneField { friend class StoneFieldState; friend class StoneWidget; private: int sizex; int sizey; int maxstone; struct Stone *field; int colors; unsigned int board; unsigned int score; mutable int gameover; bool m_gotBonus; int marked; KRandomSequence random; QList<StoneFieldState> *undolist; public: StoneField(int width=15,int height=10, int colors=3,unsigned int board=0, bool undoenabled=true); ~StoneField(); int width() const; int height() const; void newGame(unsigned int board,int colors); void reset(); int mark(int x,int y,bool force=false); void unmark(); int remove(int x,int y,bool force=false); int undo(int count=1); - bool isGameover() const; + bool isGameover() const; bool gotBonus() const; + void clearBonus(); bool undoPossible() const; int getBoard() const; int getScore() const; int getColors() const; int getMarked() const; protected: int getFieldSize() const; struct Stone *getField() const; int map(int x,int y); void mark(int index,unsigned char color); }; #endif diff --git a/noncore/games/zsame/StoneWidget.cpp b/noncore/games/zsame/StoneWidget.cpp index 646fc9c..5dd0252 100644 --- a/noncore/games/zsame/StoneWidget.cpp +++ b/noncore/games/zsame/StoneWidget.cpp @@ -92,192 +92,196 @@ StoneWidget::~StoneWidget() { delete [] map[c]; } delete [] map; setMouseTracking(false); killTimers(); } unsigned int StoneWidget::board() { return stonefield.getBoard(); } int StoneWidget::score() { return stonefield.getScore(); } int StoneWidget::marked() { return stonefield.getMarked(); } QSize StoneWidget::size() { return QSize(sizex,sizey); } int StoneWidget::colors() { return stonefield.getColors(); } QSize StoneWidget::sizeHint () const { return QSize(field_width,field_height); } void StoneWidget::newGame(unsigned int board,int colors) { stonefield.newGame(board,colors); history.clear(); modified= false; emit s_newgame(); emit s_colors(stonefield.getColors()); emit s_board(stonefield.getBoard()); } void StoneWidget::reset() { stonefield.reset(); history.clear(); emit s_newgame(); } void StoneWidget::unmark() { stonefield.unmark(); emit s_marked(0); } bool StoneWidget::undoPossible() const { if (stonefield.isGameover()) return false; return stonefield.undoPossible(); } int StoneWidget::undo(int count) { if (stonefield.isGameover()) return 0; int ret_val=stonefield.undo(count); QPoint p=mapFromGlobal(cursor().pos()); int x=p.x(); int y=p.y(); if (x<0||y<0||x>=field_width||y>=field_height) { emit s_score(stonefield.getMarked()); return ret_val; } int marked=stonefield.mark(x/stone_width,y/stone_height); emit s_marked(marked); slice=0; emit s_score(stonefield.getScore()); modified= (stonefield.getScore()>0); return ret_val; } bool StoneWidget::isGameover() { return stonefield.isGameover(); } bool StoneWidget::hasBonus() { return stonefield.gotBonus(); // don't ask me why the names differ... ;-| [hlm] } +void StoneWidget::clearBonus() { + stonefield.clearBonus(); +} + bool StoneWidget::isOriginalBoard() { return !modified; } void StoneWidget::readProperties(Config *) { /* Q_ASSERT(conf); history.clear(); if (!conf->hasKey("Board")|| !conf->hasKey("Colors")|| !conf->hasKey("Stones")) { return; } newGame(conf->readNumEntry("Board"),conf->readNumEntry("Colors")); QStrList list; conf->readListEntry("Stones",list); for (const char *item=list.first();item;item=list.next()) { int x=-1,y=-1; if (sscanf(item,"%02X%02X",&x,&y)!=2) break; history.append(new QPoint(x,y)); stonefield.remove(x,y); } */ } void StoneWidget::saveProperties(Config *) { /* Q_ASSERT(conf); QStrList list(true); QString tmp; for (QPoint *item=history.first();item;item=history.next()) { tmp.sprintf("%02X%02X",item->x(),item->y()); list.append(tmp.ascii()); } conf->writeEntry("Stones",list); conf->writeEntry("Board",stonefield.getBoard()); conf->writeEntry("Colors",stonefield.getColors()); */ } void StoneWidget::timerEvent( QTimerEvent * ) { QPoint p=mapFromGlobal(cursor().pos()); int x=p.x(); int y=p.y(); if (x<0||y<0||x>=field_width||y>=field_height) stonefield.unmark(); slice=(slice+1)%maxslices; paintEvent(0); } void StoneWidget::paintEvent( QPaintEvent *e ) { Stone *stone=stonefield.getField(); for (int y=0;y<sizey;y++) { int cy = y * stone_height; for (int x=0;x<sizex;x++) { int cx = stone_width * x; bool redraw=stone->marked||stone->changed; if (!redraw&&e) { QRect r(cx,cy,stone_width,stone_height); redraw=r.intersects(e->rect()); } if (redraw) { stone->changed=false; if (stone->color) { int tslice = stone->marked?slice:0; bitBlt(this,cx,cy, &map[stone->color-1][tslice].stone, 0, 0, stone_width,stone_height,CopyROP,FALSE); } else { erase(cx, cy, stone_width, stone_height); } } stone++; // naechster Stein. } } } void diff --git a/noncore/games/zsame/StoneWidget.h b/noncore/games/zsame/StoneWidget.h index 9cd7e10..89a8afc 100644 --- a/noncore/games/zsame/StoneWidget.h +++ b/noncore/games/zsame/StoneWidget.h @@ -1,115 +1,116 @@ /* Yo Emacs, this is -*- C++ -*- */ /* * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #ifndef _STONEWIDGET #define _STONEWIDGET #include <qpixmap.h> #include <qwidget.h> #include <qpe/config.h> #include "StoneField.h" struct StoneSlice; class StoneWidget : public QWidget { Q_OBJECT int modified; // int marked; // # of marked stones int stones_x, stones_y; int sizex, sizey; int field_width, field_height; QList<QPoint> history; StoneField stonefield; // picture number of stonemovie int slice; StoneSlice **map; public: StoneWidget( QWidget *parent=0, int x=10,int y=10); ~StoneWidget(); unsigned int board(); int score(); int marked(); QSize size(); int colors(); virtual QSize sizeHint() const; bool undoPossible() const; void newGame(unsigned int board, int colors); void reset(); void unmark(); int undo(int count=1); // test for game end bool isGameover(); // if isGameover(): finished with bonus? bool hasBonus(); + void clearBonus(); // test for unchanged start position bool isOriginalBoard(); virtual void readProperties(Config *conf); virtual void saveProperties(Config *conf); protected: void timerEvent( QTimerEvent *e ); void paintEvent( QPaintEvent *e ); void mousePressEvent ( QMouseEvent *e); void myMoveEvent ( QMouseEvent *e); // properties of the stone picture int stone_width,stone_height; // size of one stone int maxcolors; // number of different stones (Y direction) int maxslices; // number of pictures per movie (X direction) signals: // A new game begins void s_newgame(); void s_colors(int colors); void s_board(int board); void s_score(int score); void s_marked(int m); void s_gameover(); // The stone (x,y) was clicked(removed), // all neighbor stones disappear without further signals void s_remove(int x,int y); void s_sizechanged(); }; #endif diff --git a/noncore/games/zsame/ZSameWidget.cpp b/noncore/games/zsame/ZSameWidget.cpp index 04ad825..dee4a1c 100644 --- a/noncore/games/zsame/ZSameWidget.cpp +++ b/noncore/games/zsame/ZSameWidget.cpp @@ -125,120 +125,121 @@ void ZSameWidget::saveProperties(Config *) { } void ZSameWidget::sizeChanged() { // stone->setFixedSize(stone->sizeHint()); } void ZSameWidget::newGame(unsigned int board,int colors) { while (board>=1000000) board-=1000000; // kdDebug() << "newgame board " << board << " colors " << colors << endl; stone->newGame(board,colors); setScore(0); } bool ZSameWidget::confirmAbort() { return stone->isGameover() || stone->isOriginalBoard() || (QMessageBox::warning(this, i18n("Resign"), i18n("<qt>Do you want to resign?</qt>"), QMessageBox::Yes, QMessageBox::No|QMessageBox::Default|QMessageBox::Escape, 0) == QMessageBox::Yes ); } void ZSameWidget::m_new() { if (confirmAbort()) newGame(_random(),default_colors); } void ZSameWidget::m_restart() { if (confirmAbort()) newGame(stone->board(),default_colors); } void ZSameWidget::m_load() { // kdDebug() << "menu load not supported" << endl; } void ZSameWidget::m_save() { // kdDebug() << "menu save not supported" << endl; } void ZSameWidget::m_undo() { // Q_ASSERT(stone); stone->undo(); } void ZSameWidget::m_showhs() { /* Q_ASSERT(stone); stone->unmark(); KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this); d.addField(Board, i18n("Board"), "Board"); d.exec(); */ } void ZSameWidget::m_quit() { // Q_ASSERT(stone); stone->unmark(); qApp->quit(); // delete this; } void ZSameWidget::m_tglboard() { // kdDebug() << "toggled" << endl; } void ZSameWidget::setColors(int ) { // status->changeItem(i18n("%1 Colors").arg(colors),1); } void ZSameWidget::setBoard(int ) { // status->changeItem(i18n("Board: %1").arg(board, 6), 2); } void ZSameWidget::setMarked(int ) { // status->changeItem(i18n("Marked: %1").arg(m, 6),3); } void ZSameWidget::stonesRemoved(int,int) { // KNotifyClient::event("stones removed", // i18n("%1 stones removed.").arg(stone->marked())); } void ZSameWidget::setScore(int ) { // status->changeItem(i18n("Score: %1").arg(score, 6),4); // undo->setEnabled(stone->undoPossible()); // restart->setEnabled(!stone->isOriginalBoard()); } void ZSameWidget::gameover() { // kdDebug() << "GameOver" << endl; if (stone->hasBonus()) { QMessageBox::information(this,i18n("Game won"), i18n("<qt>You even removed the last stone, great job! " "This gave you a score of %1 in total.</qt>").arg(stone->score())); + stone->clearBonus(); } else { QMessageBox::information(this,i18n("Game over"), i18n("<qt>There are no more removeable stones. " "You got a score of %1 in total.</qt>").arg(stone->score())); } stone->unmark(); } void ZSameWidget::desktop_widget(int *f)const{ QWidget* wid = QApplication::desktop(); /* width > height landscape mode */ if ( wid->width() > wid->height() ) { f[0]=15; f[1]=9; } /* normal */ else{ f[0]=12; f[1]=13; } } |