summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/tetrix/ohighscoredlg.cpp199
-rw-r--r--noncore/games/tetrix/ohighscoredlg.h97
-rw-r--r--noncore/games/tetrix/qtetrix.cpp9
-rw-r--r--noncore/games/tetrix/qtetrix.h2
-rw-r--r--noncore/games/tetrix/tetrix.pro6
5 files changed, 308 insertions, 5 deletions
diff --git a/noncore/games/tetrix/ohighscoredlg.cpp b/noncore/games/tetrix/ohighscoredlg.cpp
new file mode 100644
index 0000000..cf0dcf7
--- a/dev/null
+++ b/noncore/games/tetrix/ohighscoredlg.cpp
@@ -0,0 +1,199 @@
+/***************************************************************************
+ begin : January 2003
+ copyright : ( C ) 2003 by Carsten Niehaus
+ email : cniehaus@handhelds.org
+ **************************************************************************/
+
+/***************************************************************************
+ * *
+ * 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. *
+ * *
+ **************************************************************************/
+
+#include <qdialog.h>
+#include <qpe/config.h>
+#include <qlayout.h>
+#include <qpe/config.h>
+
+#include <qstring.h>
+#include <qhbox.h>
+#include <qvbox.h>
+#include <qlabel.h>
+#include <qlistview.h>
+#include <qlineedit.h>
+
+#include "ohighscoredlg.h"
+
+OHighscore::OHighscore( int score , int playerLevel )
+{
+ pLevel = playerLevel;
+ getList();
+ checkIfItIsANewhighscore( score );
+}
+
+OHighscore::~OHighscore()
+{
+ std::list<t_playerData*>::iterator deleteIterator = playerData.begin();
+ for ( ; deleteIterator != playerData.end() ; deleteIterator++ )
+ {
+ delete ( *deleteIterator );
+ }
+}
+
+void OHighscore::getList()
+{
+ Config cfg ( "tetrix" );
+ cfg.setGroup( QString::number( 1 ) );
+ lowest = cfg.readNumEntry( "Points" );
+ playerData.clear();
+
+ int rest = 1; //for the filling up later
+
+ for ( int i = 1 ; i < 11 ; i++ )
+ {
+ if ( cfg.hasGroup( QString::number( i ) ) )
+ {
+ cfg.setGroup( QString::number( i ) );
+ int temp = cfg.readNumEntry( "Points" );
+
+ t_playerData *pPlayerData = new t_playerData;
+ pPlayerData->sName = cfg.readEntry( "Name" );
+ pPlayerData->points = temp;
+ pPlayerData->level = cfg.readNumEntry( "Level" );
+
+ playerData.push_back( pPlayerData );
+
+ if ( (temp < lowest) ) lowest = temp;
+ rest++;
+ }
+ }
+
+ //now I fill up the rest of the list
+ if ( rest < 11 ) //only go in this loop if there are less than
+ //10 highscoreentries
+ {
+ lowest = 0;
+ for ( ; rest < 11 ; rest++ )
+ {
+ t_playerData *pPlayerData = new t_playerData;
+ pPlayerData->sName = tr( "empty");
+ pPlayerData->points = 0;
+ pPlayerData->level = 0;
+
+ playerData.push_back( pPlayerData );
+ }
+ }
+
+}
+
+void OHighscore::checkIfItIsANewhighscore( int points)
+{
+ if ( points > lowest )
+ isNewhighscore = true;
+ else
+ isNewhighscore = false;
+}
+
+void OHighscore::insertData( QString name , int punkte , int playerLevel )
+{
+ Config cfg ( "tetrix" );
+ int entryNumber = 1;
+ std::list<t_playerData*>::iterator insertIterator = playerData.begin();
+ while ( insertIterator != playerData.end() )
+ {
+ if ( punkte > ( *insertIterator )->points )
+ {
+ t_playerData* temp = new t_playerData;
+ temp->sName = name;
+ temp->points = punkte;
+ temp->level = playerLevel;
+ playerData.insert( insertIterator , temp );
+
+ //now we have to delete the last entry
+ insertIterator = playerData.end();
+ insertIterator--;
+//X delete *insertIterator; //memleak?
+ playerData.erase( insertIterator );
+
+ /////////////////////////////////////////
+ //this block just rewrites the highscore
+ insertIterator = playerData.begin();
+ while ( insertIterator != playerData.end() )
+ {
+ cfg.setGroup( QString::number( entryNumber ) );
+ cfg.writeEntry( "Name" , ( *insertIterator )->sName );
+ cfg.writeEntry( "Points" , ( *insertIterator )->points );
+ cfg.writeEntry( "Level" , ( *insertIterator )->level );
+ entryNumber++;
+ insertIterator++;
+ }
+ ////////////////////////////////////////
+
+ return;
+ }
+ insertIterator++;
+ }
+}
+
+QString OHighscore::getName()
+{
+ QString name;
+ QDialog *d = new QDialog ( this, 0, true );
+ d->setCaption( tr( "Enter your name!" ));
+ QLineEdit *ed = new QLineEdit ( d );
+ ( new QVBoxLayout ( d, 3, 3 ))->addWidget ( ed );
+ ed->setFocus ( );
+
+ if ( d->exec() == QDialog::Accepted ) {
+ name = ed->text();
+ }
+ //delete d;
+ return name;
+}
+
+OHighscoreDialog::OHighscoreDialog(OHighscore *highscore, QWidget *parent, const char *name, bool modal) : QDialog(parent, name, modal)
+{
+ hs_ = highscore;
+ setCaption( tr( "Highscores" ) );
+ vbox_layout = new QVBoxLayout( this, 4 , 4 );
+ list = new QListView( this );
+ list->setSorting( -1 );
+ list->addColumn( tr( "#" ));
+ list->addColumn( tr( "Name" ));
+ list->addColumn( tr( "Points" ));
+ list->addColumn( tr( "Level" ));
+
+ createHighscoreListView();
+
+ vbox_layout->addWidget( list );
+ showMaximized();
+}
+
+void OHighscoreDialog::createHighscoreListView()
+{
+ int pos = 10;
+ int points_ = 0;
+ int level_ = 0;
+
+ std::list<t_playerData*>::reverse_iterator iListe = hs_->playerData.rbegin();
+
+ for ( ; iListe != hs_->playerData.rend() ; ++iListe )
+ {
+ QListViewItem *item = new QListViewItem( list );
+ item->setText( 0 , QString::number( pos ) ); //number
+ item->setText( 1 , ( *iListe )->sName ); //name
+ if ( ( *iListe )->points == -1 )
+ points_ = 0;
+ else points_ = ( *iListe )->points;
+ if ( ( *iListe )->level == -1 )
+ level_ = 0;
+ else level_ = ( *iListe )->level;
+ item->setText( 2 , QString::number( points_ ) ); //points
+ item->setText( 3 , QString::number( level_ ) ); //level
+ pos--;
+ }
+}
+
diff --git a/noncore/games/tetrix/ohighscoredlg.h b/noncore/games/tetrix/ohighscoredlg.h
new file mode 100644
index 0000000..fdbe623
--- a/dev/null
+++ b/noncore/games/tetrix/ohighscoredlg.h
@@ -0,0 +1,97 @@
+/***************************************************************************
+ * *
+ * 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. *
+ * *
+ **************************************************************************/
+
+class QWidget;
+class QVBoxLayout;
+class QListView;
+
+#include <qdialog.h>
+#include <list>
+
+using namespace std;
+
+struct t_playerData
+{
+ QString sName;
+ int points;
+ int level;
+};
+
+class OHighscore : public QWidget
+{
+ Q_OBJECT
+
+ public:
+ /*
+ *An OHighscore-Object contains all Points, level and playernames sorted in a stl::vector
+ */
+ OHighscore( int , int );
+
+ ~OHighscore();
+
+ /*
+ * is true if the player did a new highscore
+ */
+ bool isNewhighscore;
+
+ /*
+ * this inserts the new entry at the correct position
+ */
+ void insertData( QString , int , int );
+
+ list<t_playerData*> playerData;
+
+ /*
+ * As Qt/e does not support QInputDialog I did that code myself
+ */
+ QString getName();
+ private:
+
+ list<t_playerData*>::iterator iPlayerData;
+
+ /*
+ * the lowest score in the highscorelist
+ */
+ int lowest;
+
+ /*
+ * the level of the highscore
+ */
+ int pLevel;
+
+ /*
+ * get all scores in a vector and give "lowest" a value
+ */
+ void getList();
+
+ /*
+ * sets the bool if the current score is in the top10
+ */
+ void checkIfItIsANewhighscore( int );
+};
+
+class OHighscoreDialog : public QDialog
+{
+ Q_OBJECT
+
+ public:
+ OHighscoreDialog(OHighscore *highscore, QWidget *parent, const char *name = 0, bool modal = true );
+
+ private:
+ OHighscore *hs_;
+
+ QVBoxLayout *vbox_layout;
+
+ /*
+ * this method creates the QListView with all data
+ */
+ void createHighscoreListView();
+
+ QListView *list;
+};
diff --git a/noncore/games/tetrix/qtetrix.cpp b/noncore/games/tetrix/qtetrix.cpp
index f649894..6d29c3f 100644
--- a/noncore/games/tetrix/qtetrix.cpp
+++ b/noncore/games/tetrix/qtetrix.cpp
@@ -24,2 +24,3 @@
#include <qpe/resource.h>
+#include <qpe/config.h>
@@ -29,3 +30,5 @@
#include <qlayout.h>
+#include <qstring.h>
+#include "ohighscoredlg.h"
@@ -163,5 +166,9 @@ void QTetrix::gameOver()
{
+ OHighscore *hs = new OHighscore( showScore->text().toInt() , showLevel->text().toInt() );
+ if ( hs->isNewhighscore )
+ hs->insertData( hs->getName(), showScore->text().toInt() , showLevel->text().toInt() );
+ OHighscoreDialog hscdlg( hs, this, "OHighscoreDialog", true );
+ hscdlg.exec();
}
-
void QTetrix::quit()
diff --git a/noncore/games/tetrix/qtetrix.h b/noncore/games/tetrix/qtetrix.h
index b6e058a..cb33941 100644
--- a/noncore/games/tetrix/qtetrix.h
+++ b/noncore/games/tetrix/qtetrix.h
@@ -19,4 +19,2 @@
**********************************************************************/
-
-
#ifndef QTETRIX_H
diff --git a/noncore/games/tetrix/tetrix.pro b/noncore/games/tetrix/tetrix.pro
index 061b4f3..f316dc2 100644
--- a/noncore/games/tetrix/tetrix.pro
+++ b/noncore/games/tetrix/tetrix.pro
@@ -6,3 +6,4 @@ HEADERS = gtetrix.h \
qtetrixb.h \
- tpiece.h
+ tpiece.h \
+ ohighscoredlg.h
SOURCES = main.cpp \
@@ -11,3 +12,4 @@ SOURCES = main.cpp \
qtetrixb.cpp \
- tpiece.cpp
+ tpiece.cpp \
+ ohighscoredlg.cpp
INCLUDEPATH += $(OPIEDIR)/include