Helping with maths
So here is the story in short: I have a young family member who struggles with maths, so I wanted to try and make this as fun as possible. Below are the C++ project files. At the moment, it’s simple maths; however, I will be adding settings that will allow you, the parent, to set the difficulty. I will also be implementing an automatic difficulty setting that is linked to the score. The plan is for the difficulty to increase once the score reaches 20.
Now, I am aware that no child wants to answer questions in a terminal, so I will be creating a UI for it over the coming weeks. Anyway, as promised, here is the code:
/*
* File: allGameModes.h
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*
* Created on 23 May 2023, 14:53
*/
#ifndef ALLGAMEMODES_H
#define ALLGAMEMODES_H
#include <iostream>
#include <cstdlib>
#include <ctime>
int main();
int generateRandomNumber();
void multiplication();
void selection();
void playAgain();
void clearConsole();
void subtraction();
void addition();
float generateRandomNumberDivision();
#endif /* ALLGAMEMODES_H */
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
int main() {
selection();
return 0;
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void addition() {
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
int number1 = generateRandomNumber();
int number2 = generateRandomNumber();
int answer = number1 + number2;
std::cout << "What is " << number1 << " + " << number2 << " = " << std::endl;
int guess;
std::cin >> guess;
if (guess == answer && chances >= 0) {
std::cout << "\033[92mWell done! " << number1 << " + " << number2 << " does indeed equal " << answer << "\033[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "\033[91mThat's not right. " << number1 << " + " << number2 << " = " << answer << ", not " << guess << "\033[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "\033[92mFinal score: " << score << "\033[0m" << std::endl;
playAgain();
}
}
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void division() {
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
float number1 = generateRandomNumberDivision();
float number2 = generateRandomNumberDivision();
float answer = number1 / number2;
if (answer == 0.0f) {
number1 = generateRandomNumberDivision();
number2 = generateRandomNumberDivision();
answer = number1 / number2;
}
std::cout << "What is " << number1 << " / " << number2 << " = ";
float guess;
std::cin >> guess;
if (std::abs(guess - answer) < 0.0001f && chances >= 0) {
std::cout << "\033[92mWell done! " << number1 << " / " << number2 << " does indeed equal " << answer << "\033[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "\033[91mThat's not right. " << number1 << " / " << number2 << " = " << answer << ", not " << guess << "\033[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "\033[92mFinal score: " << score << "\033[0m" << std::endl;
playAgain();
}
}
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void multiplication() {
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
int number1 = generateRandomNumber();
int number2 = generateRandomNumber();
int answer = number1 * number2;
std::cout << "What is " << number1 << " * " << number2 << " = " << std::endl;
int guess;
std::cin >> guess;
if (guess == answer && chances >= 0) {
std::cout << "\033[92mWell done! " << number1 << " * " << number2 << " does indeed equal " << answer << "\033[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "\033[91mThat's not right. " << number1 << " * " << number2 << " = " << answer << ", not " << guess << "\033[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "\033[92mFinal score: " << score << "\033[0m" << std::endl;
playAgain();
}
}
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void subtraction() {
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
int number1 = generateRandomNumber();
int number2 = generateRandomNumber();
int answer = number1 - number2;
std::cout << "What is " << number1 << " - " << number2 << " = " << std::endl;
int guess;
std::cin >> guess;
if (guess == answer && chances >= 0) {
std::cout << "\033[92mWell done! " << number1 << " - " << number2 << " does indeed equal " << answer << "\033[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "\033[91mThat's not right. " << number1 << " - " << number2 << " = " << answer << ", not " << guess << "\033[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "\033[92mFinal score: " << score << "\033[0m" << std::endl;
playAgain();
}
}
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void playAgain() {
std::cout << "Type Y to play again or N to exit: ";
std::string YN;
std::cin >> YN;
if (YN == "y" || YN == "Y") {
clearConsole();
selection();
} else {
clearConsole();
std::cout << "GoodBye for now!";
exit(0);
}
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void selection() {
int choice;
puts("Please select an option \nOption 0: Exit \nOption 1: Addition \nOption 2: Subtraction \nOption 3: Division \nOption 4: Multiplication \n");
std::cin >> choice;
switch (choice) {
case 0:
clearConsole();
std::cout << "GoodBye for now!";
exit(0);
case 1:
clearConsole();
std::cout << "You selected Option 1.\n";
addition();
break;
case 2:
clearConsole();
std::cout << "You selected Option 2.\n";
subtraction();
break;
case 3:
clearConsole();
std::cout << "You selected Option 3.\n";
division();
break;
case 4:
clearConsole();
std::cout << "You selected Option 4.\n";
multiplication();
break;
default:
std::cout << "Oops that's not a valid selection.\n";
selection();
}
}
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void clearConsole() {
std::cout << "\x1B[2J\x1B[H";
}
int generateRandomNumber() {
return rand() % 13;
}
float generateRandomNumberDivision() {
return static_cast<float>(rand() % 10 + 1);
}