Nippolo ha scritto:
@Dany30 se posti il codice completo e compilabile diventa più semplice aiutarti.
PREMESSA: DEVO CONCLUDERE ALCUNE COSE, MA PER QUANTO RICHIESTO PENSO CHE QUESTO VADA BENE!
Vabbene 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,number; //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 );
int esc_function(); //function to manage the esc botton
void read_input(string outstr, int& num, 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 game,n_attempts,sec,temporary[4];
const int n_max_attempts=10;
char solution[4],attempt[4],random,strike,bal;
game=1,i=0,c=0;
gotoxy(40,2);
cout<<"---MASTER MIND---"<<endl;
read_input(" Insert 1 to 9 for number of games (0==10), ",number,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;
cout<<"\n";
for(i=0;i<4;i++){ //try to print the solution
printf("%c",solution[i]);}
n=6;gotoxy(15,n);//set the position to put the attempt
ini=time(NULL); //Set time NULL
/// Loading the attempt array
do{
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 users 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 ( i = 0 ; i<4; i++){
if (attempt[i] == solution[i]){
putch('X');
temporary[i] = 1; // avoid counting like bal
strike=strike+1;
}
}
for (x = 0 ; x<4; x++){
for (j = 0 ; j<4; j++){
if (attempt[x] == solution[j] and temporary[j]==0){
putch('O');
temporary[x] = 1; // avoid the double count
}
}
}n=n+1;gotoxy(15,n); c=0;
}while(car!=27 and strike!=4 and n_attempts<n_max_attempts+1 );
if(strike==4){cout<<"\n ***YOU WIN*** "<<endl;cout<<sec;}
continuous();
game=game+1;
}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);}
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, int& num, char& car){
int numCifre=0; num=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--;num=num/10;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, ",number,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();}