#include <stdio.h>//scanf(),printf(),fflush(),getchar(stdin),fopen(),fclose(),fgetc(),fgets(),
#include <stdlib.h>//system(),malloc(), free(),
#include <ctype.h>//toupper(),
#include <string.h>//strcmp(),memset(),strcpy();strcat(),
#include <time.h>//time(),
#include <windows.h>//Sleep()
struct account{
char name[20];
char code[12];
char history[20];
};//creat a struct
//the function about the hangman game
int randomNum(int maxNum);//a function of random number
int chooseWord(char *wordChosen);//choose the word radomly from dictionary.txt
int win(int letterFound[], long wordSize);//judge whether the game win
int researchLetter(char letter, char secretWord[], int letterFound[]);// Find the letter entered by the user in the word to be guessed
char readCharacter();//get the word from stdin
void printBody(int mistakes, char* body);//print the hangman body
void selfguess();//enter a single word and guess it
//there welcome menu
void welcomemenu1();//the first menu
void welcomemenu2();//the second menu
void welcomemenu3();//the third menu
int back();//Exit the entire program
int create();//create a new account
int login(void);//return flag 1 and 0
void clear();//clean the history
void review();//review the history
void history(char*ptr);//store the game history
int main()
{
char letter = 0; // tester's input
char secretWord[100] = {'\0'}; // the words in dictionary.txt randomly
int *letterFound = NULL; // the pointer is used to compare tester's input with the secretword,like a temp
int leftTimes = 8; // the times we left
int i = 0;
long wordSize = 0; // the length of secretword
int flag1=0,flag2=0,flag3=0;//to control the while loop
int ch1=0;//three choice we will make
int ch2=0;
int ch3=0;
char ptr[10]="";
char* body;
//Initialise the body pointer
body=(char*)malloc(100);
memset(body,0,100);
while(flag1 == 0)
{
welcomemenu1();//call the first menu
scanf("%d",&ch1);
switch(ch1)
{
case 1 : flag1=login();
break;
case 2 : flag1=create();
break;
case 3 : back();
break;
default : printf("your input is wrong,please input again\n");
}
fflush(stdin);
Sleep(2000);
system("cls");
}
while(flag2 == 0)
{
welcomemenu2();
scanf("%d",&ch2);
switch(ch2)
{
case 1 : flag2=1;
break;
case 2 : review();
break;
case 3 : clear();//clean the history
break;
case 4 : back();//log out and memary the win and lose
break;
default : printf("your input is wrong,please input again\n");
}
fflush(stdin);
Sleep(2000);
system("cls");
}
while (1)
{
while (flag3 == 0)
{
welcomemenu3();
scanf("%d", &ch3);
switch (ch3)
{
case 1 : flag3 = 1;
break;
case 2 : selfguess();
break;
case 3 : back();//log out and memary the win and lose
break;
default : printf("your input is wrong,please input again\n");
}
fflush(stdin);
Sleep(2000);
system("cls");
}
//we enter the game
// find a word from the FILE(dictionary) radomly
if (!chooseWord(secretWord))
return 0; // log out the game
// find the length of the word you chose
wordSize = strlen(secretWord);
letterFound = (int*)malloc(wordSize * sizeof(int));
if (letterFound == NULL)
return 0;
// let all Boolean value be 0£¬which means there is no letter that has been guessed
for (i = 0; i < wordSize; i++)
letterFound[i] = 0;
// if we have chance, continue
while (leftTimes > 0 && !win(letterFound, wordSize))
{
printf("\n\nyou can try %d times", leftTimes);
printf("\nWhat's the secret word ? ");
/* We show the guessed words and indicate the letters that have not been guessed with *
eg. *O**LE */
for (i = 0; i < wordSize; i++)
{
if (letterFound[i])
printf("%c", secretWord[i]);
else
printf("*");
}
printBody(leftTimes, body);//print the hang man
printf("\ninput a letter : ");
letter = readCharacter();//get all topper letter
// if the letter user input didn't exist
if (!researchLetter(letter, secretWord, letterFound))
{
leftTimes--;
}
}
//struct account tester;
if (win(letterFound, wordSize))
{
printf("\n\nwelldone! the word is : %s\n", secretWord);
strcpy(ptr, "win");
history(ptr);
}
else
{
printf("\n\nfailure! the word is : %s\n", secretWord);
strcpy(ptr, "failure");
history(ptr);
}
free(letterFound);
//fclose(fptr);
}
return 0;
}
// determine whether the user win or not
int win(int letterFound[], long wordSize)
{
int i = 0;
int win = 1; // 1 is success£¬0 is failure
for (i = 0 ; i < wordSize ; i++)
{
if (letterFound[i] == 0)
win = 0;
}
return win;
}
// Find the letter entered by the user in the word to be guessed
int researchLetter(char letter, char secretWord[], int letterFound[])
{
int i = 0;
int correctLetter = 0;// 0 means the letter you input is not in the word£¬1 means it is in
// Traversing an array of secretWord to determine whether the word is in the array
for (i = 0 ; secretWord[i] != '\0' ; i++)//read the secret until meet '\0'
{
if (letter == secretWord[i])
{
correctLetter = 1;
letterFound[i] = 1; // For all array positions equal to the guessed letter, the value is changed to 1
}
}
return correctLetter;
}
char readCharacter()
{
char character = 0;
fflush(stdin);
character = getchar(); // read the letter(stdin)
character = toupper(character); // turn the character to upper case
// read the word from the keyboard until '\n',to stop
while (getchar() != '\n')
;//Indicates that the loop has an empty body, i.e. it only makes judgements and loops and does not perform operations.
return character; // Return the first letter read
}
//choose the word radomly from dictionary.txt
int chooseWord(char *wordChosen)
{
int wordNum = 0; // word numbers in the dictionary.txt
int chosenWordNum = 0; // Number the selected words
int characterRead = 0; // the letter we chose from the file
FILE* pdictionary=NULL; // declear a FILE pointer*pdictionary
pdictionary = fopen("dictionary.txt", "r");
if (pdictionary == NULL) // if we cannot open the file successfully
{
printf("\ncannot upload the dictionary\n");
return 0; //exit
}
do
{
characterRead = fgetc(pdictionary);
if (characterRead == '\n')
wordNum++;
} while (characterRead != EOF);//until at the end of the file
chosenWordNum = randomNum(wordNum); // Select a random word (numbered)
// We read from the beginning of the file again (rewind function) until we come across the selected word
rewind(pdictionary);
while (chosenWordNum > 0)
{
characterRead = fgetc(pdictionary);
if (characterRead == '\n')
chosenWordNum--;
}
/* The file pointer is already pointing to the right place, so we use fgets to read that line (i.e. the selected word)*/
fgets(wordChosen, 100, pdictionary);
wordChosen[strlen(wordChosen) - 1] = '\0';
fclose(pdictionary);
return 1;
}
int randomNum(int maxNum)
{
srand(time(NULL));
return (rand() % maxNum);
}
void printBody(int leftTimes, char* body)
{
printf("\tleft times :%d\n", leftTimes);
switch(leftTimes)
{
case 7: body[6] = '\\'; break;
case 6: body[5] = '/'; break;
case 5: body[4] = '\\'; break;
case 4: body[3] = '|'; break;
case 3: body[2] = '/'; break;
case 2: body[1] = ')';break;
case 1: body[0] = '('; break;
default: break;
}
printf("\t _________\n"
"\t| |\n"
"\t| %c %c\n"
"\t| %c%c%c\n"
"\t| %c %c\n"
"\t| \n"
"\t| \n"
"\t--------------\n", body[0], body[1], body[2],
body[3], body[4], body[5], body[6]);
}
void selfguess()
{
char*ptr;
char letter = 0; // tester's input
char secretWord[100] = {0}; // the words in dictionary.txt randomly
int *letterFound = NULL; // the pointer is used to compare tester's input with the secretword,like a temp
int leftTimes = 7; // the times we left
int i = 0;
long wordSize = 0; // the length of secretword
char* body;
body=malloc(100);//Initialise the body pointer
memset(body,0,100);
printf("\nplease input all letters into uppercase \n");
scanf("%s",secretWord);
// find the length of the word you chose
wordSize = strlen(secretWord);
letterFound = (int*)malloc(wordSize * sizeof(int));
if (letterFound == NULL)
return;
// let all Boolean value be 0£¬which means there is no letter that has been guessed
for (i = 0 ; i < wordSize ; i++)
letterFound[i] = 0;
// if we have chance, continue
while (leftTimes > 0 && !win(letterFound, wordSize))
{
printf("\n\nyou can try %d times", leftTimes);
printf("\nWhat's the secret word ? ");
/* We show the guessed words and indicate the letters that have not been guessed with *
eg. *O**LE */
for (i = 0 ; i < wordSize ; i++)
{
if (letterFound[i])
printf("%c", secretWord[i]);
else
printf("*");
}
printBody(leftTimes,body);//print the hang man
printf("\ninput a letter : ");
letter = readCharacter();//get all topper letter
// If the letter user input does not exit
if (!researchLetter(letter, secretWord, letterFound))
{
leftTimes--; // left times will be cut 1
}
}
if (win(letterFound, wordSize))
{
printf("\n\nwelldone! the word is : %s\n", secretWord);
strcpy(ptr,"win");
history(ptr);
}
else
{
printf("\n\nfailure! the word is : %s\n", secretWord);
strcpy(ptr,"failure");
history(ptr);
}
free(letterFound);
return;
}
void welcomemenu1()
{
printf("welcome to hangman\n");
printf("please input your choice\n");
printf("The input should be number 1,2 and 3,others are illegal\n");
printf("\t1.Login to your account\n");
printf("\t2.create new account\n");
printf("\t3.quit\n");
}
void welcomemenu2()
{
printf("please input your choice\n");
printf("The input should be number 1,2,3 and 4,others are illegal\n");
printf("\t1.start a new game\n");
printf("\t2.review your game history\n");
printf("\t3.clear your game history\n");
printf("\t4.logout\n");
}
void welcomemenu3()
{
printf("please input your choice\n");
printf("The input should be number 1,2 and 3,others are illegal\n");
printf("\t1.start a new game from dictionary.txt randomly\n");
printf("\t2.start a new game for another user to enter a secret word\n");
printf("\t3.logout\n");
}
int back()
{
printf("Goodbye\n");
exit(0);
}
int create() /*function create definition*/
{
char filename[25]="";
struct account tester; /*declare a structure*/
char code1[12],code2[12];
FILE *fptr; /*declare a file pointer*/
fflush(stdin);
system("cls");
printf("Please enter the name of the player\n");
printf("the length of the name should be less than 19 digits:\n\n\t"); /*Obtain each structure member information checking each for validity*/
while (fgets(filename,19,stdin) == NULL)
{
fflush(stdin);
printf("Something went wrong with the input, please try again:\n\n\t");
}
filename[strlen(filename)-1]='\0';
fflush(stdin);
strcpy(tester.name,filename);//copy filename and send it into the struct tester
printf("%s",filename);
if((fptr=fopen(filename,"rb"))==NULL)//when we find there is no exit input filename
{
if((fptr=fopen(filename,"wb"))==NULL)//create a new file
{
printf("cannot establish the file\n");
return 0;
}
fclose(fptr);
}
else
{
fclose(fptr);
printf("The username already exists please try again");
return 0;
}
int m=1;//to control the while loop
while(m==1)
{
printf("Please enter the code:\n\t");
printf("Warning: no space!!!\n");
while (scanf("%s", code1) == 0)
{
fflush(stdin);
printf("Please try again and make sure the number has less than 12 digits");
}
printf("Please enter the same code again:\n\n\t");
while (scanf("%s", code2) == 0)
{
fflush(stdin);
printf("Please try again and make sure the number has less than 12 digits");
}
if(strcmp(code1,code2))
{
printf("two inputs are not the same, please try again");
}
else
{
printf("welldone\n");
strcpy(tester.code,code1);
m=0;
}
}
fflush(stdin);
/*Once all of the member information has been collected, the file is opened ready to append the new entry at the end of the file*/
fptr = fopen(filename, "wb+");
if (fptr == NULL) /*Test the success of opening the file*/
{
printf("\n\nThere was a problem opening the file, please restart the program\n\n");
Sleep(2000);
return 0;
}
else /*On successfully opening the file the structure is written to the file*/
{
strcpy(tester.history, "");
fwrite(&tester, sizeof(struct account), 1, fptr);
fclose(fptr); /*Close the file*/
return 1;
}
return 0;
}
//a fuction used to log in an exist account
int login(void)
{
fflush(stdin);
char filename[25]="";
struct account a,b;//b is a buffer struct to get the content in the file
//a is to store the information we input
FILE *fptr;//declear the struct account pointer
printf("welcome to login menu\n");
printf("please input the account name:\n");
while (fgets(filename,19,stdin) == NULL)//input the name
{
fflush(stdin);
printf("Something went wrong with the input, please try again:\n\n\t");
}
filename[strlen(filename)-1]='\0';
strcpy(a.name,filename);//copy the name we just input to filename
fptr=fopen(filename,"rb");
if (fptr == NULL)
{
printf("This account does not exist");
return 0;
}
fread(&b,sizeof(struct account),1,fptr);//write fptr's content to account b
printf("please input the code\n");
scanf("%s",a.code);
while (1)
{
if(strcmp(a.code,b.code)==0)
{
fclose(fptr);
printf("welldone, welcome to play hangman\n");
Sleep(500);
return 1;
}
else
{
printf("the code is wrong, please input again\n");
scanf("%s",a.code);
}
}
return 0;
}
void review()
{
fflush(stdin);
char filename[25]="";
struct account a,b;//b is a buffer struct to get the content in the file
//a is to store the information we input
FILE *fptr;//declear the struct account pointer
printf("\nplease input the account name:\n");
while (fgets(filename,19,stdin) == NULL)//input the name
{
fflush(stdin);
printf("Something went wrong with the input, please try again:\n\n\t");
}
filename[strlen(filename)-1]='\0';
strcpy(a.name,filename);//copy the name we just input to filename
fptr=fopen(filename,"rb");
if (fptr == NULL)
{
printf("This account does not exist");
return 0;
}
fread(&b,sizeof(struct account),1,fptr);//write fptr's content to account b
//judge whether the name existence or not
printf("please input the password\n");
scanf("%s",a.code);
fflush(stdin);
//judge whether the password right or not
while (1)
{
if(strcmp(a.code,b.code)==0)
{
fclose(fptr);
printf("welldone, you can review your game history\n");
Sleep(500);
break;
}
else
{
printf("the password is wrong, please input again\n");
scanf("%s",a.code);
}
}
fflush(stdin);
if (b.history[0] == '\0')
printf("You do not have any history to print\n");
else
printf("%s\n",b.history);
}
void clear()
{
fflush(stdin);
char filename[25]="";
//a is to store the information we input
//b is a buffer struct to get the content in the file
struct account a,b;
FILE *fptr;//declear the struct account pointer
printf("\nplease input the account name:\n");
while (fgets(filename,19,stdin) == NULL)//input the name
{
fflush(stdin);
printf("Something went wrong with the input, please try again:\n\n\t");
}
filename[strlen(filename)-1]='\0';
strcpy(a.name,filename);//copy the name we just input to filename
fptr=fopen(filename,"rb");
if (fptr == NULL)
{
printf("This account does not exist");
return 0;
}
fread(&b,sizeof(struct account),1,fptr);//write fptr's content to account b
fflush(stdin);
//judge whether the name existence or not
printf("please input the code\n");
scanf("%s",a.code);
fflush(stdin);
//judge whether the password right or not
while (1)
{
if(strcmp(a.code,b.code)==0)
{
fclose(fptr);
printf("welldone, you can clean your game story\n");
Sleep(500);
break;
}
else
{
printf("the code is wrong, please input again\n");
scanf("%s",a.code);
}
}
strcpy(b.history, "");
fptr=fopen("filename","wb");
fwrite(&b,sizeof(struct account),1,fptr);
fclose(fptr);
printf("Now you have clean the history\n");
return;
}
void history(char*ptr)
{
struct account tester;
fflush(stdin);
int option=0;
char filename[25]="";
//a is to store the information we input
//b is a buffer struct to get the content in the file
struct account a,b;
FILE *fptr;//declear the struct account pointer
printf("\n If you want to memory the history, input 1 and do as follows\n");
printf("If not,input 0 to exit the progrem\n ");
while (scanf("%d", &option) != 1 ||(option<0||option>1)) /* Display menu and get user entry - check user entry validity*/
{
printf("Invalid entry\n\n");
Sleep(2000);
fflush(stdin);
system("cls");
}
if(option==0)
back();
else if(option==1)
{
fflush(stdin);
printf("please input the account name:\n");
while (fgets(filename,19,stdin) == NULL)//input the name
{
fflush(stdin);
printf("Something went wrong with the input, please try again:\n\n\t");
}
filename[strlen(filename)-1]='\0';
strcpy(a.name,filename);//copy the name we just input to filename
fptr=fopen(filename,"rb");
if (fptr == NULL)
{
printf("This account does not exist");
return 0;
}
fread(&b,sizeof(struct account),1,fptr);//write fptr's content to account b
fclose(fptr);
//judge whether the name existence or not
fflush(stdin);
printf("please input the code\n");
scanf("%s",a.code);
fflush(stdin);
//judge whether the password right or not
while (1)
{
if(strcmp(a.code,b.code)==0)
{
printf("welldone, you can memory your game history\n");
Sleep(500);
break;
}
else
{
printf("the code is wrong, please input again\n");
scanf("%s",a.code);
}
}
fflush(stdin);
fptr=fopen(filename,"wb+");
//build a new structure to cover orginal file
strcpy(b.history,ptr);
//write yhe whole struct into file
fwrite(&b,sizeof(struct account),1,fptr);
fflush(stdin);
fclose(fptr);
}
return;
}