3 of 5
tag
dfhwze
  • 13.9k
  • 3
  • 37
  • 100

CLI Hangman game in C++

Multiple questions regarding this have already been asked, but as this is my first time to the site, I really don't know if code review welcomes multiple similar questions, but here I go..

Well this is a basic hangman game in c++ without the classy man getting hanged animation (life is printed instead)

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <time.h>
#include <fstream>

using namespace std;

string previousGuesses;     //string to hold previous guesses

class movie {
    private:
        string name;
        int _size;

    public:
        movie(void);
        inline string getName(void) {
            return this->name;
        }
        inline int getSize(void) {
            return this->_size;
        }
};

movie::movie(void) {
    ifstream fin;
    fin.open("movies.txt", ios::in);
    // int random_integer = rand();
    getline(fin, this->name);       //currently takes the first line as movie name. Randomising logic will be inserted....any hints??
    this->_size = name.size();
    fin.close();
}

int numOfSpaces(string name) {
    register unsigned counter = 0;
    for (register int i = 0; i < name.size(); i++) {
        if(name[i] == ' ') {
            counter++;
        }
    }
    return counter;
}

bool guessedPreviously(char ch) {
    if(previousGuesses.find(ch) != -1) {        //if guess is found, return true
        return true;
    }
    previousGuesses += ch;
    return false;
}

void print(char name[], int size) {
    for (register int i = 0; i < size; i++) {
        cout << name[i];
    }
    cout << endl;
}

int main(int argc, char const* argv[]) {
    /*to get name of the movie*/
    srand(time(NULL)); //seed for rand();
    register int counter = 0;   //used to check if the full guess has been finally made though better logic is welcome
    register int life = 10;     //hold the lives user has
    movie m1;           //object of movie class
    char name[m1.getSize()];    //temp char[] (used to mask/unmask movie name)
    char guess;                 //shall hold user's guess
    string _name = m1.getName();    //a variable to store the movie name (needed as m1.getName()[i] surprisingly doesn't work....why??)
    for (register int i = 0; i < m1.getSize(); i++) {
        /*logic to keep spaces, spaces in the temp char[]*/
        if(_name[i] != ' ')
            name[i] = '*';
        else
            name[i] = ' ';
    }
    while (1) {     //runs the game loop
        /*User's guess*/
        cout << "Enter a guess: ";
        cin >> guess;
        if(guessedPreviously(guess)) {
            cout << "guess already made!!" << endl;
            life--;
            cout << life << endl;
            if (life == 0) {
                cout << "game over!!!" << endl;
                cout << m1.getName() << endl;
                exit(0);
            }
            continue;
        }
        cout << endl;               //I/O formatting ...... because we need it!!
        /*check guess*/
        bool check = false;         //boolean variable to umm.. not let the else if get unnecessarily executed
        for (register int i = 0; i < _name.size(); i++) {
            if(_name[i] == guess) {
                name[i] = guess;    //replaces * in temporary char[] at all places where guess is correct
                check = true;
                /*To check if the full guess has finally been made!!*/
                counter++;
                if(counter == (m1.getSize() - numOfSpaces(m1.getName()))) {     //m1.getSize() also returns spaces, which are not being guessed
                    cout << "congrats!! You've won" << endl;
                    exit(0);
                }
            }
            else if (i == _name.size() - 1 && !check) {     //the 'what to do if guess is wrong??' logic
                cout << "wrong guess!!" << endl;
                life--;     //kill the user once XD
                cout << "You have " << life << " guesses left!!" << endl;
                if (life == 0) {
                    cout << "game over!!!" << endl;
                    cout << m1.getName() << endl;
                    exit(0);
                }
            }
        }
        /*unmask*/
        print(name, m1.getSize());
    }

    return 0;
}

All kinds of improvements are welcome :)

Also how do I make the code case insensitive efficiently??