Hello everyone,
I'm trying to do a game of hang man by comparing whats in one char array with another with a for loop with the compareword function. I tested it out with the wrong letters and had it return -1 so that parts working. What isn't fully working is when I guess the right letter. You have six chances and when I put in the correct letters I get -1 for the first 2 letters, 2 for the 3rd (which is right) and 2 for the last which is wrong.
Also, I'm trying to add * to the wip array but when I pass it to the displayarray function, it doesn't display unless I use a %c instead of %s. My instructor told me to put a %s outside of the for loop, but its still not displaying.
Any ideas ?
Thanks.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define S 25
//function prototypes
//displays instructions for user
void Instructions();
//displays character arrays
void DisplayArray(char array[]);
//prompts user to enter a letter and stores it in guess array
void GuessLetter(char array[], int guess);
//compares arrays to see if they match or not
int CompareArray(char arraya[], char arrayb[]);
//function that returns the index of the letter that the user has guessed or
//-1 if the letter isn't in the word
int CompareWord(char arraya[], char arrayb[], int size);
main()
{
//declare variables
FILE *fp;
int i;
int x;
char wtbg[S] = {'\0'};
char gl[S] = {'\0'};
char wip[S] = {'\0'};
int guess = 0;
int numLetters = 0;
int theSame = 0;
int wresult = 0;
//displays instructions for user
Instructions();
//get word from file
fp = fopen ("words.txt", "r");
fscanf(fp,"%s", wtbg);
//copy length of wtbg to wip array
numLetters = strlen(wtbg);
DisplayArray(wtbg);
while (guess != 6)
{
//printf("GUESS=%d", guess);
//display wip
printf("GUESS THIS WORD:\n");
for (i = 1;i <=numLetters; i++)
{
wip[i] = '*';
//printf("%c", wip[i]);
}
//wip[i] = '\0';
GuessLetter(gl, guess);
printf("\nLetters guessed so far: ");
DisplayArray(gl);
DisplayArray(wip);
wresult = CompareWord(wtbg, gl, numLetters);
printf("%d", wresult);
//theSame = CompareArray(wtbg, wip, numLetters);
//printf("\nAre they the same array? %d", theSame);
//if (theSame == 0)
//{
//printf("\nCONGRATS...YOU WON THE GAME!!!");
//}
//else if (theSame == 1)
//{
//printf("\nSORRY YOU DIDN'T WIN LOOSER...");
//}
//printf("NUM LETTERS = %d", numLetters);
//update number of guesses
guess++;}
//printf("GUESS=%d", guess);}
return 0;
}
//displays instructions for user
void Instructions()
{
printf("WELCOME TO THE ULTIMATE GAME OF HANGMAN!\n\n");
printf("PLEASE TAKE A LOOK AT THE FOLLOWING RULES/INSTRUCTIONS!\n\n");
printf("You will have the opportunity to guess a word.\n\n");
printf("Guess the letters one at a time.\n\n");
printf("You can have upto six wrong guesses.\n\n");
printf("The game ends when either you have guessed:\n");
printf("--all the letters in a word\n");
printf("--guessed wrong six times\n\n");
printf("LET THE GAMES COMMENCE...\n\n");
}
//displays character arrays
void DisplayArray(char array[])
{
printf("%s\n", array);
}
//prompts user to enter a letter and stores it in guess array
void GuessLetter(char array[], int guess)
{
printf("\nPlease enter a letter:");
scanf(" %c", &array[guess]);
array[guess] = tolower (array[guess]);
}
//compares arrays to see if they match or not
int CompareArray(char arraya[], char arrayb[])
{
int theSame;
theSame = strcmp(arraya,arrayb);
return theSame;
}
int CompareWord(char arraya[], char arrayb[], int size)
{
int i;
for (i = 1;i < size, i++;)
if(arraya[i] == arrayb[i])
{
return i;}
else
return -1;
}