-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.cpp
217 lines (170 loc) · 4.09 KB
/
mastermind.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
///*
// * File: main.cpp
// * Author: vania
// *
// * Created on September 18, 2013, 3:50 PM
// */
//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
bool hasduplicate(string);
bool checks(string);
int MisplacedNumbers(string ,string);
int CorrectPositions(string ,string);
string numtostr(int,int,int,int);
string givefeedback(string,string);
void eliminateprobability(int);
string givefeedback(string,string);
void generateProbabilities(void);
int probabilitycount=0;
string probabilities[362880]; /*(9! possibilities not excluding duplicates*/
int main()
{
srand(time(0));
cout<<endl;
cout << "Let's play Mastermind" <<endl;
short int correct,wrongplace,i;
int GuessCounter;
string result;
string feedback;
string lastguess;
string Temp[362880];
/*Valid numbers 0 -9*/
/*9! == 362880 possibilities */
GuessCounter = 0;
i=0;
generateProbabilities();
do
{
if(GuessCounter==0)
{
cout<<"Feedback will be displayed in the following format: xAyB " <<endl;
cout << "Enter in number:";
cin >> result; //Store number from Human Player
cout <<endl;
}
lastguess=probabilities[rand()%probabilitycount];
cout<<"Computer Guesses: "<< lastguess <<endl <<endl;
feedback = givefeedback(lastguess,result); // look at last guess computer gives and
correct = (feedback[0] - '0');
wrongplace = (feedback[2] - '0');
GuessCounter++;
i=0;
do
{
if ((CorrectPositions(probabilities[i],lastguess)!=correct) || (MisplacedNumbers(probabilities[i],lastguess)!=wrongplace))
{
eliminateprobability(i);
}
else
i++;
} while ((i<probabilitycount) && (probabilitycount>=0));
} while (((correct!=4) || (wrongplace!=0) )&& (probabilitycount>0));
if (probabilitycount<=0)
{
cout<<" The feedback you have provided is invalid.\n";
return -1;
}
cout<<"\n I found it in " << GuessCounter <<" Guesses "<<endl;
return 0;
}
bool hasduplicate(string number)
{
int i;
int j;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
{
if (i!=j)
if (number[i]==number[j])
{
return true;
}
}
return false;
}
bool checks(string number)
{
if (number.length()!=4)
return false; //if not 4 numbers
if (hasduplicate(number))
return false; // duplicates not allowed
return true;
}
int CorrectPositions(string solution,string guess)
{
int t=0;
for(int i=0;i<4;i++)
{
if (solution[i]==guess[i])
t++;
}
return t;
}
int MisplacedNumbers(string solution, string guess)
{
int l=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if((solution[i]==guess[j])&&(i!=j))
l++;
}
return l;
}
string numtostr(int i,int j,int k,int l)
{
string result="";
// add '0' to convert to str
result+= i + '0';
result+= j + '0';
result+= k + '0';
result+= l + '0';
return result;
}
void eliminateprobability(int i)
{
/*Iterate through array and give rid of probabilities that aren't possible*/
int x;
for (x=i;x<=probabilitycount;x++)
{
/*Reposition elements in the array*/
probabilities[x]=probabilities[x+1];
}
probabilitycount=probabilitycount-1;
}
string givefeedback(string guess,string sol)
{
string feedback = ""; // CorrectPosition
int correctpos=CorrectPositions(sol,guess);
int correctnums=MisplacedNumbers(sol,guess);
feedback+= correctpos + '0';
feedback+="A";
feedback+=correctnums + '0';
feedback+="B";
cout << "Feedback: " <<feedback <<endl;
return feedback;
}
void generateProbabilities()
{
int i;
int j;
int k;
int l;
//Generate probability List
//gives range 0 - 9
for (i=0;i<10;i++)
for (j=0;j<10;j++)
for (k=0;k<10;k++)
for (l=0;l<10;l++)
{
if (checks( numtostr(i,j,k,l) )==true) // add non duplicated numbers to array is ascending order
{
probabilities[probabilitycount]= numtostr(i,j,k,l);
probabilitycount+=1;
}
}
}