From: Timo Härkönen Date: Mon, 6 Sep 2010 19:38:03 +0000 (+0300) Subject: Collecting basic statistics from won games X-Git-Tag: 0.7.2~6 X-Git-Url: http://git.maemo.org/git/?p=impuzzle;a=commitdiff_plain;h=58ec0e286982cd15beff65303797420c2f913fcb Collecting basic statistics from won games --- diff --git a/src/defines.h b/src/defines.h index 6007ebf..f58092f 100644 --- a/src/defines.h +++ b/src/defines.h @@ -33,6 +33,7 @@ #define SELECT_IMAGE_TXT "Select image..." #define RESTORE_FILE "impuzzle.dat" +#define STATS_FILE "imstats.dat" #define HOME_DIRECTORY ".impuzzle" #define IMPUZZLE_VERSION "0.6" diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bb6ac1b..e2dbc09 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -22,6 +22,8 @@ #include "settingsdialog.h" #include "aboutdialog.h" #include "puzzleitem.h" +#include "statistics.h" +#include "statisticsdialog.h" #include #include @@ -63,6 +65,7 @@ void MainWindow::createMenu() { menu_ = menuBar()->addMenu("&Game"); menu_->addAction(newGameAction_); + menu_->addAction(statisticsAction_); menu_->addAction(saveAction_); menu_->addAction(importAction_); @@ -85,6 +88,9 @@ void MainWindow::createActions() saveAction_ = new QAction(tr("Save and quit"), this); connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame())); saveAction_->setDisabled(true); + + statisticsAction_ = new QAction(tr("Statistics"), this); + connect(statisticsAction_, SIGNAL(triggered()), this, SLOT(showStatistics())); } void MainWindow::importClicked() @@ -114,6 +120,9 @@ void MainWindow::gameEnded() { if(saveAction_->isEnabled()) { saveAction_->setDisabled(true); + Statistics::instance()->increaseGameCount(Settings::instance()->pieceCount() == EASY_PIECE_COUNT ? Statistics::easyDifficulty : Statistics::hardDifficulty); + Statistics::instance()->addNewScore(PuzzleItem::moveCount(), + Settings::instance()->pieceCount() == EASY_PIECE_COUNT ? Statistics::easyDifficulty : Statistics::hardDifficulty); PuzzleItem::resetMoveCount(); } } @@ -139,3 +148,9 @@ void MainWindow::closeEvent(QCloseEvent *event) event->accept(); } + +void MainWindow::showStatistics() +{ + StatisticsDialog dialog(this); + dialog.exec(); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 2ce4607..4fa0aab 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -40,6 +40,7 @@ private slots: void aboutClicked(); void gameEnded(); void enableSaving(); + void showStatistics(); protected: void closeEvent(QCloseEvent *event); @@ -54,6 +55,7 @@ private: QAction *importAction_; QAction *aboutAction_; QAction *saveAction_; + QAction *statisticsAction_; QMenu *menu_; QMenu *helpMenu_; diff --git a/src/src.pro b/src/src.pro index 2b2b969..2b28d76 100644 --- a/src/src.pro +++ b/src/src.pro @@ -14,7 +14,9 @@ HEADERS += gameview.h \ introitem.h \ settings.h \ settingsdialog.h \ - aboutdialog.h + aboutdialog.h \ + statistics.h \ + statisticsdialog.h SOURCES += gameview.cpp \ main.cpp \ mainwindow.cpp \ @@ -23,7 +25,9 @@ SOURCES += gameview.cpp \ introitem.cpp \ settings.cpp \ settingsdialog.cpp \ - aboutdialog.cpp + aboutdialog.cpp \ + statistics.cpp \ + statisticsdialog.cpp RESOURCES += resources.qrc desktop.files += impuzzle.desktop desktop.path = /usr/share/applications/hildon/ diff --git a/src/statistics.cpp b/src/statistics.cpp new file mode 100644 index 0000000..eb5043e --- /dev/null +++ b/src/statistics.cpp @@ -0,0 +1,150 @@ +#include "statistics.h" +#include "defines.h" + +#include +#include +#include +#include + +Statistics *Statistics::instance_ = 0; + +Statistics::Statistics(QObject *parent) : + QObject(parent) +{ + moves_ << 0 << 0; + minMoves_ << 0 << 0; + maxMoves_ << 0 << 0; + games_ << 0 << 0; + + readFile(); +} + +Statistics *Statistics::instance() +{ + if(!instance_) { + instance_ = new Statistics; + } + + return instance_; +} + +int Statistics::gameCount(Difficulty difficulty) const +{ + return games_.at(difficulty); +} + +int Statistics::totalGameCount() const +{ + int count = 0; + for(int i = 0; i < games_.count(); ++i) { + count += games_.at(i); + } + + return count; +} + +qreal Statistics::averageMoves(Difficulty difficulty) const +{ + qreal count = 0.0; + if(games_.at(difficulty) > 0) { + count = moves_.at(difficulty) / static_cast(games_.at(difficulty)); + } + + return count; +} + +int Statistics::minMoves(Difficulty difficulty) const +{ + return minMoves_.at(difficulty); +} + +int Statistics::maxMoves(Difficulty difficulty) const +{ + return maxMoves_.at(difficulty); +} + +void Statistics::addNewScore(int moves, Difficulty difficulty) +{ + if(moves_.count() >= difficulty) { + moves_[difficulty] += (moves_[difficulty] + moves); + } + + if(maxMoves_.count() >= difficulty) { + if(maxMoves_.at(difficulty) < moves || maxMoves_.at(difficulty) == 0) { + maxMoves_[difficulty] = moves; + } + } + + if(minMoves_.count() >= difficulty) { + if(minMoves_.at(difficulty) > moves || minMoves_.at(difficulty) == 0) { + minMoves_[difficulty] = moves; + } + } + + saveFile(); +} + +void Statistics::increaseGameCount(Difficulty difficulty) +{ + if(games_.count() >= difficulty) { + games_[difficulty]++; + } +} + +void Statistics::readFile() +{ + QFile file(QString("%1/%2/%3") + .arg(QDir::homePath()) + .arg(HOME_DIRECTORY) + .arg(STATS_FILE)); + + if(!file.exists()) { + return; + } + + if(!file.open(QIODevice::ReadOnly)) { + return; + } + + QTextStream in(&file); + + while(!in.atEnd()) { + QString str = in.readLine(); + + QStringList list = str.split(" "); + + if(list.count() == 5) { + moves_[list.at(0).toInt()] = list.at(1).toInt(); + minMoves_[list.at(0).toInt()] = list.at(2).toInt(); + maxMoves_[list.at(0).toInt()] = list.at(3).toInt(); + games_[list.at(0).toInt()] = list.at(4).toInt(); + } + } + + file.close(); +} + +void Statistics::saveFile() +{ + QFile file(QString("%1/%2/%3") + .arg(QDir::homePath()) + .arg(HOME_DIRECTORY) + .arg(STATS_FILE)); + + if(!file.open(QIODevice::WriteOnly)) { + return; + } + + QTextStream out(&file); + + for(int i = 0; i < games_.count(); i++) { + out << QString("%1 %2 %3 %4 %5\n") + .arg(i) + .arg(moves_.at(i)) + .arg(minMoves_.at(i)) + .arg(maxMoves_.at(i)) + .arg(games_.at(i)); + } + + file.close(); +} diff --git a/src/statistics.h b/src/statistics.h new file mode 100644 index 0000000..f2fa380 --- /dev/null +++ b/src/statistics.h @@ -0,0 +1,49 @@ +#ifndef STATISTICS_H +#define STATISTICS_H + +#include +#include + +class Statistics : public QObject +{ + Q_OBJECT + +public: + enum Difficulty { + easyDifficulty = 0, + hardDifficulty + }; + + static Statistics *instance(); + + int gameCount(Difficulty difficulty) const; + + int totalGameCount() const; + + qreal averageMoves(Difficulty difficulty) const; + + int minMoves(Difficulty difficulty) const; + + int maxMoves(Difficulty difficulty) const; + +public slots: + void addNewScore(int moves, Difficulty difficulty); + + void increaseGameCount(Difficulty difficulty); + + void readFile(); + + void saveFile(); + +private: + Statistics(QObject *parent = 0); + + static Statistics *instance_; + + QList moves_; + QList minMoves_; + QList maxMoves_; + QList games_; +}; + +#endif diff --git a/src/statisticsdialog.cpp b/src/statisticsdialog.cpp new file mode 100644 index 0000000..5cc927e --- /dev/null +++ b/src/statisticsdialog.cpp @@ -0,0 +1,31 @@ +#include "statisticsdialog.h" +#include "statistics.h" + +#include +#include + +StatisticsDialog::StatisticsDialog(QWidget *parent) : + QDialog(parent) +{ + setModal(true); + setWindowTitle(tr("Statistics")); + + label_ = new QLabel; + + QString txt = QString("Easy:\nGames\t%1\nBest\t%2\nAvg\t%3\nWorst\t%4\n\nHard:\nGames\t%5\nBest\t%6\nAvg\t%7\nWorst\t%8") + .arg(QString::number(Statistics::instance()->gameCount(Statistics::easyDifficulty))) + .arg(QString::number(Statistics::instance()->minMoves(Statistics::easyDifficulty))) + .arg(QString::number(Statistics::instance()->averageMoves(Statistics::easyDifficulty))) + .arg(QString::number(Statistics::instance()->maxMoves(Statistics::easyDifficulty))) + .arg(QString::number(Statistics::instance()->gameCount(Statistics::hardDifficulty))) + .arg(QString::number(Statistics::instance()->minMoves(Statistics::hardDifficulty))) + .arg(QString::number(Statistics::instance()->averageMoves(Statistics::hardDifficulty))) + .arg(QString::number(Statistics::instance()->maxMoves(Statistics::hardDifficulty))); + + label_->setText(txt); + + mainLayout_ = new QVBoxLayout; + mainLayout_->addWidget(label_); + + setLayout(mainLayout_); +} diff --git a/src/statisticsdialog.h b/src/statisticsdialog.h new file mode 100644 index 0000000..714a014 --- /dev/null +++ b/src/statisticsdialog.h @@ -0,0 +1,21 @@ +#ifndef STATISTICSDIALOG_H +#define STATISTICSDIALOG_H + +#include + +class QLabel; +class QVBoxLayout; + +class StatisticsDialog : public QDialog +{ + Q_OBJECT + +public: + StatisticsDialog(QWidget *parent = 0); + +private: + QLabel *label_; + QVBoxLayout *mainLayout_; +}; + +#endif