Klistra in koden i ett programmeringsprogram och kompilera. Koden är gjord med Dev-C++ men även andra program som Code::Blocks fungerar.
// Hänga gubbe - Hangman, by http://joolzblogg.blogspot.com/ #include <iostream> #include <string> #include <ctime> #include <cstdlib> using namespace std; void presentation(void); void rensaSkarm(void); int ordLangd(string ordet); void fyllOrdetDolt(string ordetDolt, int langd); void kontroll(char gissning, string ordetDolt, string ordet, int langd, int& antalFel, char felGissningar[]); void resultat(string ordet, string ordetDolt, int antalFel); bool jamforFalt(string ordet, string ordetDolt, int langd); void ritaGubbe(int antalFel); void omvandla(char& gissning); void tillkalla(string ordetDolt, int langd); int main() { char koraigen; char meny; int slumpord; char felGissningar[10]; do { int antalFel = 0; string ordetDolt; char gissning; presentation(); cout << "\tMENY" << endl; cout << "1. Skriv in eget ord" << endl; cout << "2. L\x86t datorn slumpa fram ett ord" << endl; cout << "Ditt val: "; cin >> meny; cout << endl; string ordet; if (meny == '1') { cout << "Skriv ordet: "; cin >> ordet; rensaSkarm(); } else if (meny == '2') { srand(time(0)); slumpord = rand()%16 + 1; switch (slumpord) { case 1: ordet = "mobil"; break; case 2: ordet = "lastbil"; break; case 3: ordet = "bokhylla"; break; case 4: ordet = "datorspel"; break; case 5: ordet = "standardbibliotek"; break; case 6: ordet = "kol"; break; case 7: ordet = "jojjo"; break; case 8: ordet = "hangman"; break; case 9: ordet = "oliver"; break; case 10: ordet = "algoritm"; break; case 11: ordet = "flagga"; break; case 12: ordet = "kassettband"; break; case 13: ordet = "skolarbete"; break; case 14: ordet = "nationalencyklopedin"; break; case 15: ordet = "wikipedia"; break; case 16: ordet = "underverk"; break; } } else { rensaSkarm(); cout << "Felinmatning! Ordet s\x84tts till \"loser\" f\x94r att p\x86minna om dig! \n"; ordet = "loser"; } int langd = ordLangd(ordet); fyllOrdetDolt(ordetDolt, langd); tillkalla(ordetDolt, langd); do { cout << "\nGissa p\x86 en bokstav: "; cin >> gissning; omvandla(gissning); rensaSkarm(); kontroll(gissning, ordetDolt, ordet, langd, antalFel, felGissningar); cout << "Felaktiga gissningar: " << antalFel << endl; cout << "Testade bokst\x84ver: "; for (int b = 0; b < antalFel; b++) { cout << felGissningar[b]; cout << ' '; } cout << endl; cout << endl; tillkalla(ordetDolt, langd); ritaGubbe(antalFel); }while (jamforFalt(ordet, ordetDolt, langd) && antalFel < 6); resultat(ordet, ordetDolt, antalFel); cout << "Vill du spela igen? (j/n) "; cin >> koraigen; rensaSkarm(); }while (koraigen == 'j' || koraigen == 'J'); system("PAUSE"); return 0; } void presentation(void) { cout << "\t V\x84lkommen till Hangman 1.0 \n"; cout << "Instruktioner: Ett ord har valts ut. Gissa p\x86 en bokstav och se \n"; cout << "om den finns med i ordet. Spelet \x84r slut n\x84r du \n"; cout << "har kommit p\x86 ordet eller efter sex felaktiga gissningar. \n\n"; } void rensaSkarm(void) { cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; } int ordLangd(string ordet) { int langd; langd = ordet.size(); return langd; } void fyllOrdetDolt(string ordetDolt, int langd) { for (int i = 0; i < langd; i++) { ordetDolt[i] = '*'; } } void kontroll(char gissning, string ordetDolt, string ordet, int langd, int& antalFel, char felGissningar[]) { int temp = 0; for (int y = 0; y < langd; y++) { if (gissning == ordet[y]) { ordetDolt[y] = gissning; } else { temp++; } } if (temp == langd) { felGissningar[antalFel] = gissning; antalFel++; } } void resultat(string ordet, string ordetDolt, int antalFel) { if (antalFel < 6) { cout << "\nDu vann! Du kn\x84" << "ckte ordet " << ordet << ". \n\n"; } else { cout << "\nDu f\x94rlorade! Ordet var " << ordet << ". \n\n"; } } bool jamforFalt(string ordet, string ordetDolt, int langd) { bool svar; int temp = 0; for (int z = 0; z < langd; z++) { if (ordet[z] == ordetDolt[z]) { temp++; } } if (temp != langd) { svar = true; } else { svar = false; } return svar; } void ritaGubbe(int antalFel) { if (antalFel == 1) { cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " :''''''''''''''''''''''': " << endl; cout << " : : " << endl; cout << " :...........................: " << endl; cout << " " << endl; } if (antalFel == 2) { cout << " " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " :''''''''''''''''''''''': " << endl; cout << " : : " << endl; cout << " :...........................: " << endl; cout << " " << endl; } if (antalFel == 3) { cout << " " << endl; cout << " | / " << endl; cout << " | / " << endl; cout << " |/ " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " :''''''''''''''''''''''': " << endl; cout << " : : " << endl; cout << " :...........................: " << endl; cout << " " << endl; } if (antalFel == 4) { cout << " _____________ " << endl; cout << " | / " << endl; cout << " | / " << endl; cout << " |/ " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " :''''''''''''''''''''''': " << endl; cout << " : : " << endl; cout << " :...........................: " << endl; cout << " " << endl; } if (antalFel == 5) { cout << " _____________ " << endl; cout << " | / ! " << endl; cout << " | / ! " << endl; cout << " |/ ! " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " | " << endl; cout << " :''''''''''''''''''''''': " << endl; cout << " : : " << endl; cout << " :...........................: " << endl; cout << " " << endl; } if (antalFel == 6) { cout << " _____________ " << endl; cout << " | / ! " << endl; cout << " | / ! " << endl; cout << " |/ ! " << endl; cout << " | o " << endl; cout << " | -|- " << endl; cout << " | / \\ " << endl; cout << " | " << endl; cout << " | " << endl; cout << " :''''''''''''''''''''''': " << endl; cout << " : : " << endl; cout << " :...........................: " << endl; cout << " " << endl; } } void omvandla(char& gissning) { switch (gissning) { case 'A': gissning = 'a'; break; case 'B': gissning = 'b'; break; case 'C': gissning = 'c'; break; case 'D': gissning = 'd'; break; case 'E': gissning = 'e'; break; case 'F': gissning = 'f'; break; case 'G': gissning = 'g'; break; case 'H': gissning = 'h'; break; case 'I': gissning = 'i'; break; case 'J': gissning = 'j'; break; case 'K': gissning = 'k'; break; case 'L': gissning = 'l'; break; case 'M': gissning = 'm'; break; case 'N': gissning = 'n'; break; case 'O': gissning = 'o'; break; case 'P': gissning = 'p'; break; case 'Q': gissning = 'q'; break; case 'R': gissning = 'r'; break; case 'S': gissning = 's'; break; case 'T': gissning = 't'; break; case 'U': gissning = 'u'; break; case 'V': gissning = 'v'; break; case 'X': gissning = 'x'; break; case 'Y': gissning = 'y'; break; case 'Z': gissning = 'z'; break; case 'Å': gissning = 'å'; break; case 'Ä': gissning = 'ä'; break; case 'Ö': gissning = 'ö'; break; } } void tillkalla(string ordetDolt, int langd) { cout << "Ordet: "; for (int h = 0; h < langd; h++) { cout << ordetDolt[h]; } cout << endl; }