OK I’m going to show you how to make a little game where you and the computer take turns guessing at a random number between 1-100. I’ll do my best to explain everything. :)
OK the first thing we do is add this.
OK #include<iostream> includes the file to take user input and output it on the console window.Code:#include<iostream> #include<cstdlib> #include<ctime> using namespace std;
#include<cstdlib> this includes file that deal with generating random numbers.
#include<ctime> allows us to seed our random number or in other words allows us to make the number random.
using namespace std; this lets us call any function in the std namespace without having to prefix it so std::cout can now be writhen cout which will save you lots of time.
Next we add.
In side Int main()s braces is where all of our code is executed by the complier.(for this tutorial.)Code:int main() { srand(time(NULL)); char playagain = 'y'; while (playagain == 'y') { } }
srand(time(NULL)); This seeds our random number
char playagain = 'y'; This sets playagain to a char value which is a character value then set it == or equal to ‘y’
while (playagain == 'y')This is or main loop where the program is ran so while playagain is equal to ‘y’ execute whatever’s in these braces( this make sure the user selected y to play the game again.)
{
}
Ok now we define our main variables in our while loop.
int comguessHigh = 100; and int comguessLow = 1; Are the variables used to tell the computer not to guess higher or lower than the high or low guess.Code:int comguessHigh = 100; int comguessLow = 1; int thenumber = rand() % 100 + 1; int tries = 0, guess;
int thenumber = rand() % 100 + 1; this sets the variable thenumber to a random number between 1-100.
Int tries = 0,guess; This sets the variable tries and guess equal to 0.
Now comes the guessing the number part.
cout << "Enter guess: This asks the user to enter a numberCode:for(;;) { cout << "Enter guess: "; cin >> guess; tries += 1; if (guess < thenumber) { cout << "\nThat's to low.\n" << endl; } else if (guess > thenumber) { cout << "\nThat's to high.\n" << endl; } else { cout << "That's it! You guessed it in " << tries << " tries." << endl; break; }
cin >> guess; This takes the user input and sets guesses value equal to the number entered.
tries += 1; This sets tries plus 1 which will later be used to tell the user how many tries it took them to guess the number.
if (guess < thenumber) if guess is smaller than thenumber tell the user it to low then move on to the next operation.(which will do in a minute)
{
cout << "\nThat's to low.\n" << endl;
}
else if (guess > thenumber) if guess is larger than thenumber tell the user it to high then move on to the next operation.(which will do in a minute)
{
cout << "\nThat's to high.\n" << endl;
}
else [If the users guess is not to high or low tell the user how many tries it took to guess it in then exit the for(;;) loop with the break; statement.]
{
cout << "That's it! You guessed it in " << tries << " tries." << endl;
break;
}
OK so now the player can guess at the number but now we need to make the computer take a guess.
int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow; This changes comguess which is the computers guess so it can only be lower than its highest guess and higher than its lowest guess.Code:int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow; cout << "Computer guess's: " << comguess << endl; if (comguess < thenumber) { cout << "\nThe computers guess was to low.\n" << endl; comguessLow = comguess; } else if (comguess > thenumber) { cout << "\nThe computers guess was to high.\n" << endl; comguessHigh = comguess; } else { cout << "\nSorry the computer guessed it." << endl; break; }
The rest of that code checks to see if the computers guess to high or low just like we did a minute ago when the player guessed.
Now the last thing we have to do is when the player or computer guesses the number we need to ask if we want to play again.
}Code:} cout << "\nDo you want to play again? (y/n)" << endl; cin >> playagain; } cout << "\nOk mabye next time." << endl; cin.get(); return 0; }
cout << "\nDo you want to play again? (y/n)" << endl;
cin >> playagain;
}
Asks the user if they want to play again if a letter is entered that is not y the while loop is not executed because again is not equal to y.
cin.get(); This keeps the console window open intel the program is done running.
return 0;This exits the program.
I know that that migh be a little hard to tell where every thing go's so heres the whole code.
Well there you have it hope you learned somthing if you have any questions or problems feel free to ask.(leave comments on how to make my next tut better :)Code:#include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() { srand(time(NULL)); char playagain = 'y'; while (playagain == 'y') { int comguessHigh = 100; int comguessLow = 1; int thenumber = rand() % 100 + 1; int tries = 0, guess; for(;;) { cout << "Enter guess: "; cin >> guess; tries += 1; if (guess < thenumber) { cout << "\nThat's to low.\n" << endl; } else if (guess > thenumber) { cout << "\nThat's to high.\n" << endl; } else { cout << "That's it! You guessed it in " << tries << " tries." << endl; break; } int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow; cout << "Computer guess's: " << comguess << endl; if (comguess < thenumber) { cout << "\nThe computers guess was to low.\n" << endl; comguessLow = comguess; } else if (comguess > thenumber) { cout << "\nThe computers guess was to high.\n" << endl; comguessHigh = comguess; } else { cout << "\nSorry the computer guessed it." << endl; break; } } cout << "\nDo you want to play again? (y/n)" << endl; cin >> playagain; } cout << "\nOk mabye next time." << endl; cin.get(); return 0; }
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
Looks good. If you want to make the computer a little better, you could have it pay attention to your guesses, and react to them as well, and post it's guesses to help the player. +rep
Thanks,Ya good Idea.
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
I've made such a game, didn't interest me.
Why not?
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks