Triplex

Hello, I’ve completed the triplex. Not the nicest solution but I did not use the videos for it.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sstream>
#include <regex>
#include <vector>


//creating string from a number
template <typename T> 
std::string numberToString(T number)
{
    std::ostringstream  ss;
    ss << number;
    return ss.str();
}

int stringToNumber(std::string str)
{
    std::stringstream ss(str);
    int result = 0;
    ss >> result;
    return result;
}

//Generates a random number based on the difficulty
int randNumber(int difficulty)
{
    int result = rand() % difficulty + difficulty;

    return result;
}


void logic(int initDifficulty, int maxDifficulty)
{
    std::string itemName = "All Seeing Eye";
    std::cout<<"You are a heroic rouge who wants to defeat an evil wizard in his magic tower.\n";
    std::cout<<"Your party can defeat all the enemies on the floors, but they need you to deactivate the traps that protects the staircases between floors.\n";
    std::cout<<"Fortunately you've just bought the magic item: the " + itemName + " which can help you break the traps.\n";
    std::cout<<"All traps need 3 numbers to defeat followed by an 'x', like this: <number> <number> <number> x\n";
    std::cout<<"Your item gives you the opportunity to get the sum and the product of the 3 number\n";
    std::cout<<"Your task is to correctly identify theese number. If you fail so you and your party will die a horrible death and the wizard will rule the world for eons.\n";

    srand( time(NULL));
    int difficulty = initDifficulty;
    int a, b, c, sum, prod;
    std::string guess = "";
    
    for(int i = initDifficulty ; i<maxDifficulty; i++)
    {
        difficulty = i;
        std::cout << std::endl;
        std::cout<<"You are heading for the " + numberToString(i+1) + ". floor of the tower. You have to disable a new trap."<<std::endl;
        a = randNumber(difficulty);
        b = randNumber(difficulty);
        c = randNumber(difficulty);

        sum = a + b + c;
        prod = a * b * c;

        std::cout<<"The trap is near you, you have to give it the correct combination! "<<std::endl;
        std::cout<<"The " + itemName + " tells you that the sum is: " + numberToString(sum) + ", and the product is: " + numberToString(prod) << std::endl;
        

        bool correct = false;

        while(!correct)
        {
            std::getline(std::cin, guess);
            if(! std::regex_match(guess, std::regex("^\\d+ \\d+ \\d+ x$")) )
            {
                std::cout<<"Fortunately one of your mates alerts you that you are trying to access the magic trap incorrectly. '<number> <number> <number> x' is the correct access."<<std::endl;
                continue;
            }
            correct = true;
        }
        std::vector<std::string> splitted;
        std::istringstream iss(guess);
        for(std::string s; iss>>s; )
            splitted.push_back(s);
        
        if(stringToNumber(splitted[0]) + stringToNumber(splitted[1]) + stringToNumber(splitted[2]) != sum ||  stringToNumber(splitted[0]) * stringToNumber(splitted[1]) * stringToNumber(splitted[2]) != prod)
        {
            std::cout<<"You have failed your quest. The darkness will rule your world for eons. You are dead with all of the hopes of the people."<<std::endl;
            std::cout<<"\t\t\t\t\t GAME OVER" <<std::endl;
            return;
        }
    }
    std::cout<<"Congratulations! You have defeated the evil wizard! You and your teammates will be renowned across all of the known lands for this feat!" <<std::endl;
    std::cout<<"The game ends here as the heroes go home in the glory."<<std::endl;

}



int main()
{
    int initDifficulty = 1;
    int maxDifficulty = 10;

    logic(initDifficulty, maxDifficulty);

    std::cout<<"\t\t\t\tThank you for playing the game."<<std::endl;
    std::cout<<"\t\t\t\tGame was created by Jonah Veress." <<std::endl;

    return 0;
}
1 Like

Great job with your triplex! :fire: :fire:

Privacy & Terms