This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey there, I gotta deliver a project for my c class tomorrow and that project is the hangman :)
The thing is that the program actually works, but I need to make an option to start a new game after you guess the word. I tried to do it using a while but the thing is it doesn't work, it just shows again the screen with the guessed word whenever I start a new game. I need help since I'm a beginner and I put some great effort trying to compilate this
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <time.h>
using namespace std;
void PrintMessage(string message, bool printprincipio = true, bool printfinal = true)
{
if (printprincipio)
{
cout << " --------------------------------- " << endl;
cout << "|";
}
else
{
cout << "|";
}
bool principio = true;
for (int i = message.length(); i < 33; i )
{
if (principio)
{
message = " " message;
}
else
{
message = message " ";
}
principio = !principio;
}
cout << message.c_str();
if (printfinal)
{
cout << "|" << endl;
cout << " --------------------------------- " << endl;
}
else
{
cout << "|" << endl;
}
}
void DrawHangman(int cdeintentos = 0)
{
if (cdeintentos >= 1)
PrintMessage("|", false, false);
else
PrintMessage("", false, false);
if (cdeintentos >= 2)
PrintMessage("|", false, false);
else
PrintMessage("", false, false);
if (cdeintentos >= 3)
PrintMessage("O", false, false);
else
PrintMessage("", false, false);
if (cdeintentos == 4)
PrintMessage("/ ", false, false);
if (cdeintentos == 5)
PrintMessage("/| ", false, false);
if (cdeintentos >= 6)
PrintMessage("/|\\", false, false);
else
PrintMessage("", false, false);
if (cdeintentos >= 7)
PrintMessage("|", false, false);
else
PrintMessage("", false, false);
if (cdeintentos == 8)
PrintMessage("/", false, false);
if (cdeintentos >= 9)
PrintMessage("/ \\", false, false);
else
PrintMessage("", false, false);
}
void PrintLetters(string input, char from, char to)
{
string s;
for (char i = from; i <= to; i )
{
if (input.find(i) == string::npos)
{
s = i;
s = " ";
}
else
s = " ";
}
PrintMessage(s, false, false);
}
void PrintAvailableLetters(string taken)
{
PrintMessage("Letras Disponibles");
PrintLetters(taken, 'A', 'M');
PrintLetters(taken, 'N', 'Z');
}
bool PrintWordAndCheckWin(string word, string guessed)
{
bool won = true;
string s;
for (int i = 0; i < word.length(); i )
{
if (guessed.find(word[i]) == string::npos)
{
won = false;
s = "_ ";
}
else
{
s = word[i];
s = " ";
}
}
PrintMessage(s, false);
return won;
}
string LoadRandomWord(string path)
{
int lineCount = 0;
string word;
vector<string> v;
ifstream reader(path);
if (reader.is_open())
{
while (std::getline(reader, word))
v.push_back(word);
int randomLine = rand() % v.size();
word = v.at(randomLine);
reader.close();
}
return word;
}
int TriesLeft(string word, string guessed)
{
int error = 0;
for (int i = 0; i < guessed.length(); i )
{
if (word.find(guessed[i]) == string::npos)
error ;
}
return error;
}
int main()
{
setlocale(LC\_CTYPE, "spanish");
int op=true;
while (op!=2){
cout << " ___ _ _ _ _ ___ ___ ___ _ ___ ___ "<<endl;
cout << " | \_\_| | /\_\\\\ | || |/ \_ \\\\| \_ \\\\/ \_\_| /\_\\\\ | \\\\ / \_ \\\\ "<<endl;
cout << " | \_|| |\_\_ / \_ \\\\| \_\_ | (\_) | | (\_\_ / \_ \\\\| |) | (\_) |"<<endl;
cout << " |\_\_\_|\_\_\_\_| /\_/ \\\\\_|\_||\_|\\\\\_\_\_/|\_|\_\\\\\\\\\_\_\_/\_/ \\\\\_|\_\_\_/ \\\\\_\_\_/ "<<endl;
system ("PAUSE");
system ("CLS");
cout << "1. INSTRUCCIONES" << endl;
cout << "2. JUGAR" << endl;
cout << "¿QUÉ OPCION DESEA ELEGIR?" << endl;
cin >> op;
system ("CLS");
switch (op){
case 1:
cout << "1.EL JUEGO SOLO PUEDE SER JUGADO CONTRA LA PC" << endl;
cout << "2.EN CASO DE QUERER JUGARLO CON OTRA PERSONA DEBE DE IR A LA CARPETA DEL PROYECTO Y EDITAR LAS PALABRAS EXISTENTES EN EL ARCHIVO PALABRAS.TXT" << endl;
cout << "3.EL SISTEMA DEL JUEGO ES SENSIBLE A MINÚSCULAS, ASÍ QUE DEBE DE INGRESAR LAS PALABRAS EN MAYÚSCULAS" << endl;
cout << "[4.NO](https://4.NO) SE PUEDEN INGRESAR PALABRAS CON LA LETRA Ñ" << endl;
system ("PAUSE");
system ("CLS");
break;
case 2:
srand(time(0));
string guesses;
string wordToGuess;
wordToGuess = LoadRandomWord("palabras.txt");
int tries = 0;
bool win = false;
int op=true;
while (op==true){
do
{
system("cls");
PrintMessage("EL AHORCADO");
DrawHangman(tries);
PrintAvailableLetters(guesses);
PrintMessage("ADIVINA LA PALABRA");
win = PrintWordAndCheckWin(wordToGuess, guesses);
if (win)
break;
char x;
cout << ">"; cin >> x;
if (guesses.find(x) == string::npos)
guesses = x;
tries = TriesLeft(wordToGuess, guesses);
} while (tries < 10);
if (win)
PrintMessage("GANASTE");
else
PrintMessage("PERDISTE");
cout<<"DESEAS VOLVER A JUGAR? SI =1 / NO=2"<<endl;
cin>>op;
}
system("pause");
getchar();
return 0;
}
}
}
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...