Hallo Leute,
ich habe ein Hangman ich C++ geschrieben und jetzt habe ich ein Problem. Ich möchte mein Programm in Klassen und Objekten umwandeln. Egal was ich bisher gemacht habe es funktioniert einfach nicht. Würde mich freuen, wenn ihr mir helfen könntet und mir einige Passagen in Klassen und Objekten umwandeln könntet, damit ich sehe, wie das wirklich funktioniert.
So hier ist mein Quellcode:
ps: Hab es nicht geschafft fertig zu kommentieren. Aber ich denke ihr könnt sehen, was das programm mancht!
Vielen dank an euch alle!
ich habe ein Hangman ich C++ geschrieben und jetzt habe ich ein Problem. Ich möchte mein Programm in Klassen und Objekten umwandeln. Egal was ich bisher gemacht habe es funktioniert einfach nicht. Würde mich freuen, wenn ihr mir helfen könntet und mir einige Passagen in Klassen und Objekten umwandeln könntet, damit ich sehe, wie das wirklich funktioniert.
So hier ist mein Quellcode:
Code:
#include <iostream> // Includen :)
#include <string>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
using namespace std;
char pos7[15][30] = // Damit wir sehen, wann unser Hangman gehängt wird,
{ " ------------", // hier ein Bild von ihm.
"| |",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"____"};
char pos6[15][30] =
{ " ------------",
"| |",
"| ---",
"| |. .|",
"| | * |",
"| | v |",
"| ---",
"|",
"|",
"|",
"|",
"|",
"|",
"|",
"____"};
char pos5[15][30] =
{ " ------------",
"| |",
"| ---",
"| |. .|",
"| | * |",
"| | v |",
"| ---",
"| |",
"| |",
"| |",
"| |",
"|",
"|",
"|",
"____"};
char pos4[15][30] =
{ " ------------",
"| |",
"| ---",
"| |. .|",
"| | * |",
"| | v |",
"| ---",
"| |",
"| \\---|",
"| |",
"| |",
"|",
"|",
"|",
"____"};
char pos3[15][30] =
{ " ------------",
"| |",
"| ---",
"| |. .|",
"| | * |",
"| | - |",
"| ---",
"| |",
"| \\---|---/",
"| |",
"| |",
"|",
"|",
"|",
"____"};
char pos2[15][30] =
{ " ------------",
"| |",
"| ---",
"| |. .|",
"| | * |",
"| | o | ",
"| ---",
"| |",
"| \\---|---/",
"| |",
"| |",
"| / ",
"| / ",
"| / ",
"____"};
char pos1[15][30] =
{ " ------------",
"| |",
"| ---",
"| |. .|",
"| | * |",
"| | o | Dying...",
"| ---",
"| |",
"| \\---|---/",
"| |",
"| |",
"| / \\",
"| / \\",
"| / \\",
"____"};
char pos0[15][30] =
{ " ------------",
"| |",
"| ---",
"| |x x|",
"| | * |",
"| | x | Dead!",
"| ---",
"| \\",
"| \\---|---/",
"| |",
"| |",
"| / \\",
"| / \\",
"| / \\",
"____"};
void drawman(int outnum)
{
if (outnum == 7)
{
for (int j=0; j <= 14; j++)
cout << pos7[j] << endl;
}
if (outnum == 6)
{
for (int j=0; j <= 14; j++)
cout << pos6[j] << endl;
}
if (outnum == 5)
{
for (int j=0; j <= 14; j++)
cout << pos5[j] << endl;
}
if (outnum == 4)
{
for (int j=0; j <= 14; j++)
cout << pos4[j] << endl;
}
if (outnum == 3)
{
for (int j=0; j <= 14; j++)
cout << pos3[j] << endl;
}
if (outnum == 2)
{
for (int j=0; j <= 14; j++)
cout << pos2[j] << endl;
}
if (outnum == 1)
{
for (int j=0; j <= 14; j++)
cout << pos1[j] << endl;
}
if (outnum == 0)
{
for (int j=0; j <= 14; j++)
cout << pos0[j] << endl;
}
}
// Funktionsdeklaration
void playgame(char[], char[]);
void playhangman(void);
void beginround1(void);
void beginround2(void);
void beginround3(void);
void displayword(int wordsize, char* blankcp);
void rmletter(char bank[], char guess);
void disbank(char bank[]);
void clear(void);
void cont(void);
int getran(int j);
void drawman(int outnum);
// Globale
char choice; // Menüauswahl
void main() // Beginne Main Funktion
{
do
{
clear(); // Bildschirm löschen
cout << "HANGMAN\n\n" // Anzeige des Menüs
<< "1) Play Hangman\n"
<< "2) Quit\n\n";
cout << "Enter your choice: "; // Auf Eingabe des Users warten
cin >> choice; // Gewählte Option
if (choice == '1') // Wenn Play gewählt wurde, dann beginne mit der Funktion
playhangman();
} while (choice != '2'); // So lange wiederholen, bis 2 für Ende gedrückt wurde
}
void playhangman(void)
{
char choice2 = ' '; // Schwierigkeitsgrad wird initialisiert
do
{
clear(); // Bildschirm löschen
cout << "Difficulty Level\n\n" // Auswahl des Schwierigkeitsgrades
<< "1) Easy\n"
<< "2) Medium\n"
<< "3) Hard\n"
<< "4) Go to previous menu\n\n";
cout << "Enter your choice: "; // User muss nun wählen
cin >> choice2; // Eingabe
if (choice2 == '1') // Das Spiel wird mit der gewünschten Wahl des Users begonnen
beginround1();
if (choice2 == '2')
beginround2();
if (choice2 == '3')
beginround3();
if (choice2 == '4')
main();
// Loop: So lange wiederholen, bis eins der vorgegebenen Optionen gewählt wurde
} while(choice2 != '1' && choice2 != '2' && choice2 != '3' && choice2 != '4');
}
void beginround1(void)
{
int rannum = getran(12); // Eine Zufallszahl zwischen 1-10 für Easy
if (rannum == 1) // Wähle eins der zu eratenden Wörter aus (zufällig)
{
char easyword[] = "person"; // Das Wort wird in einem Array deklariert
char copy[sizeof(easyword)]; // Ein neues Array mit der selben Größe anlegen
playgame(easyword, copy);
}
if (rannum == 2) // Dies wird für alle Wörter wiederholt
{
char easyword[] = "expert";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 3)
{
char easyword[] = "poetry";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 4)
{
char easyword[] = "chief";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 5)
{
char easyword[] = "laugh";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 6)
{
char easyword[] = "problem";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 7)
{
char easyword[] = "forever";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 8)
{
char easyword[] = "answer";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 9)
{
char easyword[] = "wonder";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 10)
{
char easyword[] = "serial";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 11)
{
char easyword[] = "visual";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 12)
{
char easyword[] = "dwarf";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
}
void beginround2(void)
{
int rannum = getran(12); // Eine Zufallszahl zwischen 1-10 für Medium
if (rannum == 1) // Wähle eins der zu eratenden Wörter aus (zufällig)
{
char easyword[] = "impossible"; // Das Wort wird in einem Array deklariert
char copy[sizeof(easyword)]; // Ein neues Array mit der selben Größe anlegen
playgame(easyword, copy);
}
if (rannum == 2) // Dies wird für alle Wörter wiederholt
{
char easyword[] = "culinary";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 3)
{
char easyword[] = "incubus";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 4)
{
char easyword[] = "multimedia";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 5)
{
char easyword[] = "portfolio";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 6)
{
char easyword[] = "dedicated";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 7)
{
char easyword[] = "university";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 8)
{
char easyword[] = "capabilities";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 9)
{
char easyword[] = "parallel";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 10)
{
char easyword[] = "significant";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 11)
{
char easyword[] = "headline";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 12)
{
char easyword[] = "restaurant";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
}
void beginround3(void)
{
int rannum = getran(12);
if (rannum == 1)
{
char easyword[] = "congratulations";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 2)
{
char easyword[] = "architecture";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 3)
{
char easyword[] = "infrastructure";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 4)
{
char easyword[] = "jeopardize";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 5)
{
char easyword[] = "development";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 6)
{
char easyword[] = "interoperable";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 7)
{
char easyword[] = "hindrance";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 8)
{
char easyword[] = "commercialize";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 9)
{
char easyword[] = "theoretical";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 10)
{
char easyword[] = "reminiscent";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 11)
{
char easyword[] = "allegation";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
if (rannum == 12)
{
char easyword[] = "neighbouring";
char copy[sizeof(easyword)];
playgame(easyword, copy);
}
}
void playgame(char easyword[], char blank[])
{
clear(); // Bildschirm löschen
char unusedwords[] = "abcdefghijklmnopqrstuvwxyz";
int guessleft = 7;
int gotone = 0; // Prüft, ob Buchstaben mehr als einmal gedrückt wurden
int gotone2 = 0; // Prüft, ob die Buchstaben falsch sind
int size = 0; // Reserviert Platz für das zu eratende Wort
for(int i=0; easyword[i] != '\0'; i++) // So lange wiederholen, bis keine Zeichen mehr benötigt werden
{
size++; // Größe wird inkrementiert
blank[i] = '_'; // Pro Buchstabe, was in dem Wort enthalten ist wird ein '_' ausgegeben
}
blank[size] = '\0'; // Alle Felder werden gelöscht
char guessletter; // Variable für die zu eratenden Buchstaben
cout << "THE MYSTERY WORD IS " << size << " LETTERS LONG\n"; // Zeigt an wieviele Buchstaben das Wort enthält
cout << "\nThe mystery word is: ";
displayword(size, &blank[0]);
cout << "You have " << guessleft << " guesses\n\n";
drawman(guessleft);
cout << endl;
// Die Funktion zeigt an, welche Teile des Wortes richtig gelöst wurden
while (1) // So lange wiederholen, bis der User verliert oder gewinnt
{
--guessleft; // Letzter Versuch
cout << "Guess a letter: ";
cin >> guessletter; // Zeigt an, welchen Buchstaben der User gewählt hat
for (int i=0, gotone=0; easyword[i] != '\0'; i++)
{ // gotone wird resetiert und so bleiben die Versuche erhalten, wenn der User richtig geraten hat
if (guessletter == easyword[i]) // Wenn der User richtig ratet
{
++gotone;
blank[i] = guessletter;
if (gotone == 1)
{
++guessleft;
rmletter(unusedwords,guessletter);
}
}
}
clear();
cout << "THE MYSTERY WORD IS " << size << " LETTERS LONG\n\n";
if (gotone == 0)
{
rmletter(unusedwords,guessletter);
cout << "The letter " << guessletter << " is not in the mystery word\n";
}
else
cout << "The letter " << guessletter << " is in the mystery word\n";
cout << "The mystery word is: ";
displayword(size, &blank[0]);
cout << "You have " << guessleft << " guesses left.\n";
cout << "Remaining letters to guess: ";
disbank(unusedwords);
cout << endl << endl;
drawman(guessleft);
cout << endl;
if (!strcmp(blank,easyword))
{
cout << "You win! The mystery word was " << easyword << ".\n\n";
cont();
clear();
break;
}
if(guessleft <= 0)
{
cout << "You lost! The mystery word was " << easyword << "!\n\n";
cont();
clear();
break;
}
}
}
void clear(void){system("cls");}
void displayword(int wordsize, char* blankcp)
{
for (int c = 0; c <= wordsize-1; c++)
{
if(c<wordsize-1)
cout << *(blankcp+c) << " ";
if(c == wordsize-1)
cout << *(blankcp+c);
}
cout << "\n";
}
void rmletter(char bank[], char guess)
{
for (int i = 0; bank[i] != '\0'; i++)
{
if (bank[i] == guess)
{
bank[i] = ' ';
break;
}
}
}
void disbank(char bank[])
{
for (int i = 0; bank[i] != '\0'; i++)
{
if (bank[i] != ' ')
cout << bank[i];
}
void cont(void)
{
char choice3;
do {
cout << "Press 'Q' symbol to continue...";
cin >> choice3;
} while (choice3 = 'Q');
clear();
}
int getran(int j)
{
srand((unsigned)time(0));
return((rand()%j)+1);
}
ps: Hab es nicht geschafft fertig zu kommentieren. Aber ich denke ihr könnt sehen, was das programm mancht!
Vielen dank an euch alle!