Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Last active April 3, 2023 21:13
Show Gist options
  • Save andersonbosa/b209778bd0ea760b49b8b24274b90ef0 to your computer and use it in GitHub Desktop.
Save andersonbosa/b209778bd0ea760b49b8b24274b90ef0 to your computer and use it in GitHub Desktop.
Hangman game
#include <iostream>
#include <stdlib.h>
using namespace std;
#define printNewLine cout << "\n";
#define clearScreen system("clear");
#define pauseCli system("pause");
/**
* O joguinho não está funcionando 100%. Não encontrei o erro =(
* O problema é que ele não da a mensagem de win or loose.
*/
int main()
{
// jogo da forca
char word[30],
secretWord[30],
letter[1];
int wordLength = 0,
i = 0,
hits = 0, // acertos
chances = 6;
bool hasHit; // acertou
cout << "Qual a palavra secreta (da forca): ";
cin >> word;
clearScreen;
while (word[i] != '\0')
{
i++;
wordLength++;
}
for (i = 0; i < 30; i++)
{
secretWord[i] = '-';
}
bool temChancesENaoAcertou = ((chances > 0) && (hits < wordLength));
while (temChancesENaoAcertou)
{
cout << "Chances restantes: " << chances;
printNewLine;
printNewLine;
cout << "Palavra secreta: ";
for (i = 0; i < wordLength; i++)
{
cout << secretWord[i];
}
printNewLine;
printNewLine;
cout << "tente uma letra: ";
cin >> letter[0];
for (i = 0; i < wordLength; i++)
{
if (word[i] == letter[0])
{
hasHit = true;
hits++;
secretWord[i] = word[i];
}
}
if (!hasHit)
{
chances--;
}
hasHit = false;
clearScreen;
}
bool hasWin = (hits == wordLength);
if (hasWin)
{
cout << "Congratulations! You found the secret word!";
printNewLine;
}
else
{
cout << "You loose! Better luck next time!";
printNewLine;
}
pauseCli;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment