Komplikationen



  • Hallo zusammen, ich bin dabei ein Hangman Spiel zu programmieren und habe noch 2 kleine Probleme (bis jetzt). Und zwar werden richtige Buchstaben nur aufgedeckt, wenn man sie genau in der richtigen Reihenfolge angibt ansonsten werden sie nicht anerkannt. Was habe ich übersehen? Meine zweite Frage ist, wie muss ich das if-Argument formulieren, dass das Programm stoppet sobald alle Buchstaben aufgedeckt sind? Der Code ist in C++ und ich bin ein absoluter Neuling 😕

    Mein Code:
    #include <iostream>
    #include <string>
    #include "hangman.h"
    using namespace std;

    int main()
    {

    string word_to_guess = "hangman";
    string a_copy;
    int attempts = 6;

    cout << "Welcome to the Hangergames" << endl;
    cout << " " << endl;
    cout << " " << endl;

    a_copy += "[ ";

    for (int i = 0; i < word_to_guess.length(); i++)
    {

    a_copy+= "_ ";

    }

    a_copy += ']';
    cout << "Attempts left: " << attempts << endl;
    cout << a_copy << endl;

    char guess;
    cin >> guess;
    cout << "Your guess: " << guess << endl;
    cout << ' ' << endl;

    while (attempts > 0)
    {

    for (int i = 0; i < word_to_guess.length(); ++i)
    {
    if (guess == word_to_guess[i])
    {
    a_copy[2+2*i] = word_to_guess[i];
    cout << "Attempts left: " << attempts << endl;
    cout << a_copy << endl;
    cout << "Your guess: " << guess << endl;
    cin >> guess;
    cout << " " << endl;

    /if (Befehl dass "You won" kommt, wenn alle Buchstaben gefunde)
    {
    cout << "You won!" << endl;
    }
    /

    }

    else
    {
    cout << "Your attempts: " << attempts-- << endl;
    cout << a_copy << endl;
    cout << "Your guess: " << guess << endl;
    cin >> guess;
    cout << " " << endl;

    if(attempts == 0)
    {
    cout << "The word was: " << word_to_guess << endl;
    cout << "You lost!" << endl;

    }

    }

    }

    }

    return 0;
    }



  • if(a_copy.find('_')==string::npos)
    

    zum Beispiel.

    Aber in Deinem Programm ist auch sonst die Logik kaputt. Die for -Schleife sollte nur die Aufgabe haben, die gefundenen Buchstaben anzustreichen, macht aber sonst auch noch einiges anderes.

    //ps: Teilprobleme in Funktionen auslagern.

    //pps

    #include <iostream>
    #include <string>
    
    void print_gamestate(std::string const & guess, int attempts)
    {
    	std::cout << "[ ";
    	for (auto & ch : guess)
    		std::cout << ch << ' ';
    	std::cout << "]\n\nAttempts: " << attempts << "\n\n";
    
    }
    
    int main()
    {
    	std::string const word_to_guess{ "hangman" };
    	std::string guess{ std::string(word_to_guess.length(), '_') };
    	int attempts{ 6 };
    
    	do {
    		print_gamestate(guess, attempts);
    
    		char input;
    		std::cin >> input;
    
    		bool found = false;
    		for (auto i = word_to_guess.find(input); i != std::string::npos; i = word_to_guess.find(input, i + 1)) {
    			guess[i] = input;
    			found = true;
    		}
    
    		if (!found)
    			--attempts;
    
    	} while (guess != word_to_guess && attempts > 0);
    
    	print_gamestate(guess, attempts);
    
    	if (attempts > 0) {
    		std::cout << "You won!\n\n";
    	} else {
    		std::cout << "You lost!\n\n";
    	}
    }
    

Anmelden zum Antworten