Hello,
Thanks for all the answers, somehow I didn't receive any more notifications even though I did request it.
Anyways . . . I've been working on the code for a little longer, and I've got this so far.
Right now, it is opening the file correctly, generating a random number correctly (Even though I sometimes get a -1 as a random number which screws everything up, hopefully someone can suggest me something to do), but apart from that, I only have one problem left.
As of now, as soon as it goes through the line of the random number, it grabs the word and puts it into an array (secreta[10]). My idea now is the following, as the user enters the letter (variable "guess") I want to compare it to the array so I can keep working, I just don't know how to do that.
Any help is appreciated, thank you!
Code:
#include <stdio.h> /* librerias */
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
char finLoop = 's';
int puntuacion = 0, posibilidad = 7, random=0, cont=0, i, x, z, size, opcion=0, value=20;
int fallos =0, aciertos=1;
char secreta[10], c[10], jugador[20], rayas[15], guess, comodin;
FILE *fp;
printf("Bienvenido al juego del ahorcado, por favor, introduce tu nombre: ");
scanf("%s",jugador);
srand((unsigned)time(0)); //Se usa funcion srand para generar numero aleatorio
random =(rand()%10)+1; //Se genera numero aleatorio y se pasa a la variable "random"
for (i=0; i<=10; i++) // Limpiamos el array de cualquier residuo.
{
secreta[i] = '\0';
}
for (i=0; i<=10; i++) // Limpiamos el array de cualquier residuo.
{
rayas[i] = '\0';
}
fp = fopen("wordlist.txt","r"); //Abre el fichero para ser leido
if(fp==NULL)
{
printf("Error: no se puede abrir el fichero.\n");
return 1;
}
else
{
while(fgets(c, 10, fp)!=NULL)
{
printf("String: %s", c);
cont++;
if (cont == random)
{
fgets(secreta,10,fp);
goto etiqueta; // Usamos goto para que salte a la etiqueta una vez tener la palabra secreta
}
}
etiqueta: //Etiqueta creada para el uso de goto
fclose(fp);
}
size = (strlen(secreta)) - 1; //Obtenemos el tamaņo de la palabra
printf("El size es: %d",size);
system("cls");
printf("1. Jugar al ahorcado.\n");
printf("2. Reglas.\n");
printf("3. Salir.\n");
scanf("%d",&opcion);
switch (opcion)
{
case 1:
for (x=0; x<(size*2); x++)
{
rayas[x] = '_';
rayas[x+1] = ' ';
x++;
}
printf("Palabra a resolver: %s",rayas);
while (fallos != '7')
{
printf("Introduce una letra: \n");
scanf("%c",&guess);
}
printf("%s",rayas);
printf("%d",size);
scanf("%d",&x);
break;
case 2:
printf("Las reglas son las siguientes:\n");
printf("1. Por cada letra acertada, sumaras 2 puntos.\n");
printf("2. Por cada letra no acertada, restaras 1 punto.\n");
printf("3. Ganas si aciertas la palabra antes de cometer 7 fallos.\n");
printf("4. Si resuelves antes de gastar todas las oportunidades sumaras 10 puntos.\n");
break;
case 3:
return 1;
break;
default:
printf("Opcion erronea, intentelo de nuevo.\n");
}
}