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:
Und hier eine Dict Datei
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
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