Word Guess Game in C++

#include<iostream>
#include<time.h>
using namespace std;



int main(){
    srand(time(0));
    int r = rand()%10;
    char userWord[100];
    
    char words[10][10] = {
    "apple",
    "banana",
    "grape",
    "orange",
    "green",
    "Alphabet",
    "box",
    "magnet",
    "hand",
    "keyboard"
};
    // random words length
    int len = strlen(words[r]);
    
    for(int i = 0; i<strlen(words[r]); i++){
        if(i == 0){
            cout<<words[r][i];
        }
        else{
            cout<<"*";
        }
        
    }   
    
    cout<<"\nword has "<<len<<" letters\n";
    
    // chances user get to guess
    int n = 5;
    
    while(n > 0){
    cout<<"\nyou get "<<n<<" chances:\n";
    cout<<"\nGuess the word: \n";
    cout<<":-";
    cin>>userWord;
    
    for(int i = 0; i<strlen(userWord); i++){
        userWord[i] = tolower(userWord[i]);
    }
    
    if(strlen(userWord) > strlen(words[r])){
        cout<<"\nmore then the word's length:\n";
    }
    
    if(strlen(userWord) < strlen(words[r])){
        cout<<"\nless then the word's length:\n";
    }
    if(strcmp(userWord,words[r]) == 0){
        cout<<"\n****your guess is correct: "<<words[r];
        break;
    }
    
    else{
        for(int i = 0; i<len; i++){
            if(words[r][i] == userWord[i]){
                cout<<"\n letter "<<words[r][i]<<" at "<<i<< " is correct \n";
            }
        }
        
        for(int i = 0; i<len; i++){
        for(int j = i+1; j<len; j++){
            if(words[r][i] == userWord[j]){
                cout<<"\n letter "<<userWord[j]<<" at "<<j<< " is correct but misplaced \n";
            }
            }
        }
        
    }    


    n--;
    if(n < 1){
        cout<<"Game Over\n";
    }

 
}

Comments

Popular posts from this blog

AI and Human Evolution

To Do List App (GUI) using C#

Snake Game using JAVA