2
\$\begingroup\$

It is an extremely basic mini-game menu. Link to my game.

I know there are more experienced C++ programmers than me, so it would be a great help if you help make my code better.

Thank you.

PS: First time using Stack Exchange; please help me with how I can make my post better.

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <locale>
#include <unistd.h>


// Hangman Game
void greet()
{

    std::cout << "=============\n";
    std::cout << "UFO: The Game\n";
    std::cout << "=============\n";
    std::cout << "Instructions: save your friend from alien abduction by guessing the letters in the codeword.\n";
}

void display_status(std::vector<char> incorrect, std::string answer)
{

    std::cout << "\nIncorrect Guesses:\n";

    for (int i = 0; i < incorrect.size(); i++)
    {
        std::cout << incorrect[i] << ' ';
    }

    std::cout << "\nCodeword:\n";

    for (int i = 0; i < answer.length(); i++)
    {
        std::cout << answer[i] << ' ';
    }
}

void end_game(std::string answer, std::string codeword)
{

    if (answer == codeword)
    {

        std::cout << "Hooray! You saved the person and earned a medal of honor!\n";
    }
    else
    {

        std::cout << "Oh no! The UFO just flew away with another person!\n";
    }
}

void display_misses(int misses)
{

    if (misses == 0 || misses == 1)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              /     \\        (  Send help! ) \n";
        std::cout << "             /   0   \\      / `-----------'  \n";
        std::cout << "            /  --|--  \\    /                 \n";
        std::cout << "           /     |     \\                     \n";
        std::cout << "          /     / \\     \\                   \n";
        std::cout << "         /               \\                   \n";
    }
    else if (misses == 2)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              /  0  \\        (  Send help! ) \n";
        std::cout << "             / --|-- \\      / `-----------'  \n";
        std::cout << "            /    |    \\    /                 \n";
        std::cout << "           /    / \\    \\                    \n";
        std::cout << "          /             \\                    \n";
        std::cout << "         /               \\                   \n";
    }
    else if (misses == 3)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              /--|--\\        (  Send help! ) \n";
        std::cout << "             /   |   \\      / `-----------'  \n";
        std::cout << "            /   / \\   \\    /                \n";
        std::cout << "           /           \\                     \n";
        std::cout << "          /             \\                    \n";
        std::cout << "         /               \\                   \n";
    }
    else if (misses == 3)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              /--|--\\        (  Send help! ) \n";
        std::cout << "             /   |   \\      / `-----------'  \n";
        std::cout << "            /   / \\   \\    /                \n";
        std::cout << "           /           \\                     \n";
        std::cout << "          /             \\                    \n";
        std::cout << "         /               \\                   \n";
    }
    else if (misses == 4)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              /  |  \\        (  Send help! ) \n";
        std::cout << "             /  / \\  \\      / `-----------' \n";
        std::cout << "            /         \\    /                 \n";
        std::cout << "           /           \\                     \n";
        std::cout << "          /             \\                    \n";
        std::cout << "         /               \\                   \n";
    }
    else if (misses == 5)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              / / \\ \\        (  Send help! )\n";
        std::cout << "             /       \\      / `-----------'  \n";
        std::cout << "            /         \\    /                 \n";
        std::cout << "           /           \\                     \n";
        std::cout << "          /             \\                    \n";
        std::cout << "         /               \\                   \n";
    }
    else if (misses == 6)
    {

        std::cout << "                 .                            \n";
        std::cout << "                 |                            \n";
        std::cout << "              .-\"^\"-.                       \n";
        std::cout << "             /_....._\\                       \n";
        std::cout << "         .-\"`         `\"-.                  \n";
        std::cout << "        (  ooo  ooo  ooo  )                   \n";
        std::cout << "         '-.,_________,.-'    ,-----------.   \n";
        std::cout << "              /     \\        (  Send help! ) \n";
        std::cout << "             /       \\      / `-----------'  \n";
        std::cout << "            /         \\    /                 \n";
        std::cout << "           /           \\                     \n";
        std::cout << "          /             \\                    \n";
        std::cout << "         /               \\                   \n";
    }
}
// End Hangman game functions


