Created
February 15, 2012 03:31
-
-
Save willcosgrove/1832949 to your computer and use it in GitHub Desktop.
Bulls and Cows game from chapter 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Will Cosgrove | |
121007913 | |
will@willcosgrove.com | |
Section 509 | |
1/31/2012 | |
Homework 3, Problem 1 | |
*/ | |
#include "std_lib_facilities.h" | |
class Gamemaster { | |
int guesses; | |
public: | |
bool solved; | |
vector<int> numbers; | |
Gamemaster(){ | |
pick_numbers(); | |
guesses = 0; | |
solved = false; | |
} | |
void pick_numbers(){ | |
for(int i = 0; i < 4; i++){ | |
numbers.push_back(rand() % 10); | |
} | |
} | |
void check(string guess){ | |
guesses++; | |
int bulls = 0; | |
int kine = 0; | |
for(int i = 0; i < 4; i++){ | |
int current_number = guess[i] - 48; | |
if(numbers[i] == current_number){ | |
bulls++; | |
} else if(find(numbers.begin(), numbers.end(), current_number) != numbers.end()){ | |
kine++; | |
} | |
} | |
if(bulls == 4){ | |
solved = true; | |
cout << "You solved it! And in only " << guesses << " guesses! What a pro.\n"; | |
} else { | |
cout << bulls << " bulls " << kine << " kine\n"; | |
} | |
} | |
}; | |
int main(){ | |
string guess; | |
Gamemaster g = Gamemaster(); // Initialize the Gamemaster | |
while(!g.solved){ // While the game is not solved | |
cout << "Make a guess\n"; | |
cin >> guess; | |
g.check(guess); // Check for bulls and kine | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment