Beginners Guess-That-Number Game With C++
by SierraM05 in Circuits > Software
933 Views, 14 Favorites, 0 Comments
Beginners Guess-That-Number Game With C++
Used in schools and the workforce, C++ is a powerful object-oriented language that can accomplish big and small projects. This Instructable demonstrates how to implement the basics of C++ to create a 2-Player Guess-That-Number game on the terminal!
Supplies
- Code Editor/Compiler
- https://www.onlinegbd.com is a great source for a free online compiler and debugger! Otherwise, Microsoft's Visual Studio or Visual Studio Code works.
Login or Create an Account
Before you can create a new project, you will need to have an account. Creating an account is free and simple. Accounts can be created by linking a social account (Facebook or Github) or by creating an OnlineGDB account.
Create Project and Set Language
When you're first logged into your account, the editor automatically shows a new project. You can also select "Create New Project" in the left side-bar. When in your new project window, select the C++ language from the drop-down menu in the top right-hand corner.
Set Basic Syntax
For this program, we will use:
#include <iostream>
#include <unistd.h>
And will be including:
using namespace std;
After those are set up, it's time to set up your main function, which will be the only function used in this program.
Declare Variables
Your variables can have any name that makes sense to you.
Because we will be creating a number game, most variables will be integers.
int num = 0, userAns1 = 0, userAns2 = 0, userAns3 = 0;
int playerScore1 = 0, playerScore2 = 0, roundNum = 0;
You will need integers to:
- (1) store the number that will be guessed
- (3) store the player's three number guesses
- (2) store player 1 and 2 points
- (1) store the current round number
The next set of variables will be strings.
string playAgain = "Yes";
string p1name;
string p2name;
You will need strings to:
- (1) store the response to "play again" question that will be at the end of the program
- (2) store the players name
OPTIONAL: Add Game Instructions
At the beginning of the program, it would be nice to add instructions for the players. These instructions can also serve to be your guide as you continue with your program.
Your instructions should clearly demonstrate what will happen in your game. For example, tell the players how the point system works.
Get Player Names
It's time to first implement those two player name strings. To add a little structure and character to the game, ask your players what their names are and store them for future use.
Game Loop
At the end of the round, the players will be asked if they would like to begin another round. While the players say yes, the program will begin again here.
Set Round Number
This program will display player and game stats at the end, the number of rounds played will be included. Start the round counter now.
roundNum = roundNum + 1;
and
cout << "Round: " << roundNum
allows for the round number to be added to with each round played and displayed when a new round is began.
Get Number to Be Guessed
Using the collected names, ask the first player for the number that player two will guess.
cout << p1name << ", what is the number?" << endl << endl;
cout << "Number: ";
cin >> num;
Require Number 1-10
Sometimes, the player will attempt to enter a number that is less than 1 and greater than 10. To keep the game small and fair, implement an error catcher that will require the desired input.
OPTIONAL: Clear Screen
After Player-1 gives a number, it would be best to have the screen clear to prevent the second player from seeing the number.
usleep(20000000);
cout << "\033[2J\033[1;1H";
The "usleep" function allows for a time limit before the next command is acted upon. The time is set in microseconds.
Player 2 Guesses
After collecting the number from Player-1, it's time for Player-2 to guess the number. The player will get three attempts at answering correctly. If guessed on the first attempt, the player will get full points and the program moves on. If guessed wrong, the program gives the player two more tries before it moves on.
Player Two's Turn
It is now time to switch the player roles and allow player 1 to earn points and guess the number. Go through steps 9-11 and switch the player names and answers.
Give Option to Continue
Now that both players have had the opportunity to give a number and to guess, the round is over. The program could end here, but it's best to give the players the choice.
Ask the players to end or continue the program.
The input gathered for "playAgain" will be used when determining whether the while loop (step 7) is valid or not.
End Game
When the while loop is not valid (the user enters input other than "Yes" or "yes") the game should end. It's time to give the players the collected stats on rounds and points.