// Tic Tac Toe Game

std::string board[9] = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
int player = 1;
int position = 0;

void introduction()
{

    std::cout << "Press [Enter] to begin: ";
    std::cin.ignore();

    std::cout << "\n";

    std::cout << "===========\n";
    std::cout << "Tic-Tac-Toe\n";
    std::cout << "===========\n\n";

    std::cout << "Player 1) X\n";
    std::cout << "Player 2) 0\n\n";

    std::cout << "Here's the 3 x 3 grid:\n\n";

    std::cout << "     |     |      \n";
    std::cout << "  1  |  2  |  3   \n";
    std::cout << "_____|_____|_____ \n";
    std::cout << "     |     |      \n";
    std::cout << "  4  |  5  |  6   \n";
    std::cout << "_____|_____|_____ \n";
    std::cout << "     |     |      \n";
    std::cout << "  7  |  8  |  9   \n";
    std::cout << "     |     |      \n\n";
}

bool is_winner()
{

    bool winner = false;
    // rows
    if ((board[0] == board[1]) && (board[1] == board[2]) && board[0] != " ")
    {
        winner = true;
    }
    else if ((board[3] == board[4]) && (board[3] == board[5]) && board[3] != " ")
    {
        winner = true;
    }
    else if ((board[6] == board[7]) && (board[6] == board[8]) && board[6] != " ")
    {
        winner = true;
    }
    // columns
    else if ((board[0] == board[3]) && (board[0] == board[6]) && board[0] != " ")
    {
        winner = true;
    }
    else if ((board[1] == board[4]) && (board[1] == board[7]) && board[1] != " ")
    {
        winner = true;
    }
    else if ((board[2] == board[5]) && (board[2] == board[8]) && board[2] != " ")
    {
        winner = true;
    } // diagonals
    else if ((board[0] == board[4]) && (board[0] == board[8]) && board[0] != " ")
    {
        winner = true;
    }
    else if ((board[2] == board[4]) && (board[2] == board[6]) && board[2] != " ")
    {
        winner = true;
    }

    return winner;
}

bool filled_up()
{

    bool filled = true;

    for (int i = 0; i < 9; i++)
    {

        if (board[i] == " ")
        {

            filled = false;
        }
    }

    return filled;
}
void draw()
{

    std::cout << "     |     |      \n";

    std::cout << "  " << board[0] << "  |  " << board[1] << "  |  " << board[2] << "\n";

    std::cout << "_____|_____|_____ \n";
    std::cout << "     |     |      \n";

    std::cout << "  " << board[3] << "  |  " << board[4] << "  |  " << board[5] << "\n";

    std::cout << "_____|_____|_____ \n";
    std::cout << "     |     |      \n";

    std::cout << "  " << board[6] << "  |  " << board[7] << "  |  " << board[8] << "\n";
    std::cout << "     |     |      \n";

    std::cout << "\n";
}

void set_position()
{

    std::cout << "Player " << player << "'s Turn (Enter 1-9): ";

    while (!(std::cin >> position))
    {

        std::cout << "Player " << player << ", please enter a valid number between 1 and 9: ";
        std::cin.clear();
        std::cin.ignore();
    }

    std::cout << "\n";

    while (board[position - 1] != " ")
    {

        std::cout << "Oops, there's already something in that position!\n\n";

        std::cout << "Player " << player << "'s Turn (Enter 1-9): ";
        std::cin >> position;

        std::cout << "\n";
    }
}

void update_board()
{

    if (player % 2 == 1)
    {

        board[position - 1] = "X";
    }
    else
    {

        board[position - 1] = "0";
    }
}

void change_player()
{

    if (player == 1)
    {

        player++;
    }
    else
    {

        player--;
    }
}

void take_turn()
{

    while (!is_winner() && !filled_up())
    {

        set_position();

        update_board();

        change_player();

        draw();
    }
}

void end_game()
{

    if (is_winner())
    {
        std::cout << "There's a winner!\n";
    }
    else if (filled_up())
    {
        std::cout << "There's a tie!\n";
    }
}

// End Tic Tac Toe functions



