Being Faster

Richards Tutoring Blog
CodeX
Published in
4 min readMay 23, 2022
Photo by Chris Liverani on Unsplash

“Time flies. It is up to you to be the navigator”. Many times, we fail to make the right decisions because we don’t take the opportunities presented to us fast enough. When starting to learn to program we don’t worry much about the timing of our creation. Unfortunately, when we transcend to a professional environment, we are confronted not only with multiple constraints but also various deadlines, a difficult requirement. In a true high-end project, a 10-second delay could spell costly damages, which is why most programs’ grace period delays are down to milliseconds.

Now in our daily life, even when we play video games, being able to do the things we want comes down to a game of timing: if you don’t push yourself while dealing with a time-sensitive project you might end up incurring overtime. If you are a gamer, you might have found yourself playing a shooter game and lost a match because of bad timing as you decided to reload just as the enemy came around the corner.

If we want to do well-thought-out and fast programs, we should also train ourselves to be able to work and react fast to whatever comes our way. The simple game I am going to showcase here will help you practice your fast timing. Sub-sections of this program could be used for many other things, so don’t be afraid to “Frankenstein” your program to create a masterpiece.

We first shall start with the header file: this file serves to set up the class that we are going to be using.

Class File

Then we move on to the cpp file which is effectively the bells and whistles of the whole program. We start, of course, with the constructor method which initializes our Boolean variable to true by default.

The next method in our arsenal is of course createCounter, which is where we will put the Boolean variable to good use. The function of this method is to create a countdown counter to signal when the user must press enter. Not only that but to put some flair, the countdown is displayed as if it is being typed. Timing is everything, and how long we wait before we run certain lines of code makes things that much more entertaining. This method contains 2 different functions used to slow things down for the timing: sleep_for and pauseThread.

Now one could have used either one for both lines, but I decided to do things this way to show you the possibilities, with the pauseThread method being one that we create here, and which serves adequately to slow things down (You can ask on the comments if you want a further explanation). The count statement is combined into one single string and then each character is printed one by one with a certain delay in between in order to give the illusion of slow typing. You could of course personalize this statement, but you will have to manage the timing accordingly if the string gets too long. The counter is used in conjunction with the count variable in order not only to print the timer but also to manage the duration of said timer. At the end of this method, it is the right time to call forth the createPrompt method.

This method displays the message which informs the user it is the right moment to press the enter key as fast as possible. We then timed how much time transpired after the message was displayed until the user pressed the key.

We shall then move on to the main cpp file. Here is where we create the class object we will use to make the game run. In this file we will be running the class method in a very particular way: we will use the async function to launch the method as a Thread. Now doing things this way is not the only way of building this game, but it is the way I chose to get the timing just right, making all the threads wait their turn, running a total of 5 threads, and prompting the user to press the enter key 5 times. At the very end of the loop, we will average all the timings we stored.

Final Thoughts

Now, this program is simplistic in nature in what it does, but it is that simplicity that opens the opportunities for improvements. With some extra changes, you could have the program prompt the user for a specific letter key to be pressed, or even for a specific word to be typed. You could even create a class method that calculates the average typing speed. Although impressive, this program has some bugs, like if your press the enter key multiple times before the prompt appears, unintended behavior occurs.

Conclusion

Making sure everything follows the right order is an integral part of programming: from a simple loop to a full-on banking app. Now mistakes will be made along the way, there is no denying that. But take those mistakes as learning opportunities and grow.

It was a pleasure to build this program for all of you. Let’s keep on learning and building great things together. Comment down below any inquiries and don’t forget to follow.

--

--

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Richards Tutoring Blog
Richards Tutoring Blog

Written by Richards Tutoring Blog

Passionate and intrepid guy with a Bachelor’s degree in computer science and another one on cyber security. I am a freelance programming tutor ready to connect