summaryrefslogtreecommitdiff
path: root/noncore/games/fifteen
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (side-by-side diff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/fifteen
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'noncore/games/fifteen') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/fifteen/.cvsignore2
-rw-r--r--noncore/games/fifteen/Makefile.in118
-rw-r--r--noncore/games/fifteen/fifteen.cpp364
-rw-r--r--noncore/games/fifteen/fifteen.h83
-rw-r--r--noncore/games/fifteen/fifteen.pro10
-rw-r--r--noncore/games/fifteen/main.cpp33
-rw-r--r--noncore/games/fifteen/qpe-fifteen.control11
7 files changed, 621 insertions, 0 deletions
diff --git a/noncore/games/fifteen/.cvsignore b/noncore/games/fifteen/.cvsignore
new file mode 100644
index 0000000..6fe2396
--- a/dev/null
+++ b/noncore/games/fifteen/.cvsignore
@@ -0,0 +1,2 @@
+moc_*
+Makefile
diff --git a/noncore/games/fifteen/Makefile.in b/noncore/games/fifteen/Makefile.in
new file mode 100644
index 0000000..23c7334
--- a/dev/null
+++ b/noncore/games/fifteen/Makefile.in
@@ -0,0 +1,118 @@
+#############################################################################
+
+####### Compiler, tools and options
+
+CXX = $(SYSCONF_CXX) $(QT_CXX_MT)
+CXXFLAGS= $(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS)
+CC = $(SYSCONF_CC) $(QT_C_MT)
+CFLAGS = $(SYSCONF_CFLAGS)
+INCPATH = -I$(QPEDIR)/include
+LFLAGS = $(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT)
+LIBS = $(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP)
+MOC = $(SYSCONF_MOC)
+UIC = $(SYSCONF_UIC)
+
+####### Target
+
+DESTDIR = ../bin/
+VER_MAJ = 1
+VER_MIN = 0
+VER_PATCH = 0
+TARGET = fifteen
+TARGET1 = lib$(TARGET).so.$(VER_MAJ)
+
+####### Files
+
+HEADERS = fifteen.h
+SOURCES = fifteen.cpp \
+ main.cpp
+OBJECTS = fifteen.o \
+ main.o
+INTERFACES =
+UICDECLS =
+UICIMPLS =
+SRCMOC = moc_fifteen.cpp
+OBJMOC = moc_fifteen.o
+
+
+####### Implicit rules
+
+.SUFFIXES: .cpp .cxx .cc .C .c
+
+.cpp.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.cxx.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.cc.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.C.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.c.o:
+ $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
+
+####### Build rules
+
+
+all: $(DESTDIR)$(TARGET)
+
+$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS)
+ $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
+
+moc: $(SRCMOC)
+
+tmake:
+ tmake fifteen.pro
+
+clean:
+ -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS)
+ -rm -f *~ core
+ -rm -f allmoc.cpp
+
+####### Extension Modules
+
+listpromodules:
+ @echo
+
+listallmodules:
+ @echo
+
+listaddonpromodules:
+ @echo
+
+listaddonentmodules:
+ @echo
+
+
+REQUIRES=
+
+####### Sub-libraries
+
+
+###### Combined headers
+
+
+
+####### Compile
+
+fifteen.o: fifteen.cpp \
+ fifteen.h \
+ $(QPEDIR)/include/qpe/resource.h \
+ $(QPEDIR)/include/qpe/config.h \
+ $(QPEDIR)/include/qpe/qpetoolbar.h \
+ $(QPEDIR)/include/qpe/qpemenubar.h
+
+main.o: main.cpp \
+ fifteen.h \
+ $(QPEDIR)/include/qpe/qpeapplication.h
+
+moc_fifteen.o: moc_fifteen.cpp \
+ fifteen.h
+
+moc_fifteen.cpp: fifteen.h
+ $(MOC) fifteen.h -o moc_fifteen.cpp
+
+
diff --git a/noncore/games/fifteen/fifteen.cpp b/noncore/games/fifteen/fifteen.cpp
new file mode 100644
index 0000000..293cd65
--- a/dev/null
+++ b/noncore/games/fifteen/fifteen.cpp
@@ -0,0 +1,364 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+#include "fifteen.h"
+
+#include <qpe/resource.h>
+#include <qpe/config.h>
+
+#include <qvbox.h>
+#include <qaction.h>
+#include <qlayout.h>
+#include <qpainter.h>
+#include <qpopupmenu.h>
+#include <qmessagebox.h>
+#include <qpe/qpetoolbar.h>
+#include <qpe/qpemenubar.h>
+#include <qstringlist.h>
+#include <qapplication.h>
+
+#include <stdlib.h>
+#include <time.h>
+
+FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name)
+ : QMainWindow( parent, name )
+{
+ // random seed
+ srand(time(0));
+
+ setToolBarsMovable( FALSE );
+ QVBox *vbox = new QVBox( this );
+ PiecesTable *table = new PiecesTable( vbox );
+ setCentralWidget(vbox);
+
+ QPEToolBar *toolbar = new QPEToolBar(this);
+ toolbar->setHorizontalStretchable( TRUE );
+ addToolBar(toolbar);
+
+ QPEMenuBar *menubar = new QPEMenuBar( toolbar );
+ menubar->setMargin(0);
+
+ QPopupMenu *game = new QPopupMenu( this );
+
+ QWidget *spacer = new QWidget( toolbar );
+ spacer->setBackgroundMode( PaletteButton );
+ toolbar->setStretchableWidget( spacer );
+
+ QAction *a = new QAction( tr( "Randomize" ), Resource::loadPixmap( "new" ),
+ QString::null, 0, this, 0 );
+ connect( a, SIGNAL( activated() ), table, SLOT( slotRandomize() ) );
+ a->addTo( game );
+ a->addTo( toolbar );
+
+ a = new QAction( tr( "Solve" ), Resource::loadPixmap( "repeat" ),
+ QString::null, 0, this, 0 );
+ connect( a, SIGNAL( activated() ), table, SLOT( slotReset() ) );
+ a->addTo( game );
+ a->addTo( toolbar );
+
+ menubar->insertItem( tr( "Game" ), game );
+}
+
+PiecesTable::PiecesTable(QWidget* parent, const char* name )
+ : QTableView(parent, name), _menu(0), _randomized(false)
+{
+ // setup table view
+ setFrameStyle(StyledPanel | Sunken);
+ setBackgroundMode(NoBackground);
+ setMouseTracking(true);
+
+ setNumRows(4);
+ setNumCols(4);
+
+ // init arrays
+ initMap();
+ readConfig();
+ initColors();
+
+ // set font
+ QFont f = font();
+ f.setPixelSize(18);
+ f.setBold( TRUE );
+ setFont(f);
+}
+
+PiecesTable::~PiecesTable()
+{
+ writeConfig();
+}
+
+void PiecesTable::writeConfig()
+{
+ Config cfg("Fifteen");
+ cfg.setGroup("Game");
+ QStringList map;
+ for (unsigned int i = 0; i < 16; i++)
+ map.append( QString::number( _map[i] ) );
+ cfg.writeEntry("Map", map, '-');
+ cfg.writeEntry("Randomized", _randomized );
+}
+
+void PiecesTable::readConfig()
+{
+ Config cfg("Fifteen");
+ cfg.setGroup("Game");
+ QStringList map = cfg.readListEntry("Map", '-');
+ _randomized = cfg.readBoolEntry( "Randomized", FALSE );
+ unsigned int i = 0;
+ for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) {
+ _map[i] = (*it).toInt();
+ i++;
+ if ( i > 15 ) break;
+ }
+}
+
+void PiecesTable::paintCell(QPainter *p, int row, int col)
+{
+ int w = cellWidth();
+ int h = cellHeight();
+ int x2 = w - 1;
+ int y2 = h - 1;
+
+ int number = _map[col + row * numCols()] + 1;
+
+ // draw cell background
+ if(number == 16)
+ p->setBrush(colorGroup().background());
+ else
+ p->setBrush(_colors[number-1]);
+ p->setPen(NoPen);
+ p->drawRect(0, 0, w, h);
+
+ // draw borders
+ if (height() > 40) {
+ p->setPen(colorGroup().text());
+ if(col < numCols()-1)
+ p->drawLine(x2, 0, x2, y2); // right border line
+
+ if(row < numRows()-1)
+ p->drawLine(0, y2, x2, y2); // bottom boder line
+ }
+
+ // draw number
+ if (number == 16) return;
+ p->setPen(black);
+ p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(number));
+}
+
+void PiecesTable::resizeEvent(QResizeEvent *e)
+{
+ QTableView::resizeEvent(e);
+
+ setCellWidth(contentsRect().width()/ numRows());
+ setCellHeight(contentsRect().height() / numCols());
+}
+
+void PiecesTable::initColors()
+{
+ _colors.resize(numRows() * numCols());
+ for (int r = 0; r < numRows(); r++)
+ for (int c = 0; c < numCols(); c++)
+ _colors[c + r *numCols()] = QColor(255 - 70 * c,255 - 70 * r, 150);
+}
+
+void PiecesTable::initMap()
+{
+ _map.resize(16);
+ for (unsigned int i = 0; i < 16; i++)
+ _map[i] = i;
+
+ _randomized = false;
+}
+
+void PiecesTable::randomizeMap()
+{
+ initMap();
+ _randomized = true;
+ // find the free position
+ int pos = _map.find(15);
+
+ int move = 0;
+ while ( move < 333 ) {
+
+ int frow = pos / numCols();
+ int fcol = pos - frow * numCols();
+
+ // find click position
+ int row = rand()%4;
+ int col = rand()%4;
+
+ // sanity check
+ if ( row < 0 || row >= numRows() ) continue;
+ if ( col < 0 || col >= numCols() ) continue;
+ if ( row != frow && col != fcol ) continue;
+
+ move++;
+
+ // rows match -> shift pieces
+ if(row == frow) {
+
+ if (col < fcol) {
+ for(int c = fcol; c > col; c--) {
+ _map[c + row * numCols()] = _map[ c-1 + row *numCols()];
+ }
+ }
+ else if (col > fcol) {
+ for(int c = fcol; c < col; c++) {
+ _map[c + row * numCols()] = _map[ c+1 + row *numCols()];
+ }
+ }
+ }
+ // cols match -> shift pieces
+ else if (col == fcol) {
+
+ if (row < frow) {
+ for(int r = frow; r > row; r--) {
+ _map[col + r * numCols()] = _map[ col + (r-1) *numCols()];
+ }
+ }
+ else if (row > frow) {
+ for(int r = frow; r < row; r++) {
+ _map[col + r * numCols()] = _map[ col + (r+1) *numCols()];
+ }
+ }
+ }
+ // move free cell to click position
+ _map[pos=(col + row * numCols())] = 15;
+ repaint();
+ }
+}
+
+void PiecesTable::checkwin()
+{
+ if(!_randomized) return;
+
+ int i;
+ for (i = 0; i < 16; i++)
+ if(i != _map[i])
+ break;
+
+ if (i == 16) {
+ QMessageBox::information(this, tr("Fifteen Pieces"),
+ tr("Congratulations!\nYou win the game!"));
+ _randomized = FALSE;
+ }
+
+}
+
+void PiecesTable::slotRandomize()
+{
+ randomizeMap();
+}
+
+void PiecesTable::slotReset()
+{
+ initMap();
+ repaint();
+}
+
+void PiecesTable::mousePressEvent(QMouseEvent* e)
+{
+ QTableView::mousePressEvent(e);
+
+ if (e->button() == RightButton) {
+
+ // setup RMB pupup menu
+ if(!_menu) {
+ _menu = new QPopupMenu(this);
+ _menu->insertItem(tr("R&andomize Pieces"), mRandomize);
+ _menu->insertItem(tr("&Reset Pieces"), mReset);
+ _menu->adjustSize();
+ }
+
+ // execute RMB popup and check result
+ switch(_menu->exec(mapToGlobal(e->pos()))) {
+ case mRandomize:
+ randomizeMap();
+ break;
+ case mReset:
+ initMap();
+ repaint();
+ break;
+ default:
+ break;
+ }
+ }
+ else {
+ // GAME LOGIC
+
+ // find the free position
+ int pos = _map.find(15);
+ if(pos < 0) return;
+
+ int frow = pos / numCols();
+ int fcol = pos - frow * numCols();
+
+ // find click position
+ int row = findRow(e->y());
+ int col = findCol(e->x());
+
+ // sanity check
+ if (row < 0 || row >= numRows()) return;
+ if (col < 0 || col >= numCols()) return;
+ if ( row != frow && col != fcol ) return;
+
+ // valid move?
+ if(row != frow && col != fcol) return;
+
+ // rows match -> shift pieces
+ if(row == frow) {
+
+ if (col < fcol) {
+ for(int c = fcol; c > col; c--) {
+ _map[c + row * numCols()] = _map[ c-1 + row *numCols()];
+ updateCell(row, c, false);
+ }
+ }
+ else if (col > fcol) {
+ for(int c = fcol; c < col; c++) {
+ _map[c + row * numCols()] = _map[ c+1 + row *numCols()];
+ updateCell(row, c, false);
+ }
+ }
+ }
+ // cols match -> shift pieces
+ else if (col == fcol) {
+
+ if (row < frow) {
+ for(int r = frow; r > row; r--) {
+ _map[col + r * numCols()] = _map[ col + (r-1) *numCols()];
+ updateCell(r, col, false);
+ }
+ }
+ else if (row > frow) {
+ for(int r = frow; r < row; r++) {
+ _map[col + r * numCols()] = _map[ col + (r+1) *numCols()];
+ updateCell(r, col, false);
+ }
+ }
+ }
+ // move free cell to click position
+ _map[col + row * numCols()] = 15;
+ updateCell(row, col, false);
+
+ // check if the player wins with this move
+ checkwin();
+ }
+}
diff --git a/noncore/games/fifteen/fifteen.h b/noncore/games/fifteen/fifteen.h
new file mode 100644
index 0000000..703a8a7
--- a/dev/null
+++ b/noncore/games/fifteen/fifteen.h
@@ -0,0 +1,83 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+#ifndef __fifteenapplet_h__
+#define __fifteenapplet_h__
+
+#include <qmainwindow.h>
+#include <qtableview.h>
+#include <qarray.h>
+
+class QPopupMenu;
+
+class PiecesTable : public QTableView
+{
+ Q_OBJECT
+
+ public:
+ PiecesTable(QWidget* parent = 0, const char* name = 0);
+ ~PiecesTable();
+
+ protected slots:
+ void slotRandomize();
+ void slotReset();
+
+ protected:
+ void resizeEvent(QResizeEvent*);
+ void mousePressEvent(QMouseEvent*);
+
+ void paintCell(QPainter *, int row, int col);
+
+ void initMap();
+ void initColors();
+ void randomizeMap();
+ void checkwin();
+ void readConfig();
+ void writeConfig();
+
+ private:
+ QArray<int> _map;
+ QArray<QColor> _colors;
+ QPopupMenu *_menu;
+ bool _randomized;
+
+ enum MenuOp { mRandomize = 1, mReset = 2 };
+};
+
+class FifteenWidget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ FifteenWidget(QWidget *parent = 0, const char *name = 0);
+
+private:
+ PiecesTable *_table;
+};
+
+class FifteenMainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ FifteenMainWindow(QWidget *parent=0, const char* name=0);
+};
+
+#endif
diff --git a/noncore/games/fifteen/fifteen.pro b/noncore/games/fifteen/fifteen.pro
new file mode 100644
index 0000000..167f4f8
--- a/dev/null
+++ b/noncore/games/fifteen/fifteen.pro
@@ -0,0 +1,10 @@
+DESTDIR = ../bin
+TEMPLATE = app
+CONFIG = qt warn_on release
+HEADERS = fifteen.h
+SOURCES = fifteen.cpp \
+ main.cpp
+INCLUDEPATH += $(QPEDIR)/include
+DEPENDPATH += $(QPEDIR)/include
+LIBS += -lqpe
+TARGET = fifteen
diff --git a/noncore/games/fifteen/main.cpp b/noncore/games/fifteen/main.cpp
new file mode 100644
index 0000000..4838a36
--- a/dev/null
+++ b/noncore/games/fifteen/main.cpp
@@ -0,0 +1,33 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+#include "fifteen.h"
+
+#include <qpe/qpeapplication.h>
+
+int main( int argc, char ** argv)
+{
+ QPEApplication app( argc, argv );
+
+ FifteenMainWindow mw;
+ mw.setCaption( FifteenMainWindow::tr("Fifteen Pieces") );
+ app.showMainWidget( &mw );
+ return app.exec();
+}
diff --git a/noncore/games/fifteen/qpe-fifteen.control b/noncore/games/fifteen/qpe-fifteen.control
new file mode 100644
index 0000000..d77eb32
--- a/dev/null
+++ b/noncore/games/fifteen/qpe-fifteen.control
@@ -0,0 +1,11 @@
+Files: bin/fifteen apps/Games/fifteen.desktop pics/Fifteen.png
+Priority: optional
+Section: qpe/games
+Maintainer: Warwick Allison <warwick@trolltech.com>
+Architecture: arm
+Arch: iPAQ
+Version: $QPE_VERSION-4
+Depends: qpe-base ($QPE_VERSION)
+Description: Fifteen pieces game
+ A game for the Qtopia environment
+.