Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Mohammad Mohammad
- CSC 7 Template for Mastermind AI
- December 6th, 2019
- */
- //System Libraries
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- #include <iomanip>
- using namespace std;
- //Function Prototypes
- string AI(char,char);
- bool eval(string,string,char &,char &);
- string set();
- int main(int argc, char** argv) {
- //Set the random number seed
- srand(static_cast<unsigned int>(time(0)));
- //Declare variables
- string code,guess; //code to break, and current guess
- char rr,rw; //right digit in right place vs. wrong place
- int nGuess; //number of guesses
- //Initialize Values
- nGuess=0;
- code=set();
- rr=rw=0;
- cout<<"The code is "<<code<<endl;
- //Loop until solved and count to find solution
- do{
- nGuess++;
- guess=AI(rr,rw);
- }while(eval(code,guess,rr,rw));
- //Check evaluation
- cout<<nGuess<<" "<<code<<" "<<guess<<endl;
- guess=AI(rr,rw);
- /*cout<<code<<endl;
- for(int i=0;i<10000;i++){
- guess=AI(0,0);
- eval(code,guess,rr,rw);
- cout<<setw(5)<<code<<setw(5)<<guess
- <<setw(2)<<static_cast<int>(rr)
- <<setw(2)<<static_cast<int>(rw)<<endl;
- }*/
- //Exit the program
- return 0;
- }
- //string AI(char rr,char rw){
- //}
- string AI(char rr,char rw){
- //Define helper functions here
- void (*print)(string [],char [],char [],int,int)=
- [] (string g[],char r[],char w[],int nb,int ne){
- for(int i=nb;i<=ne;i++){
- cout<<g[i]<<" "
- <<static_cast<int>(r[i])<<" "
- <<static_cast<int>(w[i])<<endl;
- }
- };
- //Save the historical values of guesses and results
- static const int SIZE=10000;//How many guesses to save
- static string aGuess[SIZE]; //Save the guesses
- static char grr[SIZE]; //Save right guess in right spot
- static char grw[SIZE]; //Save right guess in wrong spot
- static int guess=0; //Increment the guess number
- string sGuess="0000"; //Size the guest string
- //Store the results from the last guess
- grr[guess]=rr;
- grw[guess]=rw;
- //Test the helper function
- if(rr==4)print(aGuess,grr,grw,
- guess-10>0?guess-10:0,guess);
- //Calculate the guess
- int n1000=(guess-guess%1000)/1000;
- int n100=(guess-guess%100)/100-10*n1000;
- int n10=(guess%100-guess%10)/10;
- int n1=guess%10;
- sGuess[0]=n1000+'0';
- sGuess[1]=n100+'0';
- sGuess[2]=n10+'0';
- sGuess[3]=n1+'0';
- aGuess[++guess]=sGuess;//Save the result
- //Return the result
- return sGuess;
- }
- bool eval(string code,string guess,char &rr,char &rw){
- string check=" ";
- rr=0,rw=0;
- //Check how many are right place
- for(int i=0;i<code.length();i++){
- if(code[i]==guess[i]){
- rr++;
- check[i]='x';
- guess[i]='x';
- }
- }
- //Check how many are wrong place
- for(int j=0;j<code.length();j++){
- for(int i=0;i<code.length();i++){
- if((i!=j)&&(code[i]==guess[j])&&(check[i]==' ')){
- rw++;
- check[i]='x';
- break;
- }
- }
- }
- //Found or not
- if(rr==4)return false;
- return true;
- }
- string set(){
- string code="0000";
- for(int i=0;i<code.length();i++){
- code[i]=rand()%10+'0';
- }
- return code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement