My Version of TrippleX

Here is my version of tripplX i would enjoy criticism and feedback. Personally i think we should have made a class but i think that might be coming up. I also think that we are using to many resources in the variables I didn’t research how much space array and int take, but i think we can lower the footprint by using smaller versions of those. I am also not sure how portable this would be on a non-64bit computer. There are a few bugs that i am willing to let slide for now, such as if you put the code in as 1 4 9 and 4 is already in the right place, it will respond dubiously.

#include <iostream>
#include <array>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "stdio.h"

// define global scope variables, this would be better in its own class
std::array<std::string, 3> GuessStore = {"X", "X", "X"};
std::array<int, 3> Code;
int MAX_SINGLE_NUMBER = 10;
int GuessA, GuessB, GuessC;
int MaxTries = 5;
int CurrentTry = 0;

// generate Random Code
void GenerateCode()
{
    srand (time(NULL));
    for (int i = 0 ; i < 3; i++)
    {
        Code[i] = rand() % MAX_SINGLE_NUMBER + 1;

    }
}

// update GuessStore with correct guess's
void UpdateGuessStore(std::array<int, 3> GStore)
{
    for (int i = 0; i < GStore.size(); i++)
    {
        if (GStore[i] == Code[i])
        {
            GuessStore[i] = std::to_string(GStore[i]);
        }
    }
} 

// print intro
void PrintIntro()
{
    std::cout << "Enter the correct code to live. When you fail, I take your life.\n";
}

// print hints
void PrintHints(std::array<int, 2> CodeHints)
{
    std::cout << "There are 3 numbers in this code. ";
    std::cout << CodeHints[0] << " Is what the code adds up to.";
    std::cout << CodeHints[1] << " is the product of the code.\n";
}

// Check Guess
std::array<int, 2> CheckGuess(std::array<int, 3> Guess, std::array<int, 3> Code)
{
    int CorrectPlace = 0;
    int CorrectNumbers = 0;
    for ( int i = 0; i < Guess.size(); i++ )
    {
        for ( int j = 0; j < Code.size(); j++)
        {
            if ( Guess[i] == Code[j])
            {
                if ( i == j)
                {
                    CorrectPlace++;
                } else
                {
                    CorrectNumbers++;
                }
                
            }
        }

    }
    std::array<int, 2> GuessCount = { CorrectPlace, CorrectNumbers};
    return GuessCount;
}

// get guess returns array of size 3
std::array<int, 3> GetGuess()
{
    std::cout << "What is the code?\n:";
    std::cin >> GuessA >> GuessB >> GuessC;
    if ( std::cin.fail() )
    {
        std::cout << "Only integers are allowed you dirty human.\n";
        std::array<int, 3> Guess = { 1, 1, 1 };
        std::cin.clear();
        std::cin.ignore(10000000000, '\n');
        return Guess;
        
    }
    std::array<int, 3> Guess = {GuessA, GuessB, GuessC};
    return Guess;
}

// Setup Code and Generate Hints
std::array<int, 2> SetCode(std::array<int, 3> Code)
{
    int CodeSum = Code[0] + Code[1] + Code[2];
    int CodeProduct = Code[0] * Code[1] * Code[2];
    std::array<int, 2> CodeHints;
    CodeHints = {CodeSum, CodeProduct};
    return CodeHints;
}

// main game
bool PlayGame()
{
    std::array<int, 2> CodeHints = SetCode(Code);
    PrintHints(CodeHints);
    std::array<int, 3> Guess =  GetGuess();
    std::array<int, 2> GameWon = CheckGuess(Guess, Code);
    UpdateGuessStore(Guess);
    if ( GameWon[0] == 3 )
    {
        std::cout << "Unclear how you made it, but you may live.\n";
        return true;
    } else
    {
        std::cout << "You had: " << GameWon[0] << " numbers in the correct place. ";
        std::cout << "and " << GameWon[1] << " numbers correct, but in the wrong place.\n";
    }
    return false;
    
}

int main()
{
    std::cout.clear();
    GenerateCode();
    PrintIntro();
    do
    {
        for (int i = 0; i < GuessStore.size(); ++i)
        {
            std::cout << GuessStore[i] << " ";
        }
        std::cout << "\n You have " << MaxTries - CurrentTry << " guess's left.\n";
        bool GameWon = PlayGame();
        if (GameWon)
        {
            return 0;
        }
        CurrentTry++;
    }while ( CurrentTry != MaxTries);
    std::cout << "I have a right to your life, when and how I may choose.\n";
    std::cout << "\n If you are curious: ";
    for (int i = 0; i < Code.size() ; ++i)
    {
        std::cout << Code[i] << " ";
    }
    std::cout << "Would have saved your life.";
    return 0;
}    

I can agree on using less space with the integers and arrays. So to do that, I used the short int modifier to keep memory usage low. Also, your tripleX game was a bit on the edgy side, but hey I liked it, great job! :smile:

Privacy & Terms