Hangman game in C

1

I'm doing a game of Hangman in C and until now I have the count of the letters depending on the word to guess that the user enters, I already have a condition that tells me that if the letter that you enter is equal to any of the positions of the word then I will do a certain action and that's where my question comes in, how can I print the letters that fit within my lines, and how can I subtract the attempts that it has if it does not hit the letter?

#include<stdio.h>
#include<string.h>
#include<conio.h>

void main()
{
    int x,y,r,tamano,a;
    char palabra[100],palabra2[100];
    char letra;
    int intentos=5;
    printf ("***********AHORCADO*************\n");
    printf ("Ingrese la palabra a adivinar:\n");
    fflush(stdin);
    gets(palabra);
    system ("cls");

    tamano=strlen(palabra);
    printf("\n\n");

    for (x=0;x<tamano;x++)
        {  
         palabra2[x]=95;
        }
    for (x=0;x<tamano;x++)
        {
        printf (" %c",palabra2[x]);
        }

printf ("\nIntentos: %d",intentos);
printf ("\n");
printf ("Ingrese una letra:  ");
fflush(stdin);
scanf ("%c",&letra);

    for (x=0;x<tamano;x++)
        {
        if (letra==palabra[x])
            {
                palabra2[x]=palabra[x];
            }
        else 
        intentos--;
        }
}
    
asked by Arturo 15.08.2018 в 02:48
source

1 answer

2

There is a solution that is a bit far-fetched, but that will help you:

Insert this in your algorithm:

#include <windows.h>

void gotoxy(short x, short y)
{
   COORD pos = { x, y };
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void color(WORD col)
{
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),col);
}

What does it do?
Well, the first procedure is a "go to (x, y)", transforms the screen into a plane (not literally), calls the procedure as follows:

gotoxy(x,y);

This will make the pointer on the screen go to the coordinates of the command console, then what you type with printf will begin to be written from that point in the console screen

If you ask yourself what the other is, just set a color to whatever you type in the console, to use it:

color(variable);

And the console will change the characters and background color, according to the number entered, this is permanent until you use the command again

Do not forget to include the library.

    
answered by 15.08.2018 / 04:56
source