// Now for the intro

void first_thing() {
    std::cout << "What game do you want to play?\n";
    std::cout << "1) Hangman with UFO\n";
    std::cout << "2) Tic Tac Toe\n";
    std::cout << "3) Rock Paper Scisors\n";
    std::cout << "4) Memory Game\n";
    int game;
    std::cin >> game;


    // Hangman scenario
    if (game == 1)
    {

        greet();

        std::string codeword = "codecademy";
        std::string answer = "__________";
        int misses = 0;
        std::vector<char> incorrect;
        bool guess = false;
        char letter;

        while (answer != codeword && misses < 7)
        {
            

            std::cout << "Welcome to tic tac toe\n";
            display_misses(misses);
            display_status(incorrect, answer);

            std::cout << "\n\nPlease enter your guess: ";
            std::cin >> letter;

            for (int i = 0; i < codeword.length(); i++)
            {

                if (letter == codeword[i])
                {

                    answer[i] = letter;
                    guess = true;
                }
            }

            if (guess)
            {

                std::cout << "\nCorrect!\n";
            }
            else
            {

                std::cout << "\nIncorrect! The tractor beam pulls the person in further.\n";
                incorrect.push_back(letter);
                misses++;
            }

            guess = false;
        }

        end_game(answer, codeword);
    }


    // Tic Tac Toe Scenario

    else if (game == 2) {


        std::cout << "Welcome to Tic Tac Toe Game!!\n";
        introduction();

        take_turn();

        end_game();
    }


    // Rock Paper Scissors Scenario

    else if (game == 3) {
        srand(time(NULL));

        int computer = std::rand() % 3 + 1;

        int user;

        std::cout << "====================\n";
        std::cout << "rock paper scissors!\n";
        std::cout << "====================\n";

        std::cout << "1) Rock\n";
        std::cout << "2) Paper\n";
        std::cout << "3) Scissors\n";

        std::cout << "SHOOT!!\n ";

        std::cin >> user;

        if (user == 1)
            std::cout << "you choose: Rock\n";
        else if (user == 2)
        {

            std::cout << "you choose: Paper\n";
        }

        else
        {
            std::cout << "you choose: Scissors\n";
        }
        if (computer == 1)
        {
            std::cout << "cpu choose: Rock\n";
        }
        else if (computer == 2)
        {
            std::cout << "cpu choose: Paper\n";
        }
        else
        {
            std::cout << "cpu choose: Scissors\n";
        }
        if (user == computer)
        {

            std::cout << "it's a tie!\n";
        }

        // user rock

        else if (user == 1)
        {

            if (computer == 2)
            {

                std::cout << "you lost! booooo!\n";
            }
            if (computer == 3)
            {

                std::cout << "you won! woohoo!\n";
            }
        }

        // user paper

        else if (user == 2)
        {

            if (computer == 1)
            {

                std::cout << "you won! woohoo!\n";
            }
            if (computer == 3)
            {

                std::cout << "you lost! boo!\n";
            }
        }

        // user scissors

        else if (user == 3)
        {

            if (computer == 1)
            {

                std::cout << "you won! woohoo!\n";
            }
            if (computer == 2)
            {

                std::cout << "you lost! booooo!\n";
            }
        }
    }


    // Memory Game

    else if(game == 4) {
        std::cout << "Welcome to memory game!!\n";
        std::cout << "So host please enter the 1st word: ";
        std::string o1;
        std::string o2;
        std::string o3;
        std::string o4;
        std::string o5;
        std::string o6;
        std::string o7;
        std::string o8;
        std::string o9;
        std::string o10;
        std::cin >> o1;
        std::cout << "  2nd: \n";
        std::cin >> o2;
        std::cout << " 3rd: \n";
        std::cin >> o3;
        std::cout << "  4th: \n";
        std::cin >> o4;
        std::cout << " 5th:\n";
        std::cin >> o5;
        std::cout << " 6th: \n";
        std::cin >> o6;
        std::cout << " 7th:\n";
        std::cin >> o7;
        std::cout << " 8th: \n";
        std::cin >> o8;
        std::cout << "9th: \n";
        std::cin >> o9;
        std::cout << " 10: \n";
        std::cin >> o10;
        std::cout << "\n";

        std::cout << "---------------- \n";
        std::cout << "Words are:  \n";
        std::cout << " " << o1 << ", " << o2 << ", " << o3 << ", " << o4 << ", " << o5 << ", " << o6 << ", " << o7 << ", " << o8 << ", " << o9 << ", " << o10 << "  \n";
        
        std::cout << "Remember, you have 60seconds.....\n\n";
        std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
        sleep(2);

        std::cout << "Done!!\n";
        std::cout << "Now enter word 1:  ";
        std::string ans1;
        int points = 0;
        std::string ans2;
        std::string ans3;
        std::string ans4;
        std::string ans5;
        std::string ans6;
        std::string ans7;
        std::string ans8;
        std::string ans9;
        std::string ans10;
        std::cin >> ans1;
        if (ans1 == o1) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            --points;
        }
        

        std::cout << "2nd:   ";
        std::cin >> ans2;

        if (ans2 == o2) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            --points;
        }

        std::cout << "3rd:    ";
        std::cin >> ans3;

        if (ans3 == o3) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            --points;
        }
        

        std::cout << "4th:   ";
        std::cin >> ans4;

        if (ans4 == o4) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            --points;
        }

        std::cout << "5th:   ";
        std::cin >> ans5;

        if(ans5 == o5) {

            std::cout << "Correct\n";
            ++points;
        }

        else {
            std::cout << "Incorrect\n";
            --points;
        }

        std::cout << "6th:  ";
        std::cin >> ans6;


        if(ans6 == o6) {
            std::cout << "Correct\n";
            ++points;
        }

        else {
            std::cout << "Incorrect\n";
            --points;
        }

        std::cout << "7th:  ";
        std::cin >> ans7;

        if (ans7 == o7) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            --points;
        }

        std::cout << "8th:  ";
        std::cin >> ans8;

        if (ans8 == o8) {
            std::cout << "Correct\n";
            ++points;
        }

        else {
            std::cout << "Inccorect\n";
            --points;
        }

        std::cout << "9th:  ";
        std::cin >> ans9;

        if (ans9 == o9) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            --points;
        }

        std::cout << "Final one:  ";
        std::cin >> ans10;

        if (ans10 == o10) {
            std::cout << "Correct\n";
            ++points;
        }
        else {
            std::cout << "Incorrect\n";
            points = points - 1;
        }

        std::cout << " You finished, let's see if you won?\n";
        if (points < 0)
        {
            points = 0;
        }

        std::cout << "Your points are:   " << points << "\n";
        if (points > 5) {
            std::cout << "Congrats you passed the text!!!\n\n";
        }
        else {

            std::cout << "Oops try again!\n";
        }

    }

    
    
}



