mithusingh32

Untitled

Jul 31st, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <random>
  4.  
  5. //TODO Clean up parts of the output
  6.  
  7. //global variables
  8. std::string playWord[]= {"hell", "slim", "wait", "game", "blue", "west", "blood"};
  9. int attempts = 0;
  10. std::string word;
  11.  
  12. //Prototype
  13. bool checkWord(std::string &input, std::string &word);
  14. void intro();
  15. void play();
  16. std::string getNewWord();
  17. int bullCount(std::string &input, const char array[]);
  18. int cowCount(std::string &input, const char array[]);
  19.  
  20.  
  21. int main() {
  22.  
  23.     intro();
  24.     play();
  25.     return 0;
  26.  
  27.  
  28. }
  29.  
  30. //Intro message
  31. void intro(){
  32.     std::cout << "Welcome to Bull & Cow!" << std::endl;
  33.     std::cout << "A random word will be selected and you must guess it. " << std::endl;
  34.     std::cout << "Bull: Correct letter in the correct position." << std::endl;
  35.     std::cout << "Cow: Correct letter in the wrong position. " << std::endl;
  36.     std::cout << "Enter 1 to exit" << std::endl;
  37. }
  38.  
  39. //Random;ly selects a new word from playWord array
  40. //TODO Avoid repeating words
  41. std::string getNewWord(){
  42.  
  43.     static std::random_device rd;     // only used once to initialise (seed) engine
  44.     static std::ranlux24_base rng(rd());    // random-number engine used (Mersenne-Twister in this case)
  45.     static std::uniform_int_distribution<int> uni(0,(sizeof(playWord) / sizeof( playWord[0])) - 1); // guaranteed unbiased
  46.     auto random_integer = uni(rng);
  47.     std::string newWord = playWord[random_integer];
  48.     return newWord;
  49. }
  50.  
  51. //Main chunk of the game
  52. void play(){
  53.     std::string input;
  54.    
  55.    
  56.     start:
  57.         //Gets the word and informs user of length of the word
  58.         word = getNewWord();
  59.         std::cout << "The word is " << word.size() << " long." << std::endl;
  60.    
  61.         //Converts word into an array of char
  62.         char wordArray[word.size() + 1];
  63.         strncpy(wordArray, word.c_str(), word.size());
  64.         wordArray[word.size()] = 0;
  65.  
  66.  
  67.         //Loops the game till user gets the word or exit current word (1)
  68.         do{
  69.             std::cout <<"Enter your guess: " << std::endl;
  70.             std::cin >> input;
  71.  
  72.             std::cout << "Debugg>> " << wordArray << std::endl;
  73.  
  74.             if(input == word){
  75.                 std::cout << "\nCongrats! You got it right" << std::endl;
  76.                 break;
  77.             }
  78.             else if (input == "1")
  79.                 break;
  80.             else
  81.                 std::cout << "Bull: " << bullCount(input, wordArray) << " Cow: " << cowCount(input, wordArray) << std::endl;
  82.  
  83.         } while(true);
  84.     std::cout << "Play again? Y or N " << std::endl;
  85.     char again;
  86.     std::cin >> again;
  87.     if(again == 'y' || again == 'Y')
  88.         goto start;
  89.     else
  90.         std::cout << "Thank you for playing!" << std::endl;
  91.  
  92.  
  93. }
  94.  
  95. //Returns the bull count.
  96. int bullCount(std::string &input, const char array[]){
  97.     int bullCount = 0;
  98.     //Converts user's input into a char array
  99.     char inputArray[input.size()];
  100.     strncpy(inputArray, input.c_str(), input.size());
  101.     inputArray[input.size()] = 0;
  102.  
  103.     //Check to see if user's input matches the word and adds to bullcount
  104.     for(int i = 0; i < input.size(); i++){
  105.         if(inputArray[i] == array[i])
  106.             bullCount++;
  107.  
  108.     }
  109.     return bullCount;
  110. }
  111.  
  112. //Returns the cow count
  113. //Similar to bullcount function
  114. int cowCount(std::string &input, const char array[]){
  115.     int cowCount = 0;
  116.  
  117.     char inputArray[input.size()];
  118.     strncpy(inputArray, input.c_str(), input.size());
  119.     inputArray[input.size()] = 0;
  120.    
  121.     //Takes i char of user's input and loops through word array, adds to cowcount if there's a match
  122.     for(int i = 0; i < input.size(); i++){
  123.         for(int j = 0; j < input.size(); j++)
  124.             if(input[i] == array[j])
  125.                 cowCount++;
  126.  
  127.     }
  128.  
  129.     return cowCount;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment