Code:
/* currently goes up to get a letter then quits*/
#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 error)
{
int i;
do
{
error = 0;
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");
error++;
}
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");
error++;
}
}
}
}while(error != 0);
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*/
/* re-prints prompt*/
void guessletter(char hide[], char letguess[], int size, int size2, int num_lives, int error)
{
do
{
error = 0;
printf("Number of lives: %d\n", num_lives);
printf("Guess a letter in the word ");
printf("%s\n", hide);
fgets(letguess, size2, stdin);
letguess[strlen(letguess) - 1] = '\0';
if(strlen(letguess) > 1)
{
printf("You can only guess one letter at a time!\n");
error++;
}
else
{
letguess[0] = tolower(letguess[0]);
if(letguess[0] < 'a' || letguess[0] > 'z')
{
printf("The guess must be a letter\n");
error++;
}
}
}while(error != 0);
return;
}
/*Check whether word contains letguess*/
/*If this doesn't work try changing it to a char and putting in an assumption*/
void rightorwrong(char secret[], char hide[], char letguess[], int size, int size2, int num_lives, int count, int length)
{
int l;
int m;
for (l=0; l<length; l++)
{
if (letguess[0] == secret[l])
{
for (m=0; m<length; m++)
{
if ((letguess[0] == secret[m]) && (hide[m] == '_'))
{
hide[m] = letguess[0];
count++;
}
}
}
else
{
num_lives--;
}
}
return;
}
/* Tells Player if they have won or lost*/
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;
const int SIZE2 = 2;
char secret[SIZE] = {'\0'};
char hide[SIZE] = {'\0'};
char letguess[SIZE2] = {'\0'};
int num_lives = 10;
int count = 0;
int length = strlen(secret);
int error;
printwelc();
getword(secret, SIZE, error);
copyword(secret, hide, SIZE);
hideword(hide);
do
{
guessletter(hide, letguess, SIZE, SIZE2, num_lives, error);
rightorwrong(secret, hide, letguess, SIZE, SIZE2, num_lives, count, length);
}while((num_lives != 0) && (count != length));
winorlose(num_lives, secret);
return 0;
}
For the secret word I input 'apple', when prompted for a letter I input 'a' I then get the message 'The guess must be a letter'' the prompt then prints twice in a row. Even weirder, the more letters I guess for example 'aa' the more times the prompt prints E.g 'a' prompt prints twice, 'aa' the prompt prints three times.