-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameclass.cpp
More file actions
90 lines (76 loc) · 2.06 KB
/
Copy pathgameclass.cpp
File metadata and controls
90 lines (76 loc) · 2.06 KB
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
#include "gameclass.h"
GameClass::GameClass(int count, QObject *parent) : QObject(parent)
{
}
BullsAndCows GameClass::checkDigits(const QVector<int> &attempt)
{
BullsAndCows toResult;
// Вычисляем быков, т.е. прямые попадания
int bulls = 0;
for (int i = 0; i < countDigits; i++)
if (digits.at(i) == attempt.at(i))
bulls++;
// Если прямых попаданий равно количеству цифр, то победа
if (bulls == countDigits)
emit win();
// Вычисляем коров, т.е. просто отгаданные числа
int cows = 0;
for (int i = 0; i < countDigits; i++)
{
for (int j = 0; j < countDigits; j++)
{
if ((i != j) && (digits.at(i) == attempt.at(j)))
{
cows++;
}
}
}
toResult.bulls = bulls;
toResult.cows = cows;
#ifdef _DEBUG
qDebug() << "Bulls: " << toResult.bulls << " Cows: " << toResult.cows;
#endif
return toResult;
}
void GameClass::setDigits(int count)
{
countDigits = count;
// Удаляем все данные и обнуляем переменные
digits.clear();
// Временно сохраняем все значения в вектор
QVector<int> nums;
int tmp;
qsrand(time(0));
for (int i = 0; i < count; i++)
{
while(true)
{
tmp = qrand() % 10;
bool flag = false;
for (int j = 0; j < nums.length(); j++)
{
if (nums.at(j) == tmp)
{
flag = true;
break;
}
}
if (!flag)
{
nums << tmp;
break;
}
}
digits << tmp;
}
#ifdef _DEBUG
qDebug() << "Note:\tGenerated: " << digits;
#endif
}
QString GameClass::getDigits()
{
QString dig;
for (int i = 0; i < digits.length(); i++)
dig += QString::number(digits.at(i));
return dig;
}