I know that Turbo C++ is very old but I'm using it since I need some functions from conio.h and graphics.h that are included only on a Borland compiler.
I guess I should have copied the entire code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <graphics.h>
#include <time.h>
void initializare(int life,char *necunoscut)
{
//Print info
clrscr();
gotoxy(1,1);
printf("Hangman v. 1.0 - Joc creat in C\n\n");
printf("Type the correct letter to complete the word:\n\n");
printf("Lifes: %d\n\n",life);
printf(necunoscut);printf("\n\n");
}
//Function that returns a random number
int nr_random(void)
{
return 1+ rand()%6;
}
//Find the numbers of X charecters from the word; used to update life
int charcnt(char *ptr, char c, int size)
{
int count = 0, i; /*contor*/
for(i=0;i<size;i++){
if (c==ptr[i]) count++;
}
return count;
}
int main()
{
char *word="R O M A N I A";
char letter;
char necunoscut[256];
int lenght_word;
int i,j,l,m; // variabile contor
int x,y; // cursor's position
unsigned int random_number;
int no_lines=0, life=6,end=1;
/*Choose the word*/
srand((unsigned)time(NULL));
random_number=nr_random();
printf("%d", random_number); //I checked and it brings a different number between 0-6 each time.
switch(random_number)
{
case '0': word="A L B A N I A";
break;
case '1': word="F R A N C E";
break;
case '2': word="R O M A N I A";
break;
case '3': word="C A N A D A";
break;
case '4': word="H U N G A R Y";
break;
case '5': word="S P A I N";
break;
case '6': word="B U L G A R I A";
break;
}
lenght_word=strlen(word);
necunoscut[lenght_word+1]='\0';
//Find the number of '_' that need to be printed, depending on the size of the word
for(j=0;j<=lenght_word;j+=2){
necunoscut[j]='_';
necunoscut[j+1]=' ';
}
necunoscut[lenght_word]='\0';
//Print initial info
printf("Hangman v. 1.0 - Joc creat in C\n\n");
printf("Type the correct letter to complete the word:\n\n");
printf("Lifes: %d\n\n",life, " ");
printf(necunoscut);printf("\n\n");
//Testing if the word has been found
while (necunoscut!=word)
{
printf("Letter typed:");letter=getch();
letter=toupper(letter);
//Check if the character is a letter
while ((letter!='A')&&(letter!='B')&&(letter!='C')&&(letter!='D')&&(letter!='E')&&(letter!='F')&&(letter!='G')&&(letter!='H')&&
(letter!='I')&&(letter!='J')&&(letter!='K')&&(letter!='L')&&(letter!='M')&&(letter!='N')&&(letter!='O')&&(letter!='P')&&
(letter!='Q')&&(letter!='R')&&(letter!='S')&&(letter!='T')&&(letter!='U')&&(letter!='V')&&(letter!='W')&&(letter!='X')&&
(letter!='Y')&&(letter!='Z')) {
//If user typed a character different from a letter:
nr_lines=0;
for(l=0;l<=lenght_word;l++)
{
if (necunoscut[l]=='_') nr_lines=nr_lines+1;
}
if(nr_lines==0) break;
if (life==0) break;
putch(7);
delline();
y=wherey();
gotoxy(1,y);
printf("Letter typed: "); letter=getch();
letter=toupper(letter);
}
//If the character is a letter
for(i=0;i<=lenght_word;i++){
if (word[i]==letter) necunoscut[i]=letter;
}
if((charcnt(necunoscut,letter,lenght_word))==0) life=life-1;
//Deletes the last two line; used to update the console
initializare(life,necunoscut);
nr_lines=0;
for(l=0;l<=lenght_word;l++)
{
if (necunoscut[l]=='_') nr_lines=nr_lines+1;
}
if (nr_lines==0) break;
if (life==0) {
end=0;
break;
}
}
//End of game
if (end==0) printf("\n\nGAME OVER!\n");
if (end==1) printf("\n\nYOU WON!\n");
system("PAUSE");
return 0;
}
The problem is that each time the word is "R O M A N I A", nomatter the value of random_number. Could you give me some advice?