-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind2.cpp
More file actions
123 lines (118 loc) · 3.29 KB
/
mastermind2.cpp
File metadata and controls
123 lines (118 loc) · 3.29 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
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
#include <bits/stdc++.h>
using namespace std;
void unos(vector<int> &a){
cout << "Unesi svoju kombinaciju " << '\n';
for(int i = 0; i < 4;i++){
cin >> a[i];
}
}
pair<int,int> provera(vector<int> &a,vector<int> &res){
vector<bool> crn(4,false) ,upo(4,false);
int broj = 0;
int broj2 = 0;
for(int i = 0; i < 4; i++){
if(a[i]==res[i]){
broj++;
crn[i]=true;
upo[i]=true;
}
}
for(int i = 0;i < 4; i++){
if(upo[i]==true) continue;
for(int j = 0; j < 4; j++){
if(a[j] == res[i] and crn[j]==false){
broj2++;
crn[j]=true;
break;
}
}
}
return make_pair(broj,broj2);
}
vector<vector<int>> generisisve(){
vector<vector<int>>svi;
for(int i = 0; i < 6;i++)
for(int j = 0; j < 6;j++)
for(int k = 0; k < 6;k++)
for(int l = 0; l < 6;l++)
svi.push_back({i,j,k,l});
return svi;
}
pair<int,int> pitaj(vector<int>&upit){
cout << upit[0] << ' ' << upit[1] << ' ' << upit[2] << ' ' << upit[3] << '\n';
int tacnih,tacnih2;
cout << "Koliko ima tacnih na mestu a koliko ne na mestu" << '\n' << "Na mestu: ";
cin >> tacnih;
cout << "Koji nisu na mestu: ";
cin >> tacnih2;
// cout << '\n';
return make_pair(tacnih,tacnih2);
}
vector<int> sledeciUpit(vector<vector<int>>pre, vector<vector<int>>svi){
int mini = INT_MAX;
vector<int> naj;
for(auto &upit : svi){
map<pair<int,int>,int> poeni;
for(auto &x : pre){
pair<int,int> c = provera(upit,x);
poeni[c]++;
}
int los = 0;
for(auto &par: poeni)
los = max(par.second,los);
if(los <mini || (los == mini && find(pre.begin(),pre.end(),upit)!=pre.end())){
mini = los;
naj = upit;
}
}
return naj;
}
void igraj(){
vector<vector<int>>svi= generisisve();
vector<vector<int>>preostali = svi;
vector<int> upit {0,0,1,1};
int pokusaji = 1;
while(true){
pair<int,int> odg = pitaj(upit);
if(odg.first == 4)break;
vector<vector<int>>sledeci;
for( auto& x : preostali){
if(provera(upit,x)==odg)
sledeci.push_back(x);
}
preostali = sledeci;
upit = sledeciUpit(preostali,svi);
pokusaji++;
}
return;
}
int main()
{
cout<<"Ako zelis da pogadjas sifru pritisni dugme 1, ako zelis da robot pogadja tvoju sifru pritisni dugme 2." << '\n';
int br;cin >> br;
bool igrac=false, bot=false;
if(br == 1) igrac = true;
else if(br == 2) bot = true;
if(igrac== true){
vector<int> res(4);
srand(time(0));
for(int i = 0;i < 4;i++){
res[i]=rand() %6 ;
}
bool tacno = false;
for(int i = 0; i<8 and !tacno; i++){
pair<int,int> odgovor;
vector<int> a(4);
unos(a);
odgovor = provera(a,res);
cout << odgovor.first << ' ' << odgovor.second << '\n';
if(odgovor.first==4) tacno = true;
}
if(tacno) cout << "Bravo";
else cout << "Vise srece drugi put" << " resenje je bilo " << res[0] << ' '<< res[1] << ' '<< res[2] << ' '<< res[3] << ' ';
}
else{
igraj();
}
return 0;
}