-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
104 lines (90 loc) · 2.33 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "game.h"
#include <QDateTime>
#include <algorithm>
#include <random>
Game::Game(QObject *parent)
: QObject(parent)
, m_score(0)
, m_remainingTime(0)
, m_taskIntervalTime(0)
, m_currentTask(0)
, m_isInCheckAnswer(false)
{
m_score = 0;
m_remainingTime = 0;
m_currentTask = 0;
m_secondsTimer.setInterval(1000);
connect(&m_secondsTimer, SIGNAL(timeout()), this, SLOT(update()));
}
void Game::reset(int intervalInSeconds)
{
shuffleContainer(m_tasks);
m_taskIntervalTime = intervalInSeconds;
setScore(0);
m_secondsTimer.stop();
m_currentTask = 0;
}
QString Game::nextTask()
{
m_remainingTime = m_taskIntervalTime;
m_secondsTimer.start();
return m_tasks.value(m_currentTask).question;
}
QVector<QString> Game::possibleAnswers() const
{
QVector<QString> answers = m_tasks.value(m_currentTask).possibleAnswers;
answers.append(m_tasks.value(m_currentTask).answer);
shuffleContainer(answers);
return answers;
}
int Game::score()
{
return m_score;
}
int Game::intervall()
{
return m_taskIntervalTime;
}
void Game::update()
{
if (m_remainingTime < 0 && !m_isInCheckAnswer) {
m_secondsTimer.stop();
emit userMessage("Zeit zu ende - verloren");
emit end(m_score);
}
emit remainingTimeChanged(m_remainingTime--);
}
void Game::setScore(int score)
{
m_score = score;
emit scoreChanged(m_score);
}
bool Game::checkAnswer(const QString &answer)
{
bool result = false;
m_isInCheckAnswer = true;
const Task currentTask = m_tasks.value(m_currentTask);
if (currentTask.answer == answer) {
setScore(m_score + (m_remainingTime + 2)/10 + currentTask.scoreValue);
emit userMessage("richtig");
result = true;
} else {
setScore(m_score - (m_remainingTime + 2)/10 + currentTask.scoreValue);
emit userMessage(QString("leider falsch - %1 = %2").arg(
currentTask.question, currentTask.answer));
}
m_currentTask++;
if (m_tasks.count() > m_currentTask)
QTimer::singleShot(2000, this, SIGNAL(nextTaskPossible()));
else {
m_secondsTimer.stop();
emit userMessage("alles geschaft, super!");
emit end(m_score);
}
m_isInCheckAnswer = false;
return result;
}
void Game::setTasks(const QVector<Task> &tasks)
{
m_tasks = tasks;
}