Skip to content

Instantly share code, notes, and snippets.

@nathanielanozie
Created May 5, 2024 03:09
Show Gist options
  • Save nathanielanozie/e3d16967a11590c85137cb03b5b3a5fc to your computer and use it in GitHub Desktop.
Save nathanielanozie/e3d16967a11590c85137cb03b5b3a5fc to your computer and use it in GitHub Desktop.
c++ multiplication text game
//to compile run in terminal:
//g++ -o prog gameproblem.cpp
//then type this to run it: ./prog
#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <ctime>
//get a random integer in given range
int getRandomNumInRange(int a, int b)
{
std::random_device randDev;
std::mt19937 gen(randDev());
std::uniform_int_distribution<int> unif(a,b);
return unif(gen);
}
class MultiplicationGameProblem
{
/*responsible for integer multiplication text game
example usage:
MultiplicationGameProblem problem(1, 10); //minimimum number to multiply, maximum number to multiply
problem.doIt();
problem.printResult();
*/
public:
MultiplicationGameProblem(int, int); //excepts range for numbers in problem
void doIt(); //called by client to handle this game problem
bool printResult();
bool isCorrect(); //is client answer correct
private:
void printProblem();
void printSolutions();
void getUserAnswer();
int getAnswer();
private:
int mFirstNum;
int mSecondNum;
int mMin; //min number allowed
int mMax; //max number allowed
int mAnswerUser; //users answer value to problem
std::vector<int> mSolutions; //holds solutions
std::vector<char> mChoices; //choices for answers
};
//constructor
MultiplicationGameProblem::MultiplicationGameProblem(int min, int max)
{
mMin = min;
mMax = max;
//populate choices
mChoices.push_back('a');
mChoices.push_back('b');
mChoices.push_back('c');
mAnswerUser = 0; //garbage value it should be an actual answer
}
void MultiplicationGameProblem::printProblem()
{
std::cout << "yaaay problem:\n" << std::endl;
mFirstNum = getRandomNumInRange(mMin, mMax);
mSecondNum = getRandomNumInRange(mMin, mMax);
std::cout << mFirstNum << " " << "X" << " " << mSecondNum << "\n\n";
}
void MultiplicationGameProblem::printSolutions()
{
int answer = getAnswer();
mSolutions.push_back(answer);
int randomAnswerA = answer + getRandomNumInRange(1, 10);
int randomAnswerB = answer - getRandomNumInRange(1, 10);
mSolutions.push_back(randomAnswerA);
mSolutions.push_back(randomAnswerB);
//shuffle the answers
std::random_device rd;
std::mt19937 genB(rd());
std::shuffle(mSolutions.begin(), mSolutions.end(), genB);
//print answers
for(unsigned int i=0; i < mSolutions.size(); ++i)
{
std::cout << mChoices[i] << " " << mSolutions[i] << "\n";
}
}
void MultiplicationGameProblem::getUserAnswer()
{
//get user choice
char choice;
std::cout << "enter choice a, b, or c" << "\n";
std::cin >> choice;
int answerUser;
for(unsigned int j=0; j < mChoices.size(); ++j)
{
if(mChoices[j] == choice)
{
mAnswerUser = mSolutions[j];
}
}
}
bool MultiplicationGameProblem::printResult()
{
std::cout << "your answer " << mAnswerUser << "\n";
int answer = getAnswer();
if(mAnswerUser != answer)
{
std::cout << "please try again on a new problem\n";
std::cout << "the correct answer was " << answer << "\n";
return false;
}
std::cout << "great job! you're doing great :) \n";
return true;
}
bool MultiplicationGameProblem::isCorrect()
{
return mAnswerUser == getAnswer();
}
int MultiplicationGameProblem::getAnswer()
{
return mFirstNum * mSecondNum; //specific for multiplication
}
void MultiplicationGameProblem::doIt()
{
printProblem();
printSolutions();
getUserAnswer();
printResult();
}
class MultiplicationGame
{
/* example usage:
MultiplicationGame game(5, 1, 10);//number problems, minimimum number to multiply, maximum number to multiply
game.play();
game.printGameResult();
*/
public:
MultiplicationGame(int numProblems=10, int minNum=1, int maxNum=10)
{
for(unsigned int i=0; i<numProblems; i++)
{
MultiplicationGameProblem item(1, 10); //todo use inputs
mGameProblems.push_back(item);
}
mNumProblemsCorrect = 0;
mGameTimeSeconds = 0;
}
~MultiplicationGame(){}
/*play the game. presents a series of problems to play*/
void play()
{
std::time_t startTime = std::time(NULL);
std::vector<MultiplicationGameProblem>::iterator it;
for( it=mGameProblems.begin(); it<mGameProblems.end(); ++it)
{
(*it).doIt();
}
//keep track of total time for game
mGameTimeSeconds = (double)(std::time(NULL) - startTime);
}
void printGameResult()
{
std::cout << "Great playing!!!" << "\n\n";
std::cout << "number correct: " << getNumberCorrect() << "\n";
std::cout << "number problems: " << getNumberProblems() << "\n";
std::cout << "percent score: " << getPercentCorrect() << "\n";
//todo print time it took to answer questions
std::cout << "game time in seconds: " << mGameTimeSeconds << "\n";
}
private:
int getNumberCorrect()
{
std::vector<MultiplicationGameProblem>::iterator it;
for( it=mGameProblems.begin(); it<mGameProblems.end(); ++it)
{
if((*it).isCorrect()){ mNumProblemsCorrect += 1; }
}
return mNumProblemsCorrect;
}
int getNumberProblems()
{
return mGameProblems.size();
}
float getPercentCorrect()
{
return float(mNumProblemsCorrect)*100/float(mGameProblems.size());
}
private:
std::vector<MultiplicationGameProblem> mGameProblems; //holds all the problems for this game
int mNumProblemsCorrect;
double mGameTimeSeconds;
};
int main()
{
int numProblems = 1;
std::cout << "enter number of questions to play" << "\n";
std::cin >> numProblems;
MultiplicationGame game(numProblems, 1, 10);//number problems, minimimum number to multiply, maximum number to multiply
game.play();
game.printGameResult();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment