I'm trying to program a text based guessing game called "Bulls and Cows". The rules are as follows:
//-----------------------------------------------------------------------------
The program will generate a 4 digit number.
The user will guess a 4 digit number.
Any guessed digits that are both in the actual number and in the correct position will be called "Bulls".
Any guessed digits that are in the actual number, but are NOT in the correct position will be called "Cows".
After the player guesses a 4 digit number, the program will respond by telling the user how many "Bulls" and "Cows" are in his/her number.
ex) actual: (4123) ; guessed (4236) = 1 "Bull" and 2 "Cows"
//-----------------------------------------------------------------------------
I have been able to account for bulls with ease, however, cows are extremely
difficult for me to account for. I've tried many, many different schemes to no avail. Any experienced programmers out there who can help me?
NOTE: The function that will do the incrementing is called "check_numbers()".
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
|
#include "std_lib_facilities.h"
void generate_number(vector<int>& unknown)
{
srand(time(NULL));
for (int i = 0; i < unknown.size(); i++){ // fill vector 'unknown' with randomly generated digits; [0:9]
unknown[i] = rand()%10;
}
}
void print_rules()
{
cout << "Bulls and cows is a game in which you must guess\n"
<< "an unknown 4 digit number (the number may start with '0').\n\n";
cout << "You will begin the game by guessing any 4 digit\n"
<< "number you choose.\n";
cout << "The game will then respond with ''x' bull(s) and 'y' cow(s)'.\n\n";
cout << "Bulls represent digits that are both in the unknown\n"
<< "number and in the correct position\n\n";
cout << "Cows represent digits that are in the unknown number\n"
<< "but not in the correct position.\n\n";
cout << "For example, if the unknown number is 1234 and you\n"
<< "enter 1356, the game will respond '1 bull(s) and 1 cow(s).\n\n";
cout << "'1' is in the unknown number and in the correct position (a bull)\n"
<< "and '3' is in the unknown number, however, it is not in the\n"
<< "correct position (a cow).\n\n";
}
void choose_number(vector<int>& player)
{
cout << "Your guess: ";
char number;
for (int i = 0; i < player.size(); i++) // read in 4 digit number as individual digits and store them in vector 'player'
{
cin >> number;
if (!isdigit(narrow_cast<int>(number))) error("non-digit entered");
player[i] = int(number-'0'); // convert from ASCII representation to actual representation
}
}
void check_numbers(const vector<int>& unknown, const vector<int>& player,unsigned int& bulls,unsigned int& cows)
{
for (int i = 0; i < player.size(); i++) // how many bulls?
{
if (player[i] == unknown[i]) bulls++;
}
//TODO: Find cows
}
void print_results(const int& num_of_bulls, const int& num_of_cows)
{
string bulls = "bulls";
string cows = "cows";
if (num_of_bulls == 1 ) bulls = "bull";
if (num_of_cows == 1) cows = "cow";
cout << num_of_bulls << " " << bulls << " and "
<< num_of_cows << " " << cows << endl;
}
void play_game()
{
const char help = 'h';
const char quit = 'q';
const char start = 's';
vector<int>unknown_number(4); // store generated number as 4 digits
vector<int>player_number(4); // store guessed number as 4 digits
unsigned int num_of_bulls{ 0 };
unsigned int num_of_cows{ 0 };
generate_number(unknown_number);
cout << "Welcome to Bulls and Cows!\n"
<< "Enter 'h' to view the rules 'q' to quit, and 's' to begin.\n\n";
while (true)
{
cout << "Enter choice: ";
char choice;
cin >> choice;
switch (choice)
{
case quit:
return; // end of program
case help:
print_rules();
break;
case start:
//--------------------------------
for (int i : unknown_number)
{
cout << i; // FOR TESTING
}
cout << endl;
//--------------------------------
while (num_of_bulls != 4) // number has been guessed once num_of_bulls == 4
{
num_of_bulls = 0;
num_of_cows = 0;
choose_number(player_number);
check_numbers(unknown_number, player_number, num_of_bulls, num_of_cows); //how many bulls or cows?
print_results(num_of_bulls, num_of_cows);
}
cout << "Congratulations! You found the correct number!\n\n";
break;
default:
error("invalid input");
}
}
keep_window_open();
}
int main()
{
try {
play_game();
}
catch (exception& e) {
cerr << e.what() << endl;
keep_window_open("~1");
return 1;
}
catch (...) {
cerr << "Unknown exception" << endl;
keep_window_open("~2");
return 2;
}
}
|