int main()
{
    bool Quit = false;
    char answer;


    std::string name;
    std::cout << "What is your name? ";
    getline(std::cin, name);
    std::cout << "Hello, " << name << "!\n";
        std::cout << "Welcome to Kanav's Game\n";
while(!Quit) {
  first_thing();

  std::cout << "Would you like to quit Y / N: ";
  std::cin >> answer;

  answer = std::toupper(answer);
  if (answer == 'Y')
  {
      Quit = true;
      std::cout << "BYE!!!";

      return 0;
  }
}
}

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review! Thanks for this great question - I hope you get some good reviews, and I hope to see more of your contributions here in future! \$\endgroup\$ Feb 11, 2021 at 7:56
  • \$\begingroup\$ I think you can get much response if you break your question into pieces. One game per question. I want to answer ufo game but I don't want to go all over others. \$\endgroup\$
    – Kao
    Feb 11, 2021 at 13:14
  • \$\begingroup\$ Your link only goes to cpp.sh, not to your program. \$\endgroup\$
    – Mark H
    Feb 12, 2021 at 22:58

2 Answers 2

3
\$\begingroup\$

This is a good first draft. Most of the program works and you've worked hard to get the logic right. As you continue to learn more, you'll be surprised at how fast the amount of code in this program shrinks while still doing the same thing.

