Created
May 25, 2024 03:35
-
-
Save nathanielanozie/711e2526af2c2a2e7f88d51873855eb8 to your computer and use it in GitHub Desktop.
c++ arithmetic text game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//to compile run in terminal: | |
//g++ -o prog gamearithmetic.cpp | |
//then type this to run it: ./prog | |
#include <iostream> | |
#include <random> | |
#include <algorithm> | |
#include <vector> | |
#include <cstdio> | |
#include <ctime> | |
enum ArithmeticType | |
{ | |
kMultiplication, | |
kDivision, | |
kAddition, | |
kSubtraction | |
}; | |
//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 ArithmeticGameProblem | |
{ | |
/*responsible for integer multiplication text game | |
example usage: | |
ArithmeticGameProblem problem(1, 10); //minimimum number to multiply, maximum number to multiply | |
problem.doIt(); | |
problem.printResult(); | |
*/ | |
public: | |
ArithmeticGameProblem(int, int, ArithmeticType); //excepts range for numbers in problem | |
void doIt(); //called by client to handle this game problem | |
bool printResult() const; | |
bool isCorrect() const; //is client answer correct | |
private: | |
void printProblem(); | |
void printSolutions(); | |
void getUserAnswer(); | |
double getAnswer() const; | |
private: | |
double mFirstNum; | |
double mSecondNum; | |
ArithmeticType mArithmeticType; | |
double mMin; //min number allowed | |
double mMax; //max number allowed | |
double mAnswerUser; //users answer value to problem | |
std::vector<double> mSolutions; //holds solutions | |
std::vector<char> mChoices; //choices for answers | |
}; | |
//constructor | |
ArithmeticGameProblem::ArithmeticGameProblem(int min, int max, ArithmeticType gameType) | |
{ | |
mMin = min; | |
mMax = max; | |
mArithmeticType = gameType; | |
//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 ArithmeticGameProblem::printProblem() | |
{ | |
std::cout << "yaaay problem:\n" << std::endl; | |
mFirstNum = getRandomNumInRange(mMin, mMax); | |
mSecondNum = getRandomNumInRange(mMin, mMax); | |
//print proper symbol depending on game type | |
switch(mArithmeticType) | |
{ | |
case kMultiplication: | |
std::cout << mFirstNum << " " << "X" << " " << mSecondNum << "\n\n"; | |
break; | |
case kDivision: | |
std::cout << mFirstNum << " " << "/" << " " << mSecondNum << "\n\n"; | |
break; | |
case kAddition: | |
std::cout << mFirstNum << " " << "+" << " " << mSecondNum << "\n\n"; | |
break; | |
case kSubtraction: | |
std::cout << mFirstNum << " " << "-" << " " << mSecondNum << "\n\n"; | |
break; | |
default: | |
std::cout << mFirstNum << " " << "X" << " " << mSecondNum << "\n\n"; | |
break; | |
} | |
} | |
void ArithmeticGameProblem::printSolutions() | |
{ | |
double answer = getAnswer(); | |
mSolutions.push_back(answer); | |
double randomAnswerA = answer + getRandomNumInRange(1, 10); | |
double 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 ArithmeticGameProblem::getUserAnswer() | |
{ | |
//get user choice | |
char choice; | |
std::cout << "enter choice a, b, or c" << "\n"; | |
std::cin >> choice; | |
double answerUser; | |
for(unsigned int j=0; j < mChoices.size(); ++j) | |
{ | |
if(mChoices[j] == choice) | |
{ | |
mAnswerUser = mSolutions[j]; | |
} | |
} | |
} | |
bool ArithmeticGameProblem::printResult() const | |
{ | |
std::cout << "your answer " << mAnswerUser << "\n"; | |
double 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 ArithmeticGameProblem::isCorrect() const | |
{ | |
return mAnswerUser == getAnswer(); | |
} | |
double ArithmeticGameProblem::getAnswer() const | |
{ | |
double result; | |
switch(mArithmeticType) | |
{ | |
case kMultiplication: | |
result = mFirstNum * mSecondNum; | |
break; | |
case kDivision: | |
result = mFirstNum / mSecondNum; | |
break; | |
case kAddition: | |
result = mFirstNum + mSecondNum; | |
break; | |
case kSubtraction: | |
result = mFirstNum - mSecondNum; | |
break; | |
default: | |
result = mFirstNum * mSecondNum; | |
break; | |
} | |
return result; //specific for multiplication | |
} | |
void ArithmeticGameProblem::doIt() | |
{ | |
printProblem(); | |
printSolutions(); | |
getUserAnswer(); | |
printResult(); | |
} | |
class ArithmeticGame | |
{ | |
/* example usage: | |
ArithmeticGame game(5, 1, 10);//number problems, minimimum number to multiply, maximum number to multiply | |
game.play(); | |
game.printGameResult(); | |
*/ | |
public: | |
ArithmeticGame(int numProblems=10, int minNum=1, int maxNum=10, ArithmeticType gameType=kMultiplication) | |
{ | |
for(unsigned int i=0; i<numProblems; i++) | |
{ | |
ArithmeticGameProblem item(1, 10, gameType); //todo use inputs | |
mGameProblems.push_back(item); | |
} | |
mNumProblemsCorrect = 0; | |
mGameTimeSeconds = 0; | |
} | |
~ArithmeticGame(){} | |
/*play the game. presents a series of problems to play*/ | |
void play() | |
{ | |
std::time_t startTime = std::time(NULL); | |
std::vector<ArithmeticGameProblem>::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<ArithmeticGameProblem>::iterator it; | |
for( it=mGameProblems.begin(); it<mGameProblems.end(); ++it) | |
{ | |
if((*it).isCorrect()){ mNumProblemsCorrect += 1; } | |
} | |
return mNumProblemsCorrect; | |
} | |
int getNumberProblems() const | |
{ | |
return mGameProblems.size(); | |
} | |
float getPercentCorrect() const | |
{ | |
return float(mNumProblemsCorrect)*100/float(mGameProblems.size()); | |
} | |
private: | |
std::vector<ArithmeticGameProblem> mGameProblems; //holds all the problems for this game | |
int mNumProblemsCorrect; | |
double mGameTimeSeconds; | |
}; | |
ArithmeticType getGameType() | |
{ | |
//print possible game types | |
std::cout << "pick game type" << "\n"; | |
std::cout << "(a) Multiplication" << "\n"; | |
std::cout << "(b) Division" << "\n"; | |
std::cout << "(c) Addition" << "\n"; | |
std::cout << "(d) Subtraction" << "\n"; | |
//get user choice | |
char choice; | |
std::cout << "enter choice a, b, c or d" << "\n"; | |
std::cin >> choice; | |
ArithmeticType result; | |
switch(choice) | |
{ | |
case 'a': | |
result = kMultiplication; | |
break; | |
case 'b': | |
result = kDivision; | |
break; | |
case 'c': | |
result = kAddition; | |
break; | |
case 'd': | |
result = kSubtraction; | |
break; | |
default: | |
result = kMultiplication; | |
break; | |
} | |
return result; | |
} | |
int main() | |
{ | |
int numProblems = 1; | |
std::cout << "enter number of questions to play" << "\n"; | |
std::cin >> numProblems; | |
ArithmeticType gameType;// = kMultiplication; | |
gameType = getGameType(); | |
ArithmeticGame game(numProblems, 1, 10, gameType);//number problems, minimimum number to multiply, maximum number to multiply, game type | |
game.play(); | |
game.printGameResult(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment