I'm in my first-semester coding class and it's using c++. I recently finished this project to create a typing tutor and received full credit, but I want to find ways and opinions to optimize it as my goal is to improve. And full credit with no professor comments won't help me.
The goal is to have a randomly generated five-length string that can include a wild character [0-9] 10% of the time and another wild [%-!] any non alphanum symbol the other 10% of the time. The remaining 80% is upper and lowercase letters. (using ascii)...
You gain 500 points by typing correctly in under 10 seconds and lose points based off of passing the 10-second mark or typing a wrong character. The penalty for the wrong character is the abs(the difference between requested ascii and input ascii)...
I'm not looking for anyone to rewrite it, just some areas or redundancies and tips on how I can shorten the code.
#include <iostream>
#include <chrono>
#include <ctime>
#include <cmath>
#include <string>
using namespace std;
string generate(const int length);
string interpret(string generated);
int scoreOffset(string inputString,string randoString);
void withinInterval(int offset, int time);
void intervalFail(int offset, int time);
int main()
{
int totalScore = 2000;
int offset,totalTime;
srand(time(0));
string randoString;
string inputString;
string interpreted;
while(totalScore > 0 && totalScore < 5000)
{
randoString = generate(5);
interpreted = interpret(randoString);
cout << "Your current points " << totalScore << ", just type -> " << randoString << ": ";
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
cin >> inputString;
chrono::steady_clock::time_point end= chrono::steady_clock::now();
totalTime = chrono::duration_cast<chrono::milliseconds>(end - begin).count();
if (totalTime <= 10000)
{
offset = scoreOffset(interpreted,inputString);
if(offset == 0)
{
totalScore += 500;
withinInterval(offset,totalTime);
}
else
{
totalScore -= offset;
withinInterval(offset,totalTime);
}
}
else
{
offset = scoreOffset(interpreted,inputString);
totalScore -= (totalTime-10000);
if(offset == 0)
{
totalScore += 500;
intervalFail(offset,totalTime);
}
else
{
totalScore -= offset;
intervalFail(offset,totalTime);
}
}
}
if(totalScore < 0)
cout << "You lose..." << endl;
else
cout << "You win..." << endl;
}
string generate(const int length)
{
string str = "";
for(int i=0; i<length; i++)
{
int randomElement = (rand() % 100 + 1);
if (randomElement <= 40)
str += (rand() % 26 + 65);
else if (randomElement <= 80)
str += (rand() % 26 + 97);
else if (randomElement <= 90)
{
str += "[%-!]";
}
else
{
str+= "[0-9]";
}
}
return str;
}
string interpret(string generated)
{
int length = generated.length();
string str;
for(int i=0; i<length; i++)
{
if(generated[i] == '[' && generated[i+1] == '0')
{
str += '0';
i+=4;
}
else if(generated[i] == '[' && generated[i+1] == '%')
{
str += '*';
i+=4;
}
else
str += generated[i];
}
return str;
}
int scoreOffset(string randoString,string inputString)
{
int offset = 0;
int length = randoString.length();
for(int i = 0; i<length;i++)
{
if(isupper(randoString[i]))
{
if(isupper(inputString[i]))
offset += 0;
else
offset += abs((char(inputString[i])) - (char(randoString[i])));
}
else if(islower(randoString[i]))
{
if(islower(inputString[i]))
offset += 0;
else
offset += abs((char(inputString[i])) - (char(randoString[i])));
}
else if(isdigit(randoString[i]))
{
if(isdigit(inputString[i]))
offset += 0;
else
offset += abs((char(inputString[i])) - (char('0')));
}
else if(isalnum(randoString[i]) == false)
{
if(isalnum(inputString[i]) == false)
offset += 0;
else
offset += abs((char(inputString[i])) - int(char('*')));
}
}
return offset;
}
void withinInterval(int offset,int time)
{
cout << time << " milliseconds, you made it within the interval of 10,000..." << endl;
if(offset != 0)
cout << "String offset is " << offset << ", your total penalty is " << offset << endl;
}
void intervalFail(int offset, int time)
{
cout << time << " milliseconds, you *failed* it within the interval of 10,000..." << endl;
if(offset != 0)
cout << "String offset is " << offset << ", your total penalty is " << offset << endl;
}
+= 0
?) \$\endgroup\$