May 29, 2022 at 8:16pm UTC
Al realizar el codigo, me permite compilar correctamente, sin embargo hay dos cuestiones que no logro solucionar que es que me imprima cuando ya gane, y la segunda que las palabras esten aleatorias puesto que solo se queda con una de todas en todas las ocasiones que se ingresa al juego. Agradezco de antemano su ayuda, linda tarde.
#include <iostream>
#include <stdlib.h>
using namespace std;
void LimpiarPantalla();
void Jugarpartida();
void Dibujar();
char eleccion;
string palabras[] = { "programar", "calculo", "lampara", "bebida", "computadora", "musica", "tableta", "oso", "platano", "libreta"};
string palabra, fallidas;
int nA;
int vida;
bool correcta, completa;
int main()
{
while (true)
{
vida = 6;
palabra = "";
fallidas = "";
LimpiarPantalla();
cout << " \t Bienvenido al juego del ahorcado, una vez ingresado al juego podras divertirte descifrando \n una de nuestras palabras aleatorias, mucha SUERTE \t" << endl;
cout << " \t MENU " << endl;
cout << " 1.- Jugar partida " << endl;
cout << " 2.- Salir " << endl;
cout << " Eleccion: ";
cin >> eleccion;
switch (eleccion)
{
case '1':
Jugarpartida();
break;
case ' 2':
return 0;
break;
}
}
}
void Jugarpartida()
{
srand('int'); time(NULL);
nA = rand() %10;
cout << nA;
for (int i = 0; i<(int)palabras[nA].size(); i++)
{
palabra += "_";
}
cout << nA;
while (vida > 0)
{
LimpiarPantalla();
cout << "\t ::: Bienvenido al Juego del A h o r c a d o:::" << endl;
Dibujar();
cout << "Fallos: " << fallidas;
cout<< " Progreso: " <<palabra<< endl;
cout << "Ingrese una letra en minuscula: ";
cin >> eleccion;
correcta = false;
for (int i = 0; i < palabras[nA].size(); i++)
{
if (palabras[nA][i] == eleccion)
{
palabra[i] = eleccion;
correcta = true;
}
}
if (!correcta)
{
vida --;
fallidas += eleccion;
}
completa = true;
for (int i = 0; i<(int) palabra.size(); i++)
{
if (palabra[i] == ' _ ');
{
completa = false;
}
}
if (completa)
{
LimpiarPantalla();
cout << " \t ::: Bienvenido al Juego del Ahorcado:::" << endl;
Dibujar ();
cout << " La palabra que descifraste es: " << palabras[nA]<< endl;
cout << " FELICIDADES, Ganaste :) \t \n Ingresa un caracter para continuar: ";
cin >> eleccion;
return;
}
}
LimpiarPantalla();
cout << " \t ::: Bienvenido al Juego del Ahorcado:::" << endl;
Dibujar();
cout << "La palabra que no descifraste es: " << palabras[nA] << endl;
cout << " Perdiste este desafio:( \t \n Ingresa un caracter para continuar: ";
cin >> eleccion;
return;
}
void Dibujar()
{
switch (vida)
{
case 6:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
case 5:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| 0 " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
case 4:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| 0 " << endl;
cout << "| | " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
case 3:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| 0 " << endl;
cout << "| /| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
case 2:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| 0 " << endl;
cout << "| /|\> " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
case 1:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| 0 " << endl;
cout << "| /|\> " << endl;
cout << "| / " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
case 0:
cout << "-------- " << endl;
cout << "| | " << endl;
cout << "| 0 " << endl;
cout << "| /|\> " << endl;
cout << "| / \| " << endl;
cout << "| " << endl;
cout << "| " << endl;
cout << " --- " << endl;
break;
}
}
void LimpiarPantalla()
{
if (system("cls") ==-1)
{
cout << "Error al borrar la pantalla";
}
}
May 29, 2022 at 8:33pm UTC
try an else after the
if (complete)
{
LimpiarPantalla
...etc
}
else
{
LimpiarPantalla
..etc
}
May 30, 2022 at 2:43pm UTC
line 34: case ' 2' :
Has a space, delete that space. When you use single quotes it means only one character is being described.
line 76: if (palabra[i] == ' _ ' );
has two spaces in the single quotes, but you also have a semicolon at the end that needs to be removed
line 42: srand('int' ); time(NULL);
should be srand(time(NULL));
That's all I could find.
Happy programming.
Last edited on May 30, 2022 at 3:35pm UTC