Building the game Two Thirds of Average with C++
Today is the first day of my planned #100DaysOfCode and #100DaysOfWriting. I plan to undertake (at least) 100 days of deliberate coding (non-work related) and writing. On each of these days, I would write a piece of software and in the process learn something new. I would also write an article about something I did not know before.
Today’s Challenge
Today, due to lack of ideas on what project to work on, I decided to scout the internet in search of projects. I came across this website and decided to work on the challenge: Two Thirds of Average.
The challenge goes thus
Two Thirds of Average
A simple game of maths and psychology. In this game a number of people submit a number (full number, no fractions) between 0 and 100 (inclusive). Each persons number is a guess at what 2/3 of the average of all guesses will be. The person whos guess is the closest wins.
You can play an on-line version (agains the last 100 visitors) to get a feel for how it works.
Create a program which will allow a group of people to play the game.
Calculating 2/3 of the average isn’t too difficult. Obtaining the guesses whilst keeping everyones guesses secret is a little trickier however.
This is a fairly easy challenge given my experience in programming, so I decided to use it to learn a new language: C++. This was my first project in C++ and I was really excited to get down to it.
My Approach
My plan was simple:
- Create a C++ console application for the game.
- Get the input of 3 players one after the other.
- Make sure each player’s input is hidden (so that later players won't have an unfair advantage).
- Calculate 2/3 of the average of their inputs and determine who was the closest.
As was stated in the challenge, the most tasking part was figuring out how to keep everyone’s guess secret in C++ as the game would be played in the console. To do that, I had to follow instructions here: http://www.cplusplus.com/forum/beginner/43683/ and I came up with the following code:
The getNumber() method above disables echoing in the console, that is, whatever a user types in with the keyboard will not be displayed on the screen. It then accepts input from the user and returns it. The main() function asks the players to enter a number one by one and calls getNumber() to get the number they type in. It then displays the numbers entered by all the users. Below is an example of the code in action.
Wrapping Up
After I had solved the problem of getting the player’s guesses while keeping them hidden, the rest of the challenge was easy to do. You can view the full code here. I ran a couple of tests to make sure the program was working as expected:
Future Work
There are a number of things I did not implement either due to oversight or because I did not wish to. They include:
- Allowing more than 3 players to play the game.
- Ensuring that the values entered by users fall between the [0–100] range that was specified, and are whole numbers.
- Handling the case where more than one player enters the correct number.
- Allowing players to play again without having to reopen the program.
These and more could be the focus of future work on this project.
Conclusion
I really enjoyed my first encounter with C++ and my first day on both challenges. I will continue to work on projects with C++ to improve my experience with it. Thanks for following this far. If you have any suggestions or comments on this article or the project, please feel free to drop them below.