5
\$\begingroup\$

I would like feedback on this code.

This is a game of hangman which uses classes but my knowledge of this category is very limited so I did the best I could. I would like my code to become as professional as possible.
If anyone has any ideas or features they think I should add or even a project I should try to test my abilities please do tell me.

In the near future I plan to become a game developer so I believe this would be good experience for developing the mindset of making games for the general public.

#include <iostream>
#include <string>
#include <ctime>
#include<stdlib.h>
#include<windows.h>

using namespace std;


class Thegame
{
private:

    int tries=7;
    int showncounter;
    string response;//This stores the users secret word
    string shown="";// This is used to show user how far they have gotten in guessing

public:
    void resetword();// A function used to restore the hangman word for multiple uses
    void hangmandrawing();// A function used to draw the hangman figure
    void tryLetters();//function that used the different letters used in the hangman setting
    void storetheword(string HangmanWord);// this function stores the word in the private variable making it untouchable
    void addTries();
    void completehangman();
};
void Thegame::tryLetters()//the implementation of guessing the letters in a series of unhidden letters
{
    int loop=1;
    char letter;
    int showncounter=0;

    for(int i=0; i<(int)response.length(); i++)
    {
        if(isalnum(response[i]))//checks if the location of the string is a number or alphabet and then changes it to a -
        {
            shown=shown+"-";//this is it
            showncounter++;
        }
        else
        {
            shown=shown+response[i];//else if its something like a exclamation point or something like it leave it as it is
        }
    }
    cout<<shown<<endl;

    cout<<"What letter do you think is here?"<<endl;
    cin>>letter;

    for(int b=0; b<(int)response.size(); b++)
    {
        if(response[b]==letter || toupper(letter)==response[b])
        {
            shown[b]=letter;
        }

    }

    cout<<shown<<endl;
    cout<<"You have "<<tries<<" tries left."<<endl;
    while(loop)
    {
        int tracker=0;//used to keep track of the tries of the user for the hangman picture
        int showntracker=0;// this is used to keep track of the revised hashes to win the game

        cout<<"What letter do you think is here? or enter (?) to leave"<<endl;
        cin>>letter;

        if(letter=='?')
        {
            break;
        }
        for(int b=0; b<(int)response.size(); b++)
        {
            if(response[b]==letter || toupper(letter)==response[b])
            {
                shown[b]=letter;
                tracker=1;
            }
            else if(response[b]!=letter || response[b]!=toupper(letter))//iteration to keep track of any thing different we dont want
            {
                tracker=tracker+0;
            }
        }

        for(int d=0; d<showncounter; d++)
        {
            if(shown[d]=='-' || shown[d]==' ')
            {
                showntracker=1;
            }
            else if(shown[d]!='-')
            {
                showntracker=showntracker+0;
            }
        }

        if(tracker==0)
        {
            addTries();
            hangmandrawing();
        }

        if(showntracker==0 || shown==response)
        {
            loop=0;
            cout<<"Secret Word: "<<response<<endl;
            cout<<"CONGRATS YOU WIN!!!"<<endl;
            cout<<"Try the game again... Thank you"<<endl;
            resetword();
        }
        cout<<shown<<endl;

        if(tries>0 && showntracker!=0 && shown!=response)
        {
            cout<<"You have "<<tries<<" tries left."<<endl;
        }
        if(tries==0)
        {
            loop=0;
            cout<<"Sorry you lose"<<endl;
            cout<<"THE SECRET WORD WAS: "<<response<<endl;
            cout<<"Try the game again... Thank you"<<endl;
            resetword();

            cout<<endl;
            cout<<endl;
        }

    }
}
void Thegame::storetheword(string x)//simply placing the secret phrase in a private variable
{
    response=x;
}
void Thegame::resetword()//resetting the word and dashes back to its original nature for multiple attempts
{
    response="";
    shown="";
    tries=7;
}
void Thegame::addTries()
{
    tries--;
}
void Thegame::hangmandrawing()
{
    if(tries==6 || tries==7)
    {
        cout << " ___________"<<endl;
        cout << " |        }"<<endl;
        cout << " |         " <<endl;
        cout << "_|______________"<<endl;
    }
    else if(tries==5)
    {
        cout << " ___________"<<endl;
        cout << " |        }"<<endl;
        cout << " |       \\  " <<endl;
        cout << "_|______________"<<endl;
    }
    else if(tries==4)
    {
        cout << " ___________"<<endl;
        cout << " |         }"<<endl;
        cout << " |       \\ 0 " <<endl;
        cout << "_|______________"<<endl;
    }
    else if(tries==3)
    {
        cout << " ___________"<<endl;
        cout << " |         }"<<endl;
        cout << " |       \\ 0 /" <<endl;
        cout << "_|______________"<<endl;
    }
    else if(tries==2)
    {
        cout << " ___________"<<endl;
        cout << " |        }"<<endl;
        cout << " |      \\ 0 /" <<endl;
        cout << " |        |"<<endl;
        cout << "_|______________"<<endl;
    }
    else if(tries==1)
    {
        cout << " ___________"<<endl;
        cout << " |         }"<<endl;
        cout << " |       \\ 0 /"<<endl;
        cout << " |         | "<<endl;
        cout << " |        /  "<<endl;
        cout << "_|______________"<<endl;
    }
    else
    {
        cout << " ___________"<<endl;
        cout << " |         }"<<endl;
        cout << " |       \\ 0 /"<<endl;
        cout << " |         | "<<endl;
        cout << " |        / \\ "<<endl;
        cout << "_|______________"<<endl;
    }
}
int main()
{
    Thegame obj;
    int choice;//the bread and butter of the classes letter the user over and over until they get tired
    int loop=1;//used for the while loop below
    char ans='n';//the original condition of the while loop
    string usersWord;// the secret word the user enters
    string topic;// the topic for the hangman game

    while(ans=='n' || ans=='N')
    {
        cout << "Hello, my name is **Timmy Turner** and WELCOME TO MY GAME OF HANGMAN!!!!!!!!! " << endl;
        cout<<endl;
        cout<<"RULE#1: This is a 2+ player game"<<endl;
        cout<<"RULE#2: PLAYER 1,(has to do the following things below) "<<endl;
        cout<<"When you are past this screen: PLEASE!!! Enter the #1 AND and follow it by doing #2 "<<endl;
        cout<<"RULE#3: PLAYER 2,(After Player 1, has did all the previous things >> press any key to your choosing)"<<endl;
        cout<<"GOOD LUCK!!!!"<<endl;
        cout<<endl;
        cout<<"Once you read the instructions press any key to continue"<<endl;
        cin>>ans;
        system("CLS");
    }
    while(loop)
    {
        cout<<"***************************"<<endl;
        cout<<"1)PUT IN THE WORD         *"<<endl;
        cout<<"2)INSERT IN A TOPIC       *"<<endl;
        cout<<"3)SEE THE TOPIC           *"<<endl;
        cout<<"4)START GUESSING          *"<<endl;
        cout<<"5)RESET EVERYTHING        *"<<endl;
        cout<<"6)LEAVE THE GAME          *"<<endl;
        cout<<"***************************"<<endl;
        cout<<"Your choice here >";
        cin>>choice;
        if(choice==1)
        {
            HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
            DWORD mode = 0;
            GetConsoleMode(hStdin, &mode);
            SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
            cout << "Put in the secret phrase" << endl;
            cin.ignore();
            getline(cin,usersWord);
            SetConsoleMode(hStdin, mode);
            obj.storetheword(usersWord);
            cout<<"YOUR SECRET WORD WAS SUCCESSFUL!!"<<endl;
            cout<<endl;
            cout<<endl;
        }

        else if(choice==2)
        {
            cout<<"Enter a topic for the player who is guessing"<<endl;
            cin.ignore();
            getline(cin,topic);
            cout<<endl;
            cout<<endl;
        }
        else if(choice==3)
        {
            cout<<"The Topic is:"<<topic<<endl;
            cout<<"------------------------------"<<endl;
            cout<<endl;
            cout<<endl;

        }
        else if(choice==4)
        {
            obj.tryLetters();
        }
        else if(choice==5)
        {
            obj.resetword();
            cout<<"EVERYTHING HAS BEEN RESET"<<endl;
            cout<<endl;
            cout<<endl;
        }
        else if(choice==6)
        {
            cout<<"Thank you for trying out the game."<<endl;
            cout<<"Peace!!"<<endl;
            loop=0;
        }

    }

}
\$\endgroup\$
4
\$\begingroup\$

