2 Image Puzzle - A set your pieces straight game
3 Copyright (C) 2009 Timo Härkönen
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "statistics.h"
24 #include <QTextStream>
25 #include <QStringList>
29 Statistics *Statistics::instance_ = 0;
31 Statistics::Statistics(QObject *parent) :
42 Statistics *Statistics::instance()
45 instance_ = new Statistics;
51 int Statistics::gameCount(Difficulty difficulty) const
53 return games_.at(difficulty);
56 int Statistics::totalGameCount() const
59 for(int i = 0; i < games_.count(); ++i) {
60 count += games_.at(i);
66 qreal Statistics::averageMoves(Difficulty difficulty) const
69 if(games_.at(difficulty) > 0) {
70 count = (moves_.at(difficulty) / games_.at(difficulty));
76 int Statistics::minMoves(Difficulty difficulty) const
78 return minMoves_.at(difficulty);
81 int Statistics::maxMoves(Difficulty difficulty) const
83 return maxMoves_.at(difficulty);
86 void Statistics::addNewScore(int moves, Difficulty difficulty)
88 if(moves_.count() >= difficulty) {
89 moves_[difficulty] += moves;
92 if(maxMoves_.count() >= difficulty) {
93 if(maxMoves_.at(difficulty) < moves || maxMoves_.at(difficulty) == 0) {
94 maxMoves_[difficulty] = moves;
98 if(minMoves_.count() >= difficulty) {
99 if(minMoves_.at(difficulty) > moves || minMoves_.at(difficulty) == 0) {
100 minMoves_[difficulty] = moves;
107 void Statistics::increaseGameCount(Difficulty difficulty)
109 if(games_.count() >= difficulty) {
110 games_[difficulty]++;
114 void Statistics::readFile()
116 QFile file(QString("%1/%2/%3")
117 .arg(QDir::homePath())
122 qDebug() << __PRETTY_FUNCTION__ << "No settings file";
126 if(!file.open(QIODevice::ReadOnly)) {
130 QTextStream in(&file);
133 QString str = in.readLine();
135 QStringList list = str.split(" ");
137 if(list.count() == 5) {
138 moves_[list.at(0).toInt()] = list.at(1).toInt();
139 minMoves_[list.at(0).toInt()] = list.at(2).toInt();
140 maxMoves_[list.at(0).toInt()] = list.at(3).toInt();
141 games_[list.at(0).toInt()] = list.at(4).toInt();
148 void Statistics::saveFile()
150 QDir dir(QString("%1/%2")
151 .arg(QDir::homePath())
152 .arg(HOME_DIRECTORY));
154 dir.mkpath(QString("%1/%2")
155 .arg(QDir::homePath())
156 .arg(HOME_DIRECTORY));
159 QFile file(QString("%1/%2/%3")
160 .arg(QDir::homePath())
164 if(!file.open(QIODevice::WriteOnly)) {
168 QTextStream out(&file);
170 for(int i = 0; i < games_.count(); i++) {
171 out << QString("%1 %2 %3 %4 %5\n")
174 .arg(minMoves_.at(i))
175 .arg(maxMoves_.at(i))
182 void Statistics::resetStatistics()