Advertisement
moathon

Masermind C++ AI

Dec 13th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. /*
  2.     Mohammad Mohammad
  3.     CSC 7 Template for Mastermind AI
  4.     December 6th, 2019
  5.  */
  6.  
  7. //System Libraries
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <string>
  12. #include <iomanip>
  13. using namespace std;
  14.  
  15. //Function Prototypes
  16. string AI(char,char);
  17. bool eval(string,string,char &,char &);
  18. string set();
  19.  
  20. int main(int argc, char** argv) {
  21.     //Set the random number seed
  22.     srand(static_cast<unsigned int>(time(0)));
  23.    
  24.     //Declare variables
  25.     string code,guess;  //code to break, and current guess
  26.     char rr,rw;         //right digit in right place vs. wrong place
  27.     int nGuess;         //number of guesses
  28.    
  29.     //Initialize Values
  30.     nGuess=0;
  31.     code=set();
  32.     rr=rw=0;
  33.     cout<<"The code is "<<code<<endl;
  34.     //Loop until solved and count to find solution
  35.     do{
  36.        nGuess++;
  37.        guess=AI(rr,rw);
  38.     }while(eval(code,guess,rr,rw));
  39.     //Check evaluation
  40.     cout<<nGuess<<" "<<code<<" "<<guess<<endl;
  41.     guess=AI(rr,rw);
  42.     /*cout<<code<<endl;
  43.     for(int i=0;i<10000;i++){
  44.         guess=AI(0,0);
  45.         eval(code,guess,rr,rw);
  46.         cout<<setw(5)<<code<<setw(5)<<guess
  47.                 <<setw(2)<<static_cast<int>(rr)
  48.                 <<setw(2)<<static_cast<int>(rw)<<endl;
  49.     }*/
  50.    
  51.     //Exit the program
  52.     return 0;
  53. }
  54.  
  55. //string AI(char rr,char rw){
  56.    
  57. //}
  58.  
  59. string AI(char rr,char rw){
  60.     //Define helper functions here
  61.     void (*print)(string [],char [],char [],int,int)=
  62.         [] (string g[],char r[],char w[],int nb,int ne){
  63.             for(int i=nb;i<=ne;i++){
  64.                 cout<<g[i]<<" "
  65.                     <<static_cast<int>(r[i])<<" "
  66.                     <<static_cast<int>(w[i])<<endl;
  67.             }
  68.     };
  69.    
  70.     //Save the historical values of guesses and results
  71.     static const int SIZE=10000;//How many guesses to save
  72.     static string aGuess[SIZE]; //Save the guesses
  73.     static char grr[SIZE];      //Save right guess in right spot
  74.     static char grw[SIZE];      //Save right guess in wrong spot
  75.     static int guess=0;         //Increment the guess number
  76.     string sGuess="0000";       //Size the guest string
  77.    
  78.     //Store the results from the last guess
  79.     grr[guess]=rr;
  80.     grw[guess]=rw;
  81.    
  82.     //Test the helper function
  83.     if(rr==4)print(aGuess,grr,grw,
  84.             guess-10>0?guess-10:0,guess);
  85.    
  86.     //Calculate the guess
  87.     int n1000=(guess-guess%1000)/1000;
  88.     int n100=(guess-guess%100)/100-10*n1000;
  89.     int n10=(guess%100-guess%10)/10;
  90.     int n1=guess%10;
  91.     sGuess[0]=n1000+'0';
  92.     sGuess[1]=n100+'0';
  93.     sGuess[2]=n10+'0';
  94.     sGuess[3]=n1+'0';
  95.     aGuess[++guess]=sGuess;//Save the result
  96.    
  97.     //Return the result
  98.     return sGuess;
  99. }
  100.  
  101. bool eval(string code,string guess,char &rr,char &rw){
  102.     string check="    ";
  103.     rr=0,rw=0;
  104.     //Check how many are right place
  105.     for(int i=0;i<code.length();i++){
  106.         if(code[i]==guess[i]){
  107.             rr++;
  108.             check[i]='x';
  109.             guess[i]='x';
  110.         }
  111.     }
  112.     //Check how many are wrong place
  113.     for(int j=0;j<code.length();j++){
  114.         for(int i=0;i<code.length();i++){
  115.             if((i!=j)&&(code[i]==guess[j])&&(check[i]==' ')){
  116.                 rw++;
  117.                 check[i]='x';
  118.                 break;
  119.             }
  120.         }
  121.     }
  122.    
  123.     //Found or not
  124.     if(rr==4)return false;
  125.     return true;
  126. }
  127.  
  128. string set(){
  129.     string code="0000";
  130.     for(int i=0;i<code.length();i++){
  131.         code[i]=rand()%10+'0';
  132.     }
  133.     return code;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement