/
engine.cpp
147 lines (133 loc) · 3.18 KB
/
engine.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "engine.h"
Engine::Engine()
{
init(4);
}
void Engine::init(int _len)
{
len=_len;
finalInt=0;
for (int i=9; i>9-len; i--)
finalInt=finalInt*10+i;
if (len==10)
finalInt*=10;
currString=GAME_START;
q.clear();
QString str;
str="10";
for (int i=0; i<len-2; i++)
str+=('2'+i);
q.push_back(str);
do
q.push_back(getNextString(q.back()));
while (q.back()!=LAST_STRING);
}
int Engine::getLength()
{
return len;
}
void Engine::setBullsCows(int _bulls, int _cows)
{
if (_bulls+_cows>len || (_bulls==len-1 && _cows==1))
throw COMPG_WRONG_BC_VALUES;
bulls=_bulls;
cows=_cows;
}
int Engine::solutionSize()
{
return q.size()-1;
}
QString Engine::getNextValue()
{
if (currString!=GAME_START)
{
while (q.front()!=LAST_STRING)
{
if ( check(q.front(),currString,bulls,cows) )
q.push_back(q.front());
q.pop_front();
}
q.pop_front();
q.push_back(LAST_STRING);
}
if (q.front()==LAST_STRING)
throw COMPG_NO_SOLUTIONS;
qsrand(QTime::currentTime().msec());
currString=q[qrand()%(q.size()-1)];
return currString;
}
bool Engine::check(QString a, QString b, int _bulls, int _cows)
{
_cows+=_bulls;
for (int i=0; i<len; i++)
if (a[i]==b[i])
_bulls--;
if (_bulls!=0)
return false;
int temp[10]={};
for (int i=0; i<len; i++)
{
temp[a[i].toLatin1()-'0']++;
temp[b[i].toLatin1()-'0']++;
}
for (int i=0; i<10; i++)
if (temp[i]==2)
_cows--;
if (_cows!=0)
return false;
return true;
}
QString Engine::getNextString(QString str)
{
int temp=str.toInt()+1;
while (true)
{
bool fine=true;
if (temp==finalInt)
return LAST_STRING;
int x=temp, arr[10]={};
while (x!=0)
{
arr[x%10]++;
x/=10;
}
for (int i=0; i<10; i++)
if (arr[i]>1)
{
temp++;
i=10;
fine=false;
break;
}
if (fine)
return QString::number(temp);
}
}
void Engine::guessNumber(int _len)
{
init(_len);
guessedByComputer=getNextValue();
}
QPair<int,int> Engine::processUserGuess(QString users)
{
for (int i=0; i<users.length(); i++) // not a number
if (users[i]<QChar('0') || users[i]>QChar('9'))
throw USERG_NOT_NUMBER;
if (users.length()!=len || users[0]==QChar('0')) // if number has wrong size
throw USERG_WRONG_LENGTH;
int digits[10]={}; // if number has more than one same digits
for (int i=0; i<len; i++)
digits[users[i].toLatin1()-'0']++;
for (int i=0; i<10; i++)
if (digits[i]>1)
throw USERG_REPEATED_DIGITS;
QPair<int,int> temp(0,0);
for (int i=0; i<len; i++)
if (users[i]==guessedByComputer[i])
temp.first++;
for (int i=0; i<len; i++)
if (digits[guessedByComputer[i].toLatin1()-'0']==1)
temp.second++;
temp.second-=temp.first;
return temp;
}