1. Aim for small functions

As much as possible, you want each function to do one small thing so that someone reading the code can understand it quickly. Just reading the function in your question is difficult due to all the scrolling I have to do.

For each function, ask yourself, "What one thing should this function do?" Anything besides this one thing should be in another function. Since the first line in first_thing() is std::cout << "What game do you want to play?\n";, the function should only handle choosing a game to play. This function already has the right overall structure for this:

int game;
std::cin >> game;

if (game == 1)
{
    // ...
}
else if (game == 2)
{
    // ..
}

The problem is that you put the code for all the games right into this function. For each game in this function, cut the code, paste it into a new function, and call that new function from first_thing(). Then, the entire function becomes:

void first_thing()
{
    int game;
    std::cin >> game;

    if (game == 1)
    {
        play_hangman();
    }
    else if (game == 2)
    {
        play_tic_tac_toe();
    }
    else if (game == 3)
    {
        play_rock_paper_scissors();
    }
    else if (game == 4)
    {
        play_memory();
    }
}

Notice that the entire function now fits on one screen and the purpose of the function is clear. In fact, I would rename the function to choose_game(). Names are one of the most important things to consider when writing code. The name of a function or variable should tell you its purpose so that it's easy to understand the code. Plus, good names reduce the number of comments you have to write.

2. Keep variables contained inside functions

First, a test. What happens if you try to play two games of tic-tac-toe during a single program run? You can't play the second game because the board is already filled in from the first game. This is why variables should be placed inside functions. Once you have written a play_tic_tac_toe() function, you can start it like this:

void play_tic_tac_toe()
{
    std::string board[9] = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
    int player = 1;
    int position = 0;

    // The rest of the game.
}

Now, every time you start a new game of tic-tac-toe by calling this function, a new board is created for a new game.

But, now that the board how can the other functions like update_board() and is_winner() operate? You pass the board and all other information through the function parameters. For example:

void update_board(std::string (&board)[9], int player, int position)
{

    if (player % 2 == 1)
    {

        board[position - 1] = "X";
    }
    else
    {

        board[position - 1] = "0";
    }
}

The weird (&board) syntax is for passing board as a reference. This means that any changes made to board inside this function are visible outside the function as well. If instead you wrote std::string board[9], the update_board() function would get a copy of the board, so any changes would not be visible outside. The player and position variables are passed by copy, but we are not changing these variables, so it doesn't matter. The number 1 and a copy of number 1 are the same thing. Other C++ containers do not have this weird syntax. If your board was a std::vector<std::string>, then the function would be called this:

void update_board(std::vector<std::string>& board, int player, int position)

The ampersand (&) at the end of the type means pass-by-reference. No ampersand means a copy is made.

These changes also make it easier for people reading your code (including yourself six months from now) to follow the logic. Every variable is either declared in the function, or is a parameter of that function. As it is now, I have to go hunting through the entire file to figure out how board, position, and player are defined.

3. Lots of repetitive code means you need another function.

Computers are very good at doing repetitive things; humans, not so much. We get bored. If you find yourself typing the same thing over and over, consider whether you can write a function to do the work for you. Consider this bit of code from the Rock, Paper, Scissors section:

        if (user == 1)
            std::cout << "you choose: Rock\n";
        else if (user == 2)
        {

            std::cout << "you choose: Paper\n";
        }

        else
        {
            std::cout << "you choose: Scissors\n";
        }
        if (computer == 1)
        {
            std::cout << "cpu choose: Rock\n";
        }
        else if (computer == 2)
        {
            std::cout << "cpu choose: Paper\n";
        }
        else
        {
            std::cout << "cpu choose: Scissors\n";
        }

You are converting a numeric choice into text. Plus, the rest of the text for the user and cpu are the same, so let's write them just once.

std::string number_to_rock_paper_scissors(int choice)
{
    if(choice == 1) return "Rock";
    if(choice == 2) return "Paper";
    return "Scissors";
}

Then, inside the main game function:

