Skip to content

Instantly share code, notes, and snippets.

@lnrsoft
Last active January 21, 2017 21:38
Show Gist options
  • Save lnrsoft/9748244 to your computer and use it in GitHub Desktop.
Save lnrsoft/9748244 to your computer and use it in GitHub Desktop.
// This source code written by Roland Ihasz
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
int randint(int min, int max){
srand(static_cast<unsigned int>(time(0)));
return (min < max) ? ((rand() % ((max+1)-min)) + min) :
((rand() % ((min+1)-max)) + max);
}
int main()
{
vector<int> bc;
vector<int> bcUser;
vector<int> bulls;
vector<int> cows;
int n=1;
int sum (0);
int x;
cout << "Welcome to “Bulls and Cows” game. Please try to guess my secret 4-digit number." << endl;
cout << "Please enter an integer value between 2 and 10: ";
cin >> x;
int srand(x);
int a = randint(x,2);
int b = randint(x,3);
int c = randint(x,4);
int d = randint(x,5);
bc.push_back(a);
bc.push_back(b);
bc.push_back(c);
bc.push_back(d);
cout << a << " " << b << " " << c << " " << d << endl;
while (n<100)
{
cows.clear();
bcUser.clear();
bulls.clear();
cout << endl << n <<". " << endl;
for (int i = 0; i<=3; i++)
{
int user;
cout << "Enter your "<< i+1 <<". digits guess: ";
cin >> user;
bcUser.push_back(user);
}
for (int i = 0; i<=3; i++)
{
if (bc[i]==bcUser[i])
{
bulls.push_back(bc[i]);
}
}
if (bulls.size() == 4)
{
cout << endl << bulls.size() << " Bulls and " << cows.size() << " Cows. Well done!" << endl;
return 0;
}
if (bulls.size() < 4)
{
for (int i = 0; i<=3; i++)
{
if (bc[i]!=bcUser[i] && bc[i]==bcUser[0]) // first number
{
cows.push_back(bc[i]);
}
if (bc[i]!=bcUser[i] && bc[i]==bcUser[1]) // second number
{
cows.push_back(bc[i]);
}
if (bc[i]!=bcUser[i] && bc[i]==bcUser[2]) // third number
{
cows.push_back(bc[i]);
}
if (bc[i]!=bcUser[i] && bc[i]==bcUser[3]) // fourth number
{
cows.push_back(bc[i]);
}
}
}
++n;
cout << bulls.size() << " Bulls and " << cows.size() << " Cows." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment