Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <time.h>
- using namespace std;
- char toUpper(char c) {
- return (c >= 'a' && c <= 'z' ? 'A' + c - 'a' : c);
- }//end of toUpper
- bool contains(char str[], char c) {
- int i = 0;
- while (str[i] != 0) {
- if (str[i] == c)
- return true;
- i++;
- }//end of while
- return false;
- }//end of contains
- int main() {
- srand(time(0));
- char words[10][11] = { "ABC","BAD","CAT","DAD","FREE","GREAT","HAPPY","ISLAND","JACK" };
- int pickedWord = rand() % 10;
- cout << words[pickedWord] << endl;
- char usedLetters[26] = {};
- char c;
- int attemps = 0;
- while (attemps < 10) {
- if (attemps == 5) {
- int i = 0;
- int missingLetters = 0;
- int userMissed = 0;
- while (words[pickedWord][i]) {
- char letterInWord = words[pickedWord][i];
- if (usedLetters[letterInWord - 'A'] != 1)
- userMissed++;
- i++;
- }//end of while
- cout << "There are " << userMissed << " letters left in the word.\n";
- }//end of if
- cout << "Enter a letter:";
- cin >> c;
- c = toUpper(c);
- if (usedLetters[c - 'A'] == 1) {
- cout << "You've already tried letter " << c << endl;
- continue;
- }//end of if
- else
- usedLetters[c - 'A'] = 1;
- if (contains(words[pickedWord], c)) {
- cout << "Letter " << c << " is correct!\n";
- bool win = true;
- int i = 0;
- while (words[pickedWord][i]) {
- char letterInWord = words[pickedWord][i];
- if (usedLetters[letterInWord - 'A'] != 1) {
- win = false;
- break;
- }//end of if
- i++;
- }//end of while
- if (win) {
- cout << "You've won with only " << attemps << " wrong answer! The word was " << words[pickedWord] << endl;
- return 0;
- }//end of if
- }//end of if
- else {
- attemps++;
- cout << "Incorrect! You have " << 10 - attemps << " tries left!\n";
- }//end of else
- }//end of while
- cout << "You lose! The word was " << words[pickedWord] << "!\n";
- return 0;
- }//end of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement