Advertisement
KeeganT

Ass610

Oct 16th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. string code;
  10. string codeArr[9];
  11. int array[9];
  12.  
  13. void explain()
  14. {
  15.     cout<<"Welcome to Bulls and Cows!"<<endl;
  16.     cout<<"--------------------------"<<endl;
  17.     cout<<"The computer will generate a random 4 digit number with no repeating digits."<<endl;
  18.     cout<<"The objective of the game is for you to guess that 4 digit number."<<endl;
  19.     cout<<"The computer responds with how close your guess is to the number."<<endl;
  20.     cout<<"Bulls = Number of correct digits in the correct place"<<endl;
  21.     cout<<"Cows = Number of correct digits in the wrong place"<<endl;
  22.     cout<<"Good luck! Press any key to continue."<<endl;
  23.     system("pause>nul");
  24. }
  25.  
  26. void generateNumber()
  27. {
  28.     srand(time(NULL));
  29.     int temp;
  30.     string tempString;
  31.     for(int c=0;c<9;c++)array[c]=c+1;
  32.     for(int c=0;c<9;c++)
  33.     {
  34.         int x=rand()%4+1;
  35.         temp=array[c];
  36.         array[c]=array[x];
  37.         array[x]=temp;
  38.     }
  39.     for(int c=0;c<4;c++)
  40.     {
  41.         ostringstream convert;
  42.         convert<<array[c];
  43.         tempString=convert.str();
  44.         code+=tempString;
  45.     }
  46.     for(int c=0;c<4;c++)
  47.     {
  48.         ostringstream convert;
  49.         convert<<array[c];
  50.         codeArr[c]=convert.str();
  51.     }
  52. }
  53.  
  54. int game()
  55. {
  56.     int turnCount=1;
  57.     for(int c=0;c<10;c++)
  58.     {
  59.         string guess="";
  60.         int bullCount=0, cowCount=0;
  61.         cout<<"Attempt "<<turnCount<<endl;
  62.         cout<<"Enter your guess: "<<endl;
  63.         cin>>guess;
  64.         while(guess[0]==guess[1]||guess[0]==guess[2]||guess[0]==guess[3]||guess[1]==guess[2]||guess[1]==guess[3]||guess[2]==guess[3])
  65.         {
  66.             cout<<"Your guess should not have repeating digits! Try again."<<endl;
  67.             cout<<"Enter your guess: ";
  68.             cin>>guess;
  69.         }
  70.         if(c==9&&guess!=code)
  71.         {
  72.             cout<<"You ran out of attempts! Better luck next time! The code was "<<code<<endl;
  73.             return 0;
  74.         }
  75.         if(guess==code)
  76.         {
  77.             cout<<"Congratulations! You got the code in "<<turnCount<<" attempts! You win!"<<endl;
  78.             return 0;
  79.         }
  80.         turnCount++;
  81.         for(int c=0;c<4;c++)
  82.         {
  83.             if(guess[c]==code[c])
  84.             bullCount++;
  85.             int num=guess.find(codeArr[c]);
  86.             if(num!=-1)cowCount++;
  87.         }
  88.         cout<<"Bulls: "<<bullCount<<endl;
  89.         cout<<"Cows: "<<cowCount<<endl;
  90.         cout<<"-----------------"<<endl;
  91.     }
  92. }
  93.  
  94. int main()
  95. {
  96.     explain();
  97.     generateNumber();
  98.     game();
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement