1
resposta

[Projeto] Jogo da Forca

Jogo da Forca (Hangman)

  • Arquivo C++
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

string secretWord = "";
map<char, bool> guessed;
vector<char> wrongGuess;

void printHorizontalLine(int size, char style);
void printTitle(string title);

bool isGuessEqualsLetter(char guess);
void getGuess(char *guess);
void verifyGuess(char guess);
void printGuess();
void printWrongGuess();

bool isGameNotWon();
bool isPlayerHanged();
void printGameOver();

vector<string> readFile();
void drawSecretWord();
void addWord();
void saveFile(vector<string> newList);

int main() {
    readFile();
    drawSecretWord();
    // cout << secretWord << endl;

    const int  BORDER_SIZE = 35;
    const char BORDER_STYLE  = '-';
    const string TITLE = "JOGO DA FORCA";

    printHorizontalLine(BORDER_SIZE, BORDER_STYLE);
    printTitle(TITLE);
    printHorizontalLine(BORDER_SIZE, BORDER_STYLE);

    while(isGameNotWon() && isPlayerHanged()) {
        printWrongGuess();
        printGuess();

        char guess;
        getGuess(&guess);

        guessed[guess] = true;
        cout << endl;

        verifyGuess(guess);
        printHorizontalLine(BORDER_SIZE, BORDER_STYLE);
    }

    printGameOver();
    return 0;
}

void printHorizontalLine(int size, char style) {
    for(int i = 0; i < size; i++) {
        cout << style;
    }
    cout << endl;
}

void printTitle(string title) {
    cout << "\t" << title << endl;
}

bool isGuessEqualsLetter(char guess) {
    for(char letter : secretWord) {
        if(guess == letter) return true;
    }
    return false;
}

void getGuess(char *guess) {
    cout << "Digite seu chute: ";
    cin >> guess;
}

void verifyGuess(char guess) {
    if(!isGuessEqualsLetter(guess)) {
        wrongGuess.push_back(guess);
    } 
}

void printGuess() {
    for(char letter : secretWord) {
        if(guessed[letter]) cout << letter << " ";
        else cout << "_ ";
    }
    cout << endl << endl;
}

void printWrongGuess() {
    cout << "Chutes errados: ";
    for(char letter: wrongGuess) {
            cout << letter << " ";
    }
    cout << endl << endl;
}

bool isGameNotWon() {
    for(char letter : secretWord) {
        if(!guessed[letter]) {
            return true;
        }
    }
    return false;
}

bool isPlayerHanged() {
    return wrongGuess.size() < 5;
}

void printGameOver() {
    cout << "A palavra secreta era " << secretWord << endl;
    if(isGameNotWon()) {
        cout << "Voce perdeu." << endl;
    }
    else { 
        cout << "Parabens! Voce venceu! " << endl;
        cout << "Voce deseja adicionar uma nova palavra ao banco (S/N)? ";
        char option;
        cin >> option;

        if(option == 'S') {
            addWord();
        } else {
            exit(0);
        }
    }
}

vector<string> readFile() {
    ifstream file;
    file.open("words.txt");

    if(file.is_open()) {
        int size;
        file >> size;

        vector<string> wordDatabase;
        for(int i = 0; i < size; i++) {
        string word;
        file >> word;
        wordDatabase.push_back(word);
        }
        
        file.close(); 
        return wordDatabase;
    }
    else {
        cout << "Nao foi possivel acessar o banco de palavras." << endl;
        exit(0);
    }   
}

void drawSecretWord() {
    vector<string> words = readFile();

    srand(time(NULL));
    int drawIndex = rand() % words.size();

    secretWord = words[drawIndex];
}

void addWord() {
    cout << "Digite a nova palavra em LETRAS MAIUSCULAS: ";
    string newWord;
    cin >> newWord; 

    vector<string> wordList = readFile();
    wordList.push_back(newWord);

    saveFile(wordList);
}

void saveFile(vector<string> newList) {
    ofstream file;
    file.open("words.txt");
    if(file.is_open()) {
        file << newList.size() << endl;
        
        for(string word : newList) {
            file << word << endl;
        }

        file.close();
    }
    else {
        cout << "Nao foi possivel acessar o banco de palavras." << endl;
        exit(0);
    }
}
  1. Banco de palavras
6
ABACAXI
MELANCIA
MAMAO
LIMAO
ABOBORA
1 resposta

Olá, Eduarda! Tudo bem com você?

Dei uma olhada no seu código e ele está bem organizado e condizente com o que foi passado pelo instrutor, e isso mostra o quão comprometida você está com o curso.

Caso surjam quaisquer dúvidas eu estarei à sua disposição.

Abraços e bons estudos!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software