Hangman (Erste Versuche mit C/C++)

Romsl

Erfahrenes Mitglied
Hi,

versuche gerade ein Console hangman zu programmieren. Dabei soll eine Datei mit Woertern zunaechst auf die Anzahl der Woerter ueberprueft werden und danach moechte ich ein beliebiges Wort haben. Wenn der Benutzer alles richtig hat soll ein weiteres beliebiges Wort folgen, wenn nicht soll abgebrochen werden.

Mein jetziger Code:

Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include "dict.h"

using namespace std;

class Hangman
{

    public:

        Hangman()
        {
            total_words = -1;
            word_no = -1;

            find_word( "test.txt" );

            guessed_word = new char[ 0 ];
            guessed_letter = new char[ 0 ];
            missed = 0;

            for ( int i = 0; word[ i ]; i++ )
            {
               guessed_word[ i ] = '?';
            }

            next_round();
        }

        void find_word( string filename )
        {
            ifstream file( "test.txt" );

            if ( total_words == -1 )
            {
                ifstream file2( "test.txt" );
                total_words = count_words( file2 );
                file2.close();
                srand( (unsigned)time( NULL ) );
                word_no = rand() % total_words;
            }
            total_words = get_word( file, word );

            file.close();
        }

        int count_words( ifstream &file )
        {
            if ( !file.is_open() )
            {
                cout << "Couldn't open dictionary file." << endl;
                exit( 1 );
            }

            string str;
            int words = 0;
            int i = 0;
            while ( getline( file, str ) )
            {
                words++;
            }
            return words;
        }

int get_word( ifstream &file, char* word )
        {
            if ( !file.is_open() )
            {
                cout << "Couldn't open dictionary file." << endl;
                exit( 1 );
            }

            string str;
            int i = 0;
            while ( getline( file, str ) )
            {
                if ( word_no == i )
                {
                    word = new char[ 40 ];

                    //cout << str << endl;
                    word = strdup( str );
                    cout << "WORD: " << word << endl;

                    break;
                }
                i++;
            }
            return total_words;
        }

        void next_round()
        {
            cout << "Please enter a letter guess, my worthy adversary" << endl;

            char letter;
            cin >> letter;

            if ( !check_letter( letter ) )
            {
                missed++;
            }

            cout << "YOUR WORD SO FAR\n" << endl;

            draw_guessed_word();

            draw_hangman( missed );
        }

        /**
         * Check wheather the letter is within the word or not.
         */
        bool check_letter( char letter )
        {
            bool found = false;

            for ( int i = 0; guessed_letter[ i ]; i++ )
            {
                if ( guessed_letter[ i ] == letter ) {
                    cout << "You already used \"" << letter << "\"\n" << endl;
                    return true;
                }
            }

            guessed_letter[ strlen( guessed_letter ) ] = letter;

            for ( int i = 0; word[ i ]; i++ )
{
                if ( word[ i ] == letter )
                {
                    found = true;
                    guessed_word[ i ] = word[ i ];
                }
            }
            return found;
        }

        void draw_guessed_word()
        {
            bool finished = true;

            for ( int i = 0; guessed_word[ i ]; i++ )
            {
                if ( guessed_word[ i ] == '?' )
                {
                    finished = false;
                    cout << "___  ";
                }
                else
                {
                    cout << " " << guessed_word[ i ] << "  ";
                }
            }

            cout << endl;

            if ( finished )
            {
                cout << endl << "You won. Congratulations!" << endl;
                exit( 0 );
            }
        }

        /**
         * This method draws the hangman.
         */
        void draw_hangman( int misses )
        {
            cout << endl;

            switch ( misses )
            {
                case 1:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( "============\n" );
                    break;
                case 2:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |    O\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( "============\n" );
                    break;
                case 3:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |    O\n" );
                    printf( " |    |\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( "============\n" );
                    break;
                case 4:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |    O\n" );
                    printf( " |    |\\\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( "============\n" );
                    break;
                case 5:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |    O\n" );
                    printf( " |   /|\\\n" );
                    printf( " |\n" );
                    printf( " |\n" );
                    printf( "============\n" );
                    break;
                case 6:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |    O\n" );
                    printf( " |   /|\\\n" );
                    printf( " |     \\\n" );
                    printf( " |\n" );
                    printf( "============\n" );
                    break;
                case 7:
                    printf( " +----+\n" );
                    printf( " |    |\n" );
                    printf( " |    O\n" );
                    printf( " |   /|\\\n" );
                    printf( " |   / \\\n" );
                    printf( " |Your Dead\n" );
                    printf( "============\n" );
                    exit( 0 );
            }

            next_round();
        }

    private:
        char* word;
        char* guessed_word;
        char* guessed_letter;
        int missed;
        int total_words;
        int word_no;
};

int main( int argc, char* argv[] )
{
    Hangman *h = new Hangman();
}

Und hier eine Dict Datei

Code:
rahmen
pferd
spiel
troete
hund
katze
maus

Mein Problem besteht, dass ich den eingelesenen string nicht in das char* bekomme. Moechte also den string in den char* umwandeln. Habs mit c_str() schon versucht.

Danke,
-- Romsl
 
Hi,
also erstmal muss ich sagen das das unter c++ ein bischen anderst
läuft als in Java. Die Klassen werden hier mit den Funktionsprototypen
in die Header Datei geschrieben und der code in die cpp Datei.

Das würde dann so aussehen:
Als erstest eine Hauptdatei
Code:
//main.cpp

#include "Hangman.h"
#include ....

int main(){
...
}
Dann das Headerfile für die Klasse:
Code:
//Haengman.h

class Hangman{
private:
//Atribute
char* word;
...
public:
// Funktionen
Hangman();
int findWord( String filename );
...
};
Und die cpp datei mit dem code
Code:
//Haengman.cpp
#include "Haengman.h"

Haengman::Haengman(){
//Konstruktorzeug...
}

int Haengman::findWord( String filename){
//code
}

Und jetzt zu deinem Proble. ^^
Du meinst das in der get_word(ifstreeam if, char*word) Funktion?
Du legst hier zwar neuen Speicher an aber du hast nur eine Kopie
der Adresse.
Bsp:
word -> 0x0000 // zeigt auf eine beliebige Adresse.
Jetzt rufst du die Funktion auf und übergibst eine Kopie dieser Adressen
wordc -> 0x0000
dann legst du neuen Speicher an
wordc = new char[40];
wordc -> 0x2340 // Word zeigt jetz irgendwo hin
Jetzt verlässt du die Funktion wieder und hast immer noch dein altes
word -> 0x0000;

du hast also nur die kopierte Adresse geändert.
Um die Adresse zu änden must du einen Zeiger auf einen Zeiger übergeben.

Code:
function( char** str ){

*str  = new char[40];
//blabla
}

Der Aufruf dieser Funktion könnte dann so aussehn:
Code:
char * str = 0;
function( &str );
//blabla

Benny
 
Das Problem ich hab das Header File schon und darf die Funktion nicht aendern.

Code:
// dict.h

#include<iostream>
#include<fstream>
using namespace std;

const int MAX_WORD=300;

int get_word (ifstream &, char[]);
void draw_hangman (int);

Das Projekt haengt anbei.
 

Anhänge

Aber die Funktion ist ja keiner Klasse zugeordnet.
Die ist ja global. Und in der Haengman Klasse
definierst du sie nochmal?

Ich versteh den Sinn grad nicht so ganz.

Wenn du die die Schnittstelle allerdings nicht ändern
darfst. Dann must du den Speicher schon vorher
ausreichend anlegen und den String hinkopieren.

Benny
 
Zurück