Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <random>
- //TODO Clean up parts of the output
- //global variables
- std::string playWord[]= {"hell", "slim", "wait", "game", "blue", "west", "blood"};
- int attempts = 0;
- std::string word;
- //Prototype
- bool checkWord(std::string &input, std::string &word);
- void intro();
- void play();
- std::string getNewWord();
- int bullCount(std::string &input, const char array[]);
- int cowCount(std::string &input, const char array[]);
- int main() {
- intro();
- play();
- return 0;
- }
- //Intro message
- void intro(){
- std::cout << "Welcome to Bull & Cow!" << std::endl;
- std::cout << "A random word will be selected and you must guess it. " << std::endl;
- std::cout << "Bull: Correct letter in the correct position." << std::endl;
- std::cout << "Cow: Correct letter in the wrong position. " << std::endl;
- std::cout << "Enter 1 to exit" << std::endl;
- }
- //Random;ly selects a new word from playWord array
- //TODO Avoid repeating words
- std::string getNewWord(){
- static std::random_device rd; // only used once to initialise (seed) engine
- static std::ranlux24_base rng(rd()); // random-number engine used (Mersenne-Twister in this case)
- static std::uniform_int_distribution<int> uni(0,(sizeof(playWord) / sizeof( playWord[0])) - 1); // guaranteed unbiased
- auto random_integer = uni(rng);
- std::string newWord = playWord[random_integer];
- return newWord;
- }
- //Main chunk of the game
- void play(){
- std::string input;
- start:
- //Gets the word and informs user of length of the word
- word = getNewWord();
- std::cout << "The word is " << word.size() << " long." << std::endl;
- //Converts word into an array of char
- char wordArray[word.size() + 1];
- strncpy(wordArray, word.c_str(), word.size());
- wordArray[word.size()] = 0;
- //Loops the game till user gets the word or exit current word (1)
- do{
- std::cout <<"Enter your guess: " << std::endl;
- std::cin >> input;
- std::cout << "Debugg>> " << wordArray << std::endl;
- if(input == word){
- std::cout << "\nCongrats! You got it right" << std::endl;
- break;
- }
- else if (input == "1")
- break;
- else
- std::cout << "Bull: " << bullCount(input, wordArray) << " Cow: " << cowCount(input, wordArray) << std::endl;
- } while(true);
- std::cout << "Play again? Y or N " << std::endl;
- char again;
- std::cin >> again;
- if(again == 'y' || again == 'Y')
- goto start;
- else
- std::cout << "Thank you for playing!" << std::endl;
- }
- //Returns the bull count.
- int bullCount(std::string &input, const char array[]){
- int bullCount = 0;
- //Converts user's input into a char array
- char inputArray[input.size()];
- strncpy(inputArray, input.c_str(), input.size());
- inputArray[input.size()] = 0;
- //Check to see if user's input matches the word and adds to bullcount
- for(int i = 0; i < input.size(); i++){
- if(inputArray[i] == array[i])
- bullCount++;
- }
- return bullCount;
- }
- //Returns the cow count
- //Similar to bullcount function
- int cowCount(std::string &input, const char array[]){
- int cowCount = 0;
- char inputArray[input.size()];
- strncpy(inputArray, input.c_str(), input.size());
- inputArray[input.size()] = 0;
- //Takes i char of user's input and loops through word array, adds to cowcount if there's a match
- for(int i = 0; i < input.size(); i++){
- for(int j = 0; j < input.size(); j++)
- if(input[i] == array[j])
- cowCount++;
- }
- return cowCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment