Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* Prints welcome message */
void printwelc()
{
printf("Welcome to Word Guess!\n");
return;
}
/* Gets a secret word from the second player */
/*works*/
void getword(char secret[], int size)
{
int i;
int error1 = 0;
do
{
printf("Player 1, Please enter a secret word no larger than 16 characters\n");
fgets(secret, size, stdin);
secret[strlen(secret) - 1] = '\0';
if(strlen(secret) < 2)
{
printf("The word must be two or more letters\n");
error1++;
}
else
{
for (i=0; i < strlen(secret); i++)
{
secret[i] = tolower(secret[i]);
if(secret[i] < 'a' || secret[i] > 'z')
{
printf("The word must only contain letters\n");
error1++;
}
}
}
}while(error1 != 0);
printf("length is %d", strlen(secret));
return;
}
/* Copies the secret word into another string to be hidden*/
/*works*/
void copyword(char secret[], char hide[], int size)
{
if(size >= (strlen(secret) + 1))
{
strcpy(hide, secret);
}
else
{
printf("Not enough room to copy!\n");
}
return;
}
/* Turns copied string into '_' for every letter */
/*works*/
void hideword(char hide[])
{
int j;
int lengt = strlen(hide);
for (j=0; j < lengt; j++)
{
hide[j] = '_';
}
return;
}
/*Gets a letter from the Player if he chooses to guess a letter*/
/* breaks after one invalid option*/
char guessletter(char hide[], int size, int num_lives)
{
int error2 = 0;
char letguess;
do
{
printf("Number of lives: %d\n", num_lives);
printf("Guess a letter in the word ");
printf("%s\n", hide);
scanf("%c%*c", &letguess);
letguess = tolower(letguess);
if(letguess < 'a' || letguess > 'z')
{
printf("The guess must be a letter\n");
error2++;
}
}while(error2 != 0);
return(letguess);
}
/*Check whether word contains letguess*/
/*takes 1 life every turn regardless if you got it right or not*/
/* it doesn't work for words over 3 letters*/
void rightorwrong(char secret[], char hide[], char letguess, int size, int &num_lives, int &count, int length)
{
int l;
int m;
int guess;
char let = letguess;
char livepass = 0;
for (l=0; l<=length; l++)
{
if (let == secret[l])
{
for (m=0; m<=length; m++)
{
if ((let == secret[m]) && (hide[m] == '_'))
{
hide[m] = let;
count++;
printf("l is %d, m is %d, letguess is %c, secret[m] is %c, hide[m] is %c\n ", l, m, letguess, secret[m], hide[m]);
}
}
}
else
if (let != secret[l])
{
livepass++;
}
}
if(livepass > 0)
{
num_lives--;
}
return;
}
/* Tells Player if they have won or lost*/
/*Kind of works needs more testing after fixes*/
void winorlose(int num_lives, char secret[])
{
if (num_lives <= 0)
{
printf("Sorry, you're hanged! The word is \"%s\"\n", secret);
}
else
{
printf("Congratulations! The word is \"%s\"\n", secret);
}
return;
}
int main()
{
const int SIZE = 16;
char secret[SIZE];
char hide[SIZE];
int num_lives = 10;
int count = 0;
int length = strlen(secret);
char let;
printwelc();
getword(secret, SIZE);
copyword(secret, hide, SIZE);
hideword(hide);
do
{
let = guessletter(hide, SIZE, num_lives);
rightorwrong(secret, hide, let, SIZE, num_lives, count, length);
}while((num_lives > 0) && (count != length));
winorlose(num_lives, secret);
return 0;
}