Simulation of the implementation of word guessing game, pure simulation, not involving the graphical interface, the comments are very detailed, although I write the code ugly, but I hope to provide you with help
#include <algorithm> #include <cstdlib> #include <cstring> #include <fstream> #include < ctime> #include <cmath> #include <iomanip> #include <windows.h>using namespace Std;int times=10;// global variable, default guess number typedef struct SAVE{CHAR word[30];//stores a single word}save;save a[100];//stores all read-in words, up to 100 typedef struct RANK{INT mc;// Rank char name[20];//name Double grade;//score char tmp[64];//create record date}rank;rank board[3];//leaderboard, save top three/function declaration int Readword ();// Read from the hard disk the word void Readrank ();//read into the score record void Caidan ();//print menu void setting ();//Menu 1 Set the number of guesses limit void Toprecord ();//print leaderboard void record ( );//Writes the board rank to hard disk void PlayGame (int n);//Menu 0 for game int main () {System ("color F0"), int n;//to store the total number of words, so that play randomly selects the word number printf (" Welcome into the game! \ n "); N=readword (); Readrank ();//Read score record while (1) {int T;system (" CLS ");//Keep the interface neat Caidan (); scanf ("%d ", &t); switch (t) {case 0:{playgame (n); Case 1:{setting (); Case 2:{system ("CLS"); Toprecord ();p rintf (""); System ("pause");//Pause break;} Case 3:{record ();//boar before exitingD data written to HDD printf ("************* Heroes Goodbye *************\n"); exit (0);}} return 0;} int Readword ()//reads from hard disk the word {char character;//stores a single read-in character int i=0,j=0;//loop variable ifstream ifile ("Word.txt");//File stream if (!ifile) { printf ("Word.txt Can't open!" \ n "); return 0;} while (Ifile.get (character))//reads the characters in the file into the save struct for PlayGame random selection {if (character!= ') {a[i].word[j]=character;j++;} else{i++;j=0;}} Ifile.close (); return i;//returns the number of read-in words}void readrank ()//reads from the hard disk rank {Char character;//stores a single read-in character Char temp[100];//temporarily positions all information in a row , wait for the int i=0,j=0,k,l;//loop variable double xiaoshu;//to handle the fractional Ifstream ifile ("Rank.txt") of the score in temp[100];//File stream if (!ifile) {printf (" Rank.txt Can't open it! \ n "); return;} while (Ifile.get (character))//reads the characters in the file into the temp string {if (character!=10) {temp[i]=character;i++;} Else{board[j].mc= (temp[0]-48);//read a line to handle temp, convert string to record information stored in board for (k=2;temp[k]!= "; k++) {board[j].name[k-2] =TEMP[K];} K++;for (; k<i;k++) {if (temp[k]== '. ') {for (l=k-1;temp[l]!= '; l--)//string converted to decimal board[j].grade= ((temp[l]-48) *pow (10,K-1-L) xiaoshu= ((temp[k+1]-48) *0.1+ (temp[k+2]-48) *0.01+ (temp[k+3]-48) *0.001); board[j].grade+=xiaoshu;break;}} For (l=k+5;l<= (k+23); l++) board[j].tmp[l-k-5]=temp[l];//date j++;i=0;}} Ifile.close ();} void Caidan ()//print menu {int i;printf ("");//Print graphical interface for (i=0;i<34;i++) printf ("*");p rintf ("\ n");p rintf (" * *\n ");p rintf (" * Menu *\n ");p R intf ("* 0 Playing Game *\n");p rintf ("* 1 Setting *\n ");p rintf (" * 2 Top Records *\n ");p rintf (" * 3 Exit *\n ");p rintf (" * *\n ");p rintf (" "); fo R (i=0;i<34;i++) printf ("*");p rintf ("\ n");p rintf ("*********** input option number ***********\n");p rintf (" ");} void setting ()//menu 1 Set the number of guessing limits {printf (* * * *Please set the number of guesses **********\n ");p rintf (" "); scanf ("%d ", x);} void Toprecord ()//print leaderboard {int i;//loop variable printf ("\ n");//Print graphical interface for (i=0;i<59;i++) printf ("*");p rintf ("\ n");p rintf (" * *\n ");p rintf (" * heroes ' list *\n "); for (i=0;i<3;i++) {if (board[i].grade==0) {printf (" *%d%-10s%-8.3 F ", Board[i].mc,board[i].name,board[i].grade); printf ("0000/00/00 00:00:00 *\n");} else{printf ("*%d%-10s%-8.3f%s", board[i].mc,board[i].name,board[i].grade,board[i].tmp); printf ("*\n");}} printf ("* *\n"); printf (""); for (i=0;i<59;i++) printf ("*");p rintf ("\n\n\n");} void record ()//writes the board rank to the hard disk {int i;//loop variable ofstream ofile ("Rank.txt");//File stream if (!ofile) {printf ("Rank.txt not can open \ n ");} Else{for (i=0;i<3;i++)//write the information in board to the hard disk {OFIle<<board[i].mc<< "" <<board[i].name<< "<<fixed<<setprecision (3) << board[i].grade<< "" <<board[i].tmp<<endl;}} void PlayGame (int n)//menu 0 for game {int i,j;//i,j used to loop int Flag;//flag Mark whether to guess the letter int temp;//store random word number int len;//guess word length int correct, wrong;//statistics the number of pairs and errors char ch[30];//guessed the word char guess;//guessed the letter char tt= ' Y ';//whether continue to guess Double grade;//record score char compare[50];// Compared to the original word while ((tt== ' Y ') | | (tt== ' y ')) T for 1 is to continue guessing {printf ("************* game starts *************\n"), correct=0;wrong=0;//the number of pairs of errors before each guess Srand (unsi gned) Time (NULL)); Temp=rand ()% (n+1);//Random Value strcpy (Ch,a[temp].word); Len=strlen (ch); memset (Compare, '-', sizeof (CH) + 1);//Initialize the string as "-" printf (""); for (j=0;j<len;j++) printf ("-");p rintf ("\ n"), for (i=0;i<times;i++)//Not up to the number of times limit System cycle Guessing {flag=0;printf (""); scanf ("%s", &guess), for (j=0;j<len;j++) {if (Guess==ch[j]) {compare[j]=guess ; flag=1;} if (ch[j]== (guess+32)) {compare[j]= (guess+32); flag=1;}} if (flag)//flag is 1, guess correctly {printf (" Guess right, there are%d chances \ n ", times-i-1); printf (""); for (j=0;j<len;j++)//output guessed position and corresponding letter printf ("%c", Compare[j]);p rintf ("\ n"); for (j=0;j<len;j++)//Check whether the letters are all guessed if (compare[j]== '-') break;if (J==len)//letters all guessed {Setconsoletextattribute ( GetStdHandle (std_output_handle), background_intensity | foreground_intensity | background_red | Background_green | Background_blue | foreground_red);p rintf ("Hero you are so good!") \ n ");//output color changes to red Setconsoletextattribute (GetStdHandle (std_output_handle), background_intensity | background_red | Background_green | Background_blue);//output becomes original color break;}} else{printf ("Guess error, there are%d chances \ \", times-i-1); wrong++;}} for (j=0;j<len;j++)//In contrast to the correct answer if (compare[j]== '-') break;if (J>=len) correct= (I+1-wrong);//correct number of times is not convenient for direct statistics, Therefore, the total number of errors elsecorrect= (I-wrong);p rintf ("The end of this round!" Correct number of times:%d errors:%d\n ", Correct,wrong); if (i==times)//Only in the case of the last round of the wrong guess will meet this judgment condition printf (" Victory and defeat is the common military, Heroes please come again! ") \ n "); else{grade= (i+1.0)/len;printf (" Your score is%.3f\n ", grade); time_t t = time (0); Get record time char tmp[64];//storage time strftime (TMP, sizeof (TMP), "%y/%m/%d%x", LocalTime (&t)); if (Grade<board[2].grade || board[2].grade==0)//board leaderboard Update {setconsoletextattribute (GetStdHandle (std_output_handle), BACKGROUND_INTENSITY | foreground_intensity | background_red | Background_green | Background_blue | foreground_red);p rintf ("Congratulations on entering the Heroes list, please enter your name: \ n");//The output turns red Setconsoletextattribute (GetS Tdhandle (std_output_handle), background_intensity | background_red | Background_green | Background_blue);//Output restore if (grade<board[1].grade| | board[1].grade==0)//number less, manually update the leaderboard if (grade<board[0].grade| | board[0].grade==0) {board[2]=board[1];board[1]=board[0];board[0].grade=grade;scanf ("%s", &board[0].name); strcpy (board[0].tmp,tmp);} ELSE{BOARD[2]=BOARD[1];BOARD[1].GRADE=GRADE;SCANF ("%s", &board[1].name); strcpy (board[1].tmp,tmp);} ELSE{BOARD[2].GRADE=GRADE;SCANF ("%s", &board[2].name); strcpy (board[2].tmp, TMP);} board[0].mc=1;board[1].mc=2;board[2].mc=3;}} printf ("Do you want to continue?" (y/n) \ n "); scanf ("%s ", &tt); System (" CLS ");//Clear Screen}}
Analog Guess Word Game