Need help. I'm a beginner at programming. And I really want to learn more. I've created a simple hangman game. But something's not right. The program compiled properly, its just that whenever I or the user guessed all the correct letters, the program still asks the user to guess a letter instead of displaying a message saying, "Congratulations!". How can I make it display?
Here's the code:
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include "iFunction.h"
#pragma warning(disable:4996)
#define NUM_OF_WORDS 25 // The number of words in the words list
#define NUM_OF_CHANCES 5 // The number of tries allowed
#define TRUE 1 // Bool for TRUE
#define FALSE 0 // Bool for FALSE
int _tmain(int argc, _TCHAR* argv[])
{
char *hanged[]={
"\t\t\t\t|=====|\n"
"\t\t\t\t |\n"
"\t4 lives left...\t\t |\n"
"\t\t\t\t |\n"
"\t\t\t\t ===\n",
"\t\t\t\t|=====|\n"
"\t\t\t\tO |\n"
"\t3 lives left...\t\t |\n"
"\t\t\t\t |\n"
"\t\t\t\t ===\n",
"\t\t\t\t|=====|\n"
"\t\t\t\tO |\n"
"\t2 lives left...\t\t| |\n"
"\t\t\t\t |\n"
"\t\t\t\t ===\n",
"\t\t\t\t |=====|\n"
"\t\t\t\t O |\n"
"\t1 life left...\t\t/|\\ |\n"
"\t\t\t\t | |\n"
"\t\t\t\t ===\n",
"\t\t\t\t |=====|\n"
"\t\t\t\t[X] |\n"
"\tNo lives left...\t/|\\ |\n"
"\tYou Got Hanged!\t\t/ \\ |\n"
"\t\t\t\t ===\n"
};
char *words[] = {
"ARGUMENTS", "BYPASSING", "CHROMATIC", "DOWNLOADING","ELECTRICAL","FREEFLOW","GOOGLE","HACKING", "INTRANET", "JUNKFILE", "KEYWORDS", "LIFETIME",
"MOBILE", "NULLIFY", "PASSWORD", "QUADCORE", "RECONNECT", "STRUCTURE", "TIMELINES", "UPLOADING", "VOICECOMMAND", "WIRELESS", "XENDER",
"YOUTUBE", "ZIPCODES"
};
srand((unsigned) time(NULL));
unsigned short chosen_word = rand() % NUM_OF_WORDS; char guessed;
size_t len = strlen(words[chosen_word]); size_t i = 0;
unsigned int found = FALSE;
unsigned int chances = 0; char repeat;
do
{
system("cls");
chances = 0; chosen_word = rand() % NUM_OF_WORDS; len = strlen(words[chosen_word]); i = 0;
ShowHeadingRules();
ShowWordClue(len);
char *a = (char *)malloc(len + 1);
printf("\n Guess a letter: ");
while(1) {
guessed = toupper(getchar());
if(guessed != '\n')
{
if (found = len) {
if(strcmp(a, words[chosen_word]) == TRUE)
{
Load();
system("cls");
printf("\n Word to be guessed: ");
SeeInWord(guessed, words[chosen_word], a, len, &found);
printf("\n\n Guess a letter: ");
}
}
}
else if(found != TRUE) {
system("cls");
if (chances < 4)
{
printf("\n Word to be guessed: ");
SeeInWord(guessed, words[chosen_word], a, len, &found); }
if(chances >= 4)
{
system("cls");
printf("\n The Exact Word is '%s'", words[chosen_word]); printf("\n\n%s\n\n", hanged[chances++]);
if(chances == 5)
{
GameOver(); printf("\n\n\n\n\n");
printf(" ==========================================================================\n");
printf(" To repeat the program, press [y] or [Y]..\n");
printf(" If you wish to terminate the program,\n");
printf(" Just press any key: ");
repeat = getch();
break;
}
}
printf("\n\n\n\t\t\t********************");
printf("\n\t\t\t* Uy!!! *");
printf("\n\t\t\t*Tag-ana ug tarong!*");
printf("\n\t\t\t********************");
printf("\n --------------------------------------------------------------------");
printf("\n\n%s\n", hanged[chances++]);
printf(" --------------------------------------------------------------------");
printf("\n Guess a letter: ");
}
}
}while(repeat == 'y' || repeat == 'Y');
getch();
return 0;
}
//Header file
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
void ShowHeadingRules();
void GameOver();
void SeeInWord(char, const char [], char *, const size_t, unsigned int *);
void ShowWordClue(const size_t);
void Load();
void ShowHeadingRules() {
printf("\n\t\t\t******************************");
printf("\n\t\t\t*PROGRAMMER: Marvin M. Maasin*");
printf("\n\t\t\t*INSTRUCTOR: Sammy L. Bastes *");
printf("\n\t\t\t******************************\n\n");
printf("\t\t--------------------------------------------\n");
printf("\t\t| # # # # # ### # # # # # |\n");
printf("\t\t| # # # # ## # # ## ## # # ## # |\n");
printf("\t\t| #### ##### # # # # ## # # # ##### # # # |\n");
printf("\t\t| # # # # # ## # # # # # # # ## |\n");
printf("\t\t| # # # # # # ### # # # # # # |\n");
printf("\t\t--------------------------------------------\n");
printf("******************************************************************************\n");
printf("*Hangman is a game where you are expected to guess a word in a certain number*\n");
printf("*of chances that is before you hang the hangman. *\n");
printf("******************************************************************************\n\n");
printf("A word has been choosen, begin guessing it. Good luck!!!\n\n");
printf("You have to guess this word:");
}
void GameOver() {
printf("\t[][][][][][][][][][][][][][][][][][][][][]\n");
printf("\t[] []\n");
printf("\t[] You killed a person! Good Bye. :'( []\n");
printf("\t[] []\n");
printf("\t[][][][][][][][][][][][][][][][][][][][][]");
}
void ShowWordClue(const size_t len) {
size_t i; for(i = 0; i < len; i++)
{
printf(" _ ");
}
printf("\n\n");
}
void SeeInWord(char guessed, const char word[], char *a, const size_t len, unsigned int *found) {
size_t i;
for(i = 0; i < len; i++)
{
if(guessed == word[i])
{
*(a + i) = guessed;
*found = TRUE;
}
else
{
if(*(a + i) >= 65 && *(a + i) <= 90 ); else if(*(a + i) >= 97 && *(a + i) <= 122); else *(a + i) = '_'; }
}
for(i = 0; i < len; i++) {
printf("%c ", *(a + i));
}
for(i = 0; i < len; i++)
{
if(strcmp(a, word) == 0)
{
printf("\nCONGRATULATIONS! ");
}
}
}
void Load() {
for (int p = 0; p < 8; p++)
{
switch (p)
{
case 1:
system("cls");
printf("\n\n\n\n\t\t\t Loading ------ [10%%]");
case 2:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading --- [20%%]");
case 3:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading ------ [30%%]");
case 4:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading ---- [40%%]");
case 5:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading ------ [50%%]");
case 6:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading --- [60%%]");
case 7:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading ------ [70%%]");
case 8:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading --- [80%%]");
case 9:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading ------ [90%%]");
case 10:
system("cls");
printf("\n\n\n\n\n");
printf("\t\t\t Loading --- [100%%]");
break;
}
}
}
What I have tried:
I just tried to make a simple hangman game in C language.