@StandardOil ecco a te!
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <iostream>
using namespace std;
char car;
int g,game; //counter of games and the arguments for called function check input (number)
float t_dif; //global variables for time
time_t ini,fin;
/*P R O T O T Y P E */
// H E A D E R O F G O T O X Y F U N C T I O N
void gotoxy( short column, short line );
void reset_game(); //reset game at the end of one play
int esc_function(); //function to manage the esc botton
void read_input(string outstr, char& car);//input check
void continuous(); //function to continue with the game
void about();// information about the program and its creators
/*******M A I N F U N C T I O N *******/
int main(){
int i,j,c,n,x; //counters
int n_attempts,sec,strike,score;
const int n_max_attempts=10,score_max=1000,attempt_max=60;
char solution[4],attempt[4],random,temporary[4];
game=1,i=0,c=0,j=0;
gotoxy(40,2);
cout<<"---MASTER MIND---"<<endl;
read_input(" Insert 1 to 9 for number of games (0==10), ",car);
do{
n_attempts=0; //set number of attempts
system("cls");
gotoxy(40,2);
cout<<" MISTER MIND by anonymous ";
/***T H I N K T H E S O L U T I O N***/
srand(time(0));
for(i=0;i<4;i++){
random=rand()%6+65;
solution[i]=random;}
//S T A R T P L A Y
gotoxy(10,4);
cout<<" Game number "<<game<<" of "<<g;
n=6;gotoxy(15,n);//set the position to put the attempt
ini=time(NULL); //Set time NULL
/// Loading the attempt array
do{
for(i=0;i<4;i++){
temporary[i]=solution[i];}
do{
car=getch();
fin=time(NULL); sec=difftime(fin,ini);
if(car>=65 and car<=70 and c!=4 ){ putch(car);attempt[c]=car;c=c+1;}
if(car>=97 and car<=102 and c!=4 ){car=car-32;putch(car); attempt[c]=car; c=c+1;}//If the user hasn't activeted the block maiusc
if (car==8 and c>0 ) {putch(8);putch(32);putch(8);c=c-1;}
}while((car!=13 or c!=4)and(car!=27));
strike=0;n_attempts=n_attempts+1;
cout<<" ";
for (int i=0 ; i<4; i++){
if (attempt[i] == temporary[i]){
putch('X');
attempt[i]='0'; // avoiding count like a bal
temporary[i]='1';///avoiding reuse the char
strike=strike+1;}
}
for (int x=0 ; x<4; x++){
for (int j=0 ; j<4; j++){
if (attempt[x] == temporary[j]){
putch('O');
attempt[x]='0'; //avoiding double count
temporary[j]='1';} ///avoiding reuse the char
}
}n=n+1;gotoxy(15,n); c=0;
}while(car!=27 and strike!=4 and n_attempts<n_max_attempts );
score=score_max-sec;
score=score-n_attempts*attempt_max;
if(n_attempts==n_max_attempts) score=0;
if(score<0) score=0;
if(car==27) score=0;
gotoxy(50,8);
cout<<" ---SCORE TABLE--- ";
gotoxy(48,10);
cout<<" game tries sec score ";
gotoxy(48,12);
cout<<" "<<game<<" "<<n_attempts<<" "<<sec<<" "<<score;
if(strike==4){cout<<"\n\n ***YOU WIN*** ";putch(7);}
cout<<"\n *** ";
for(j=0;j<4;j++){
printf("%c",solution[j]);} cout<<" *** ";
reset_game();
}while(car!=27 and car==13 );
return 0;
}
/*F U N C T I O N S*/
void gotoxy( short column, short line ){ //position of the text on screen 25 lines and 80 columns
COORD pos={column,line};
SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),pos);}
void reset_game(){
gotoxy(28,26);
printf("Click the key to continue-->");
do{
car=getch();
}while(car==0 and car!=27);//loops car!=0 and car== esc
esc_function();game=game+1;
if(game>g){system("cls");read_input(" Insert 1 to 9 for number of games, ",car);game=1;}
}
int esc_function(){if(car==27){{system("cls");gotoxy(40,15);cout<<"----Thanks to played! bye!---- ";ini=time(NULL);do{ fin=time(NULL); t_dif=difftime(fin,ini);} while(t_dif<3);exit(true);return (0);}}} //function to manage the esc botton
void read_input(string outstr, char& car){
int numCifre=0;
gotoxy(40,2);
cout<<"---MASTER MIND---"<<endl;
gotoxy(10,5);
cout<<outstr;
gotoxy(10,6);
cout<<" Insert h for more information about the program";
gotoxy(10,7);
cout<<" ===> ";
do{
car=getch();
if(car==48 and numCifre!=1){numCifre++;putch(car);g=(int)car-48; g=g+10;}// 0==10 games
if (car>=49 and car<=57 and numCifre!=1) {numCifre++;putch(car);g=(int)car-48;} //numCifre allows one caracters
if (car==8 and numCifre>0) {numCifre--;putch(8);putch(32);putch(8);} //8==back space, 32==spazio
}while (car!=13 and car!=27 and car!='h' and car!='H' ); // 13==invio 27==esc
if(car=='h' or car=='H') about();
esc_function();
}
void continuous(){
gotoxy(28,26);
printf("Click the key to continue-->");
do{
car=getch();
}while(car==0 and car!=27);//loops car!=0 and car== esc
esc_function();
system("cls");read_input(" Insert 1 to 9 for number of games, ",car);}
void about(){ // information about the program and their creators
system("cls");
gotoxy(40,1);
cout<<" RULES OF THE GAME ";
cout<<"\n The Master Mind game consists of deciphering a secret code consisting of four letters, ";
cout<<"\n arranged in a random way by the computer";
cout<<"\n At each attempt the computer responds:";
cout<<"\n with strike (X) if one or more letters have been guessed and are in the correct position,";
cout<<"\n with bool (O) if one or more letters have been guessed but are in the wrong position.";
cout<<"\n If it does not answer with any of the above options,";
cout<<"\n it means that the letters of the created combination are not part of the solution combination.";
cout<<"\n Thinking about these answers to the combinations hypothesized in the various attempts,";
cout<<"\n you should be able to decipher the code in less than ten attempts: otherwise the game is lost.";
gotoxy(40,11);
cout<<"\n ***EXAMPLE OF ONE GAME*** ";
cout<<"\n You can use 6 different letters: A,B,C,D,E,F. The max lenght is 4 characters ";
cout<<"\n Click Esc for exit from the game, and single play ";
cout<<"\n The score is calculated taking into account the number of attempts and the time taken";
cout<<"\n EXAMPLE SOLUTION: FAAD ";
cout<<"\n 1296 possible configurations in the attempt N.1--> BCFD XO (1 strikes and 1 bal)";
cout<<"\n 252 possible configurations in the attempt N.2--> FFDD XX (2 strikes and 0 bal)";
cout<<"\n 16 possible configurations in the attempt N.3--> BDBB X (1 strikes and 0 bal)";
cout<<"\n 8 possible configurations in the attempt N.4--> BCFD XOO (1 strikes and 2 bals)";
cout<<"\n 1 possible configurations in the attempt N.5--> FAAD XXXX ***SOLUTION!*** (4 strikes and 0 bal)"<<endl;
cout<<"\n ***The game was created by anonymous *** ";
continuous();}