In: Computer Science
using the C programming language I need a program that includes comments/ screen shots that it works/ and the code it self
for a hang man game
this game has to be to complie no errors please you can even keep it super basic if u want and no( studio.c) that never works THANK UUUUUU
Please find the pseudo code below:
[PS: Its basic program. Error handling needs to be added.
Also when word is less than 8 and even if you enter the correct letters the while loop continues for complete 8 times.]
#include <iostream> #include <string> using namespace std; #define MAX_TRY 8 void draw(char *part) { cout << "\t____________________________________________" << endl << endl; cout << "\t| " << part[0] << endl; cout << "\t| " << part[1] << endl; cout << "\t| " << part[2] << endl; cout << "\t| " << part[3] << " " << part[4] << endl; cout << "\t| " << part[5] << endl; cout << "\t| " << part[6] << " " << part[7] << endl; cout << "\t|" << endl; cout << "\t____________________________________________" << endl; } bool checkLetter(string word, string letter) { if (word.find(letter) != string::npos) return 1; // Letter found else return 0; // Letter not found } int main() { string country[] = {"America", "Australia", "Canada", "Ireland", "London"}; char part[MAX_TRY] = {}; unsigned char count = 0, error = 0; string word, letter; int n = rand() % 6; // To randomly choose from country[] cout << "Welcome to Hangman" << endl; word = country[0]; while (count < MAX_TRY) { // Check if it hits maximum tries or if it crosses the word size cout << " Enter your input:"; cin >> letter; bool isFound = checkLetter(word, letter); // Check if the letter exist in the word or not if (!isFound) { switch (error) { //Enter new element in Hangman Picture case 0: part[0] = '|'; break; case 1: part[1] = '|'; break; case 2: part[2] = 'O'; break; case 3: part[3] = '/'; break; case 4: part[4] = '\\'; break; case 5: part[5] = '|'; break; case 6: part[6] = '/'; break; case 7: part[7] = '\\'; break; } error++; draw(&part[0]); } count++; } cout << endl; cout << "========================================" << endl; if ((error < MAX_TRY) && (error < word.size())) { cout << "\tYou Won !!!!!!!!!!" << endl; } else { cout << "\tYou Lose !!!!!!!!!!" << endl; } cout << "\tAnswer: " << word << endl; cout << "========================================" << endl << endl; return 0; }