#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include<time.h>

char nombrecat [5][15] = {"frutas","animales","paises","objetos","clubes"};
char frutas [10][15] = {"MELON","PAPAYA","SANDIA","MANZANA","PERA","NARANJA","UVA","CEREZA","CIRUELA","KIWI"}; 
char animales [10][15] = {"PERRO","GATO","CABALLO","GALLINA","JIRAFA","MONO","VACA","CONEJO","TORTUGA","LOBO"};	
char paises [10][15] = {"PERU","COLOMBIA","ARGENTINA","NICARAGUA","ITALIA","MEXICO","CANADA","VENEZUELA","ECUADOR","BRASIL"};
char objetos [10][15] = {"MOCHILA","RELOJ","ZAPATILLA","MUEBLE","CUADERNO","SILLA","MESA","CELULAR","PUERTA","AURICULARES"};
char clubes [10][15] = {"BARCELONA","JUVENTUS","BOCA","TALLERES","LIVERPOOL","PALMEIRAS","NACIONAL","MILAN","CRUZEIRO","BORUSSIA"};
char frase[15];

//LAS FUNCIONES PRINCIPALES DEL JUEGO
void categorias();
void ingresoCategoria (int op);
void empezarJuego (char palabras[][15], char nombre[]);
void dibujo (int intentos);

//FUNCION PRINCIPAL MAIN
int main(){
	system("mode con cols=80 lines=25");
	categorias();
	system("pause");
	return 0;
}

void categorias(){
int op;
		do{
		clrscr();
		printf("________________________\n");
		printf("\n\t\tJUEGO DEL AHORCADO\n");
		printf("________________________\n\n");
		printf(" CATEGORIAS\n\n");
		printf(" 1. Frutas\n");
		printf(" 2. Animales\n");
		printf(" 3. Paises\n");
		printf(" 4. Objetos\n");
		printf(" 5. Clubes de Futbol\n\n");
		printf(" Indique una categoria: "); scanf("%d",&op);
		}while(op<1 || op>5);

	if (op==1) ingresoCategoria(op);
	if (op==2) ingresoCategoria(op);
	if (op==3) ingresoCategoria(op);
	if (op==4) ingresoCategoria(op);
	if (op==5) ingresoCategoria(op);


}

//ESTA FUNCION RECIBE UN VALOR, Y DEPENDIENDO DEL VALOR RECIBIDO LLAMA A OTRA FUNCION PARA EMPEZAR EL JUEGO
void ingresoCategoria (int op){
	
	switch(op) {
		case 1: empezarJuego(frutas,nombrecat[op-1]);
				break;

		case 2: empezarJuego(animales,nombrecat[op-1]);
				break;

		case 3: empezarJuego(paises,nombrecat[op-1]);
				break;

		case 4: empezarJuego(objetos,nombrecat[op-1]);
				break;

		case 5: empezarJuego(clubes,nombrecat[op-1]);
				break;

		default: printf("Su opcion no es valida...");
				 cgetc();
				
}

}


//FUNCION QUE CONTIENE EL ALGORITMO DEL JUEGO
void empezarJuego (char palabras[][15], char nombre[]){
	int opcion,i,j,longitud,espacios,puntos=1200;
	char letra;
	int aciertos = 0;
	int intentos = 0;
	int ganar = 0;
	srand(time(NULL));
	//char frase[longitud];
	
	opcion = rand() % 10; //SE GENERA UN NUMERO ALEATORIO COMPRENDIDO ENTRE 0 Y 9
	longitud = strlen(palabras[opcion]); //SE ALMACENA LA LONGITUD DE LA PALABRA 
	
	
	//SE COLOCAN GUIONES BAJOS EN EL ARRAY DE CARACTERES FRASE
	for(i=0; i < longitud; i++){
		frase[i] = '_';
	}
	
	
	do{
		aciertos = 0;
		clrscr();
		printf("________________________\n");
		printf("\n\t\tJUEGO DEL AHORCADO\n");
		printf("________________________\n\n");
		printf(" CATEGORIA: %s\n\n",nombre);
		printf(" Intentos Disponibles: %i\t\t\t\tPuntuacion: %i\n\n",6-intentos,puntos);
		dibujo(intentos);
		
		//IMPRIME EL ARRAY DE CARACTERES FRASE
		printf("\n\n\n");
		for(i=0; i < longitud; i++){
			printf(" %c ",frase[i]);
		}

		
		if (intentos == 6){
			printf("\n\n HA PERDIDO!!\n");
			printf(" LA PALABRA ES: %s\n\n",palabras[opcion]);
			printf("Pulse una tecla para volver a intentar...");
			cgetc();
			categorias();	
		}
		
		
		//PROCESO QUE COMPRUEBA SI SE HA ADIVINADO LA PALABRA
		espacios=0;
		
		for (i = 0; i < longitud; i++){
			if (frase[i] == '_')
				espacios++;
		}
		
		
		if (espacios == 0){
			printf("\n\n FELICITACIONES.. HA GANADO!!\n\n");
			printf("Pulse una tecla para volver a jugar..");
			cgetc();
			categorias();		
		}
		
		
		printf("\n\n Elija una letra (MAYUSCULA) ");
		scanf(" %c",&letra);
		
		//PROCESO QUE VERIFICA SI LA LETRA INGRESADA EXISTE EN LA PALABRA, SI ESTO ES VERDADERO, SE REEMPLAZA EL CARACTER GUION BAJO POR LA LETRA INGRESADA 
		for (j = 0; j < longitud; j++){
			if (letra == palabras[opcion][j]){
				frase[j] = letra;
				aciertos++;
			}	
		}
		
		if (aciertos == 0){
			intentos++;	
			puntos -= 200;
		}
		
		
			
	}while(intentos != 7);
	
	
	printf("\n\n");		
}

//FUNCION QUE REALIZA EL DIBUJO DE EL AHORCADO, RECIBE EL NUMERO DE INTENTOS Y CON ESE DATO REALIZA EL DIBUJO
void dibujo (int intentos){
	switch(intentos){
		case 0:
			//clrscr();
			printf("\n     _______\n    |       |\n    |\n    |\n    |\n    |\n    |\n ----------");
			break;
		case 1:
			//clrscr();
			printf("\n     _______\n    |       |\n    |       0\n    |\n    |\n    |\n    |\n ----------");
			break;
		case 2:
			//clrscr();
			printf("\n     _______\n    |       |\n    |       0\n    |       |\n    |\n    |\n    |\n ----------");
			break;
		case 3:
			//clrscr();
			printf("\n     _______\n    |       |\n    |       0\n    |      /|\n    |\n    |\n    |\n ----------");
			break;
		case 4:
			//clrscr();
			printf("\n     _______\n    |       |\n    |       0\n    |      /|");
			printf("\\");
			printf("\n");
			printf("    |\n    |\n    |\n ----------");
			break;
		case 5:
			//clrscr();
			printf("\n     _______\n    |       |\n    |       0\n    |      /|");
			printf("\\");
			printf("\n");
			printf("    |      /\n    |\n    |\n ----------");
			break;
		case 6:
			//clrscr();
			printf("\n     _______\n    |       |\n    |       0\n    |      /|");
			printf("\\");
			printf("\n");
			printf("    |      / ");
			printf("\\");
			printf("\n");
			printf("    |\n    |\n ----------");
			break;
	}
	
}