void play_rock_paper_scissors()
{
    // ...
    std::cout << "you choose: " << number_to_rock_paper_scissors(user) << "\n";
    std::cout << "cpu choose: " << number_to_rock_paper_scissors(computer) << "\n";
    // ...
}

Every bit of logic is in one place and you are no longer repeating yourself. Try writing a function to determine who wins given two rock/paper/scissor values.

4. Lots of repetitive variables means you need a container and loops

In your Memory game, you have the following variables:

        std::string o1;
        std::string o2;
        std::string o3;
        std::string o4;
        std::string o5;
        std::string o6;
        std::string o7;
        std::string o8;
        std::string o9;
        std::string o10;

If you have a set of objects that are all the same type and need them in a certain order (you have numbered them), that means you need a vector--specifically, a std::vector<std::string>. Then, to fill up the vector, you just loop until you have enough words:

std::vector<std::string> words;
while(words.size() < 10)
{
    std::cout << "Enter word #" << words.size() + 1 << ": ";
    std::string input;
    std::cin >> input;
    words.push_back(input);
}

You can do the same thing with all of the ans1, ans2, etc. variables by creating a std::vector<std::string> answers.

std::vector<std::string> answers;
while(answers.size() < words.size())
{
    std::cout << "Enter guess for word #" << answers.size() + 1 << ": ";
    std::string input;
    std::cin >> input;
    answers.push_back(input);
}

Then, after all the input, use a loop to compare the answers with the words.

points = 0;
for(int i = 0; i < words.size(); ++i)
{
    std::cout << "Your answer for word #" << i + 1 << " is ";
    if(words[i] == answers[i])
    {
        std::cout << "correct!\n";
        ++points;
    }
    else
    {
        std::cout << "incorrect!\n";
        --points;
    }
}

5. I like the art for the UFO Hangman game

That's all.

Smaller details

You don't need #include <locale> or #include <unistd.h>. The second one is UNIX only.

Prefer std::vector to basic arrays. They are much easier to work with and behave like the rest of C++. It would be nice if the tic-tac-toe board were a std::vector (see Section 2).

sleep() is not a standard C++ function and only works in Windows. The way to pause the program is to use std::sleep_for() inside #include <thread>. Reference

The Quit variable inside main() is not used since you return from main() before the while() condition can see it changed. You can use while(true) to the same effect and get rid of Quit.

\$\endgroup\$
3
  • \$\begingroup\$ How can you do the same thing with ans1 and ans2? Sorry I am just a beginner. \$\endgroup\$
    – CrazyKanav
    Feb 15, 2021 at 4:36
  • \$\begingroup\$ @ILoveHistory I've updated my answer. Let me know if this helps. \$\endgroup\$
    – Mark H
    Feb 15, 2021 at 6:45
  • \$\begingroup\$ Thank you @Mark H \$\endgroup\$
    – CrazyKanav
    Feb 16, 2021 at 4:01
2
\$\begingroup\$

Few suggestions:

Raw string literal

No need for multiple

std::cout << ... << std::endl;

Raw string literal may be used as shown below:

const auto sImage = R"(
        .                           
                |                            
             .-\"^\"-.                       
            /_....._\\                       
        .-\"`         `\"-.                  
       (  ooo  ooo  ooo  )                   
        '-.,_________,.-'    ,-----------.   
             /--|--\\        (  Send help! ) 
            /   |   \\      / `-----------'  
           /   / \\   \\    /                
          /           \\                     
         /             \\                    
        /               \\     
)";        
std::cout << sImage << std::endl;

Passing objects by const ref

It is common practice to pass heavy stuff like strings via const reference to avoid copying

void end_game(const std::string& answer, const std::string& codeword)

Design

This one is quite abstract, vague, and personal so I won't say anything about the given design but just suggest asking few questions:

  1. How easy will be to add few more games to this code?
  2. How easy would it be to modify existing games (maybe making tic tac toe on 4x4 grid or with 3 players)?
  3. How easy would it be to add different UI (maybe add support for multiple languages)?

A better design usually makes at least some changes easier

\$\endgroup\$
1
  • \$\begingroup\$ Thank you, guys!!! \$\endgroup\$
    – CrazyKanav
    Feb 15, 2021 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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