How to Create a 20 Questions Game in C++
Part 1 of 3:
Creating a Project
- Obtain a copy of Visual Studio and open it.
- You can find a detailed guide to setup at How to Install and Setup Visual Studio Express 2013.
- Create a project by clicking the file tab on the top left and clicking New Project.
- Click the Templates tab on the left.
- Click Visual C++ under the templates tab.
- Click Empty Project in the middle.
- Name your project something relevant.
- For example: 20 Questions Game.
- Add a source file. Right-click Source Files on the right side of the screen under the solution explorer box. Then hover add and click New File.
- Source Files -> Add -> New File
- Select C++ file(.cpp), name it something relevant and click Add.
- For example: "Main.cpp" because this will be our main source file. This naming scheme is especially important in larger scale programs that require more than one file.
Part 2 of 3:
Setting Up the Program
- Begin the programming setup.
- Type:
#include
- This includes a file in the C++ library that allows console manipulation.
- Type:
using namespace std;
- This means you're using a standard (std) namespace.
- Type:
int main(){ }
- This is the main function that the program will run. Everything runs through this.
- In between the curly braces { }, press enter a few times. Everything goes in between the curly braces. Note: The green colored words are comments. These are for you (the interpreter) to make better sense of the code.
- Type:
- Declare the variables.
- Within the main function brackets,(int main()), create the following variables:
int max = 100;
int min = 0;
char ans;
int num = 0;
int guess;
int numGuess = 0;
- Note that some variables are declared with values, while others are not. This is because those variables are required by the program to be predefined. This is determined by how they're used.
- Within the main function brackets,(int main()), create the following variables:
- Create the message to the user.
- Type:
cout << 'Think of a number between 1 and 100.' << endl;
- This prompts the user for their number, giving them an idea of what is required of them.
- Type:
Part 3 of 3:
Programming the Logic
- Create the do-while loop. This will control all of the game logic.
- Type:
do{ }while(num == 0 && numGuess < 20);
- Press ↵ Enter a few times between the braces. Note: num == 0 && numGuess < 20 basically means the loop will continue until num equals 0 AND numGuess is less than 20.
- Type:
- Type everything in the picture within the do-while brackets.
- Understand the logic of the loop:
- The user will enter Y or N, based on their number.
- If their number is greater than or equal to guess, numGuess increments by 1 and the program makes a guess.
- If the guess is correct, the program breaks out of the loop and the computer wins.
- If the guess is incorrect, min = guess; effectively cutting the range of values in half and starting the loop over again.
- If their number is not greater than or equal to guess, numGuess increments by 1 and max = guess; cutting the range of values in half and starting the loop over again.
- The program will go through these guesses and checks until it narrows the users number down to a single number or it reaches its 20 question limit.
- Save and run the program. At this point, it should do everything it's intended to do, excluding the concluding message. If the program suddenly closes when it guesses your number or when it fails to guess your number, that's normal. We will fix this in the next few steps.
- Understand the logic of the loop:
- Type everything in the picture after the do-while loop. This will be the concluding lines of code that handle whether or not the player wins.
- Understand the logic of the concluding message:
- If the numGuess equals 20 and num equals 0, the computer could not guess you number.
- Note that the value of num will never change if the users number is never guessed.
- If the users number is guessed correctly, the computer will output your number and a little victory smiley face.
- Note the lines system("pause"); and return 0;
- system("pause") simply pauses the program, allowing the user to read the message.
- return 0; exists because it is good practice to return a value in the main function; even if the value is irrelevant.
- If the numGuess equals 20 and num equals 0, the computer could not guess you number.
- Understand the logic of the concluding message:
- Review your completed program.
4.5 ★ | 6 Vote
You should read it
- How to Install Software on a Mac
- How to Hide a File or Folder from Search Results in Microsoft Windows
- How to Edit a Dat File
- How to Delete Read Only Files
- How to Convert a File Into PDF
- Open the file, open the folder with a mouse click
- MS PowerPoint - Lesson 1: Introduction to MS PowerPoint 2003
- How to Enlarge Thumbnails and Icons in File Explorer
May be interested
- Instructions for designing a maze game in ScratchJRin addition to multiple choice content, you can design a maze game in scratchjr. in this first lesson, students will know how to create a simple moving space for the cat character in the maze game.
- How to Create Power-Ups and Collections in Pygamemake pygame projects more fun by integrating power-ups and collections. here are detailed instructions.
- How to create a jumping mechanism in Godotjump mechanics are a fundamental aspect of many platformer games, allowing players to navigate obstacles, reach higher platforms, and add an extra layer of interactivity to the gameplay.
- How to create a Start menu and a Game Over screen with PyGamepygame is a popular library for creating projects in python and it provides a powerful set of tools for game development.
- 49 most asked questions that Apple asked in the job interviewapple often asks about expertise (based on work experience) and intellectual challenges. for example, applying for a position at apple's retail store. you will be asked how to handle when customers are angry. the article below summarizes many of the most difficult questions that apple has asked candidates to post on glassdoor with the job placement.
- Google Maps helps developers create real-world gamespokémon go stormed in july 2016 when it debuted with game characters realized in the real world map. now google is making any game developer able to do the same with google maps through the unity game engine.
- Answer game 2 Image 1 word: Guess the word, Track the songwith this set of answers, you will easily pass the difficult screen guessing game of 2-picture 1-word game - searching for the song. however, you remember to refer to the answer only when really deadlocked, do not abuse it, so will create attraction as well as create more dramatic.
- 10 questions of brainstorming try your quick reactioninteresting multiple-choice questions test your acumen.
- Answer the game Cross the IQ River, update the latest answeranswer the latest 35 iq game cross river, help you play the game through iq river through the screen quickly. not a new iq game, but iq river is an iq game that attracts players because of its difficulty and twist. questions it may take up to 35 steps to solve the problem through the most difficult iq river.
- 18 'extremely difficult' recruitment questions from Applehere are 18 actual interview questions at apple's interview. please consult and observe carefully to see if you can give an answer!