1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#define MAX_WRONG 8
using namespace std;
int getRandom(); //gets a random number based on which a word is chosen.
int getCategory();//prompts user to choose category of word
void getWord(int, int, char []);//gets word from a list of words
void emptyBlanks(char [],int); //assigns '_' to all elements in array blank[].
void drawBlanks(char [],int);
bool check(char [],char [],int,char); //returns true if letter is in the word
bool isGameOver(char[],int); //checks if the user has guessed all letters.
void showWinScreen(); //screen displayed when user wins.
int main()
{
int random = getRandom();
int category = getCategory();
char word[100];
getWord(random,category,word);
int wrong = 0;
int length = strlen(word);
char blanks[length];
emptyBlanks(blanks,length);
do
{
drawBlanks(blanks,length);
cout<<"Wrong Guesses: "<<wrong<<" "<<random<<endl;
cout<<"\nEnter a letter: ";
char ch;
cin>>ch;
if(check(blanks,word,length,ch)==false){wrong++;}
if(isGameOver(blanks,length)==true){showWinScreen(); break;}
}while(wrong<=MAX_WRONG);
return 0;
}
void emptyBlanks(char blanks[],int length)
{
for(int i=0;i<length;i++)
{
blanks[i]='_';
}
}
void drawBlanks(char blanks[], int length)
{
for(int i=0;i<length;i++)
{
cout<<blanks[i]<<" ";
}
cout<<endl;
}
|