There is a lot to cover here.
Some of the more obvious things:

  • Includes

You say you want the code to be professional yet the includes are not properly formatted (missing a space) which looks like copy and paste code.
Apart from that you should not use C headers in C++. Especially not ones that aren't cross-platform.

  • The ever popular using namespace std

Don't use using namespace std Here is a simple explanation why.

  • Inconsistent naming

The way you name things (variables, functions really just everything) is quite frankly very bad.

You have several cases of all lowercase names and then some with camelCase. While I am personally a fan of snake_case ultimately the most important thing is that you pick a style and consistently use it.

Apart from the visual aspect of your naming you also choose quite non-descriptive names a lot of times.
Why is your class named Thegame why not Hangman?
Why response? Who's response?
Try to choose names that describe what the variable does or what it is used/intended for. Avoid overly ambiguous names.

  • Comments

In their current form your comments do more harm than good.
Generally you want comments to explain why instead of what. A lot of times comments aren't needed if you choose clear variable names that speak for themselves.

  • For loops

Why are you casting length? Why are you using both i and b for your index?
In general you should avoid evaluating things in the loop header as it can be costly.
Use the prefix operator whenever possible over the postifx one (prefer ++i over i++).

  • Prefer \n over endl

endl will flush the buffer so unless you want that just use \n.

  • Declare variables when they are needed

Don't declare all your variables at the top, instead declare them as close to the position where they are needed.

  • Infinite loops

Shamelessly stolen from @Deduplicator here

for(;;) is the more idiomatic infinite loop, read as "forever".

Prefer this to your strange construct of declaring 1 somewhere and then later using it for an endless while loop.

  • If possible make your code cross-platform

You are using several headers/functions that will only compile on Windows. In particular the part where you read user input needs to be revisited.

  • Consider using switch

You are using a lot of if else which could be rewritten using switch

\$\endgroup\$
  • \$\begingroup\$ Thank you for the advice. I'm a little confused on what you mean when you say cross-platform and regards to the switch statement I will try and make a revised version using them. \$\endgroup\$ – Improve Every Day Jul 8 '17 at 1:49
  • \$\begingroup\$ @ImproveEveryDay In general you should try to write code that works on different platforms (i.e. Windows, Linux, MacOS etc.). I've had to retroactively make someone else's code cross-platform before and it was quite agonizing. So in short try to avoid writing code that is platform exclusive. \$\endgroup\$ – yuri Jul 8 '17 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.