Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
void menu(int *option);
void instructions();
void players(int *player);
void playGame(char word[], int player);
void getList(char word[], char list[][], int *count);
void getWord();
void enterWord(char word[]);
void guessWord();
void drawMan(int numWrong);
void listWrong(char wrongLetters[], int i, char guess);
/*
*Function: Main
*
*Inputs -
*Outputs -
*
*Description:
*
*/
main(){
char word[32];
int option=0, player;
/* Basis number generation on time*/
srand( (unsigned)time( NULL ) );
system("clear");
printf("***************************************************");
printf("\n* Hello! Welcome to Hangman! *\n");
printf("***************************************************");
menu(&option);
printf("%d", option);
while(option!=4){
if(option==1)
playGame(word, player);
if(option==2)
players(&player);
if(option==3)
instructions();
menu(&option);
}
if(option==4)
exit(0);
}
/*
*Function: Menu
*
*Inputs -
*Outputs -
*
*Description:
*
*/
void menu(int *option){
int choice;
printf("\n\nPlease chose an option:\n\n");
printf("1.Play Game\n");
printf("2.Change Players\n");
printf("3.Instructions\n");
printf("4.Quit Hangman\n\n");
printf("Chose option: ");
fflush(stdin);
scanf("%d", &choice);
switch(choice){
case 1:
case 2:
case 3:
case 4:
*option=choice;
break;
default:
while((choice!=1)&&(choice!=2)&&(choice!=3)&&(choice!=4)){
printf("That is not a valid entry, try again.\n\n");
printf("Chose option: ");
fflush(stdin);
scanf("%d", &choice);
}
*option=choice;
break;
}
}
void playGame(char word[], int player){
int count, fileRead=0;
char list[100][32];
if(player==1){
if(fileRead){
fileRead++;
getList(word, list, &count);
}
getWord(word, list, count);
}
else
enterWord(word);
guessWord(word);
}
void players(int *player){
int players;
system("clear");
printf("How many players (1 or 2): ");
fflush(stdin);
scanf("%d", players);
*player=players;
}
void instructions(){
}
void getList(char word[], char list[][], int *count){
FILE *fp;
int row;
if((fp=fopen("words.dat", "r"))==NULL){
printf("Can't open file.\n");
exit(0);
}
for(row=0; (fscanf(fp, "%s", list[row])!=EOF); row++);
fclose(fp);
*count=row;
}
void getWord(char word[], char list[][], int count){
int x, i, j, usedWords[count], noMore;
noMore=0;
do{
x=(rand()%(count));
noMore++;
}while((!usedWords[x]==1)&&(noMore<count));
if(noMore==count){
system("clear");
printf("\n\n\nYou have beaten HangMan, Congratulations!\n");
printf("In other words, there are no more words...");
exit(0);
}
j=stnlen(list[x]);
i=0;
while(i < j){
word[i]=list[x][i];
i++;
}
word[j]='\0';
usedWords[x]=1;
}
void enterWord(word[]){
void guessWord(char word[]){
int wordLength, i, j, k, numWrong, numRight, badGuess;
char blanks[32], wrongLetters[18], guess;
wordLength=strlen(word);
arrayLength= wordLength + 1;
for(i=0; i<wordLength; i++){
blanks[i]='-';
}
blanks[i]='\0';
numRight=0;
numWrong=0;
while((numRight<wordlength)&&(numWrong<6)){
system("clear");
drawMan(numWrong);
printf("\n\n");
put(blanks);
printf("\n ");
put(wrongLetters);
printf("\n\nGuess a letter: ");
fflush(stdin);
scanf("%c", &guess);
fflush(stdin);
while((guess<'a')||(guess>'z')){
printf("Please use lower case letters only.\n\n");
fflush(stdin);
printf("Guess a letter: ");
scanf("%c", &guess);
}
badGuess=1;
for(j=0; j<wordLength; j++){
if(word[j]==guess){
blanks[j]=word[j];
badGuess=0;
}
}
if(badGuess){
numWrong++;
listWrong(wrongLetters, numWrong, guess)
}
if(numWrong==6)
printf("You lose! Sorry!\n");
else
printf("You win! Good job!\n");
printf("Press enter to continue to menu.");
scanf("%d");
}
void drawMan(int numWrong){
char gallow[7][10]={"/=====o "
"|| | "
"|| "
"|| "
"|| "
"||______ "
"|_______\\"};
if(numWrong==1)
gallow[2][5]='O';
if(numWrong==2)
gallow[3][5]='|';
if(numWrong==3)
gallow[3][4]='/';
if(numWrong==4){
gallow[3][6]='\\';
}
if(numWrong==5){
gallow[4][6]='\\';
}
if(numWrong==6)
gallow[4][4]='/';
puts(gallow[0]);
puts(gallow[1]);
puts(gallow[2]);
puts(gallow[3]);
puts(gallow[4]);
puts(gallow[5]);
puts(gallow[6]);
puts(gallow[7]);
return(0);
}
void listWrong(char wrongLetters[], int i, char guess){
if(i==1){
wrongLetters[0]=guess;
wrongLetters[1]=',';
wrongLetters[2]=' ';
}
if(i==2){
wrongLetters[3]=guess;
wrongLetters[4]=',';
wrongLetters[5]=' ';
}
if(i==3){
wrongLetters[6]=guess;
wrongLetters[7]=',';
wrongLetters[8]=' ';
}
if(i==4){
wrongLetters[9]=guess;
wrongLetters[10]=',';
wrongLetters[11]=' ';
}
if(i==5){
wrongLetters[12]=guess;
wrongLetters[13]=',';
wrongLetters[14]=' ';
}
if(i==6){
wrongLetters[15]=guess;
}
wrongLetters[16]='\0';
}