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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "conio.h"
using namespace std;
const int Max_Points=5;
int letterFill (char, string, string&);
string words_rand ();
string words_rand ()
{
string word;
string words[] ={"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};
srand(time(0));
int i=rand()% 20;
word = words[i];
return word;
}
int main (){
char letter;
int Points=0;
string word;
string words[] ={"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};
word = words_rand ();
string unknown(word.length(),'.');
do
{
cout << "\n" << unknown;
cout << "\nGuess word: ";
letter = getch();
system("cls");
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "there is no such letter:" << endl;
Points++;
}
else
{
cout << endl << "Correct letter: " << endl;
}
cout << "You have: " << Points;
cout << " points." << endl;
if (word==unknown)
{
cout << word << endl;
cout << "Whoop, whoop! You guessed a word!";
word = words_rand ();
string unknown(word.length(),'.');
}
if(Points == Max_Points || letter == '0' )
{
cout << "\nGAME OVER" << endl;
cout << "Word : " << word << endl;
system("pause");
return 0;
}
}
while (Points < Max_Points);
cin.ignore();
cin.get();
system("pause");
return 0;
}
int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
if (guess == guessword[i])
return 0;
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
|