Ok so I'm making a text based rpg kind of like zork. But different. so I need to add a dice roller/ number generator to the game because it's also like Dungeons and Dragons and idk how to cuz I'm a noob and I don't even know the c++ language. I've just been using youtube. Can someone please help me. Here's my code.
// The Realm of Zaad.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int Choice;
int Mode;
cout << "Welcome to The Realm of Zaad" << endl;
cout << "This is a text based rpg game. Meaning you don't actually see what's happening it is like a pen and paper role playing game but digital. ;)" << endl;
First, I would suggest you avoid using system("pause");. Instead, I would recommend you use Sleep();. You will need to include windows.h (if you're not using a Windows machine to create this, you will have to use a different include. I don't remember what it is for non-windows computers, but can easily look it up and edit this later if that's an issue for you.) and you will need to specify the amount of milliseconds for your program to sleep. If you don't know what that is, a millisecond is a thousandth of a second, so two seconds is 2,000 milliseconds.
#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
#include <windows.h>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
void randomTest(); //prototype of the function, needed if your function is written below main
{
int Choice;
int Mode;
cout << "Welcome to The Realm of Zaad" << endl;
cout << "This is a text based rpg game. Meaning you don't actually see what's happening it is like a pen and paper role playing game but digital. ;)" << endl;
Sleep(2000); //game pauses for 2 seconds
cout << "1.Single Player" << endl;
cout << "2.Multiplayer Player" << endl;
Sleep(2000); //game pauses for 2 seconds
cout << "" << endl;
randomTest(); //this line calls the function randomTest
return 0;
}
void randomTest(){//function prints a randomly generated number
default_random_engine randomGenerator(time(NULL)); //this is essentially the shell of the generator
uniform_int_distribution<int>randomInt(1, 10); //this line uses the random engine above to generate an integer between the provided minimum and maximum numbers
int randInt = randomInt(randomGenerator); //the generated number is then set equal to an integer for functionality
cout<<randInt; //finally, the integer is printed to the screen
}
For clarification, the reason you use #<ctime> and add the parameters (time(NULL)) is so that the generator doesn't use the same seed each time. A seed is basically the instance of random numbers generated. If you add those parameters, the seed is reset to another within a second of running the program. This way the only time you will ever possibly get the same number again is if you close and reopen the program within a second. If anything I said didn't make sense, let me know in a reply.
Well, if you are trying to add a dice to your game that has numbers, that's actually pretty simple. I would create another function that you would call to each time you'd need to roll the dice. First, it all depends on what numbers you are using. if you are wanting to do a 1-6 dice, then I would recommend using this:
int dice;
dice = 1 + rand() % 6;
You will need the header "#include <cstdlib> and #include <ctime> in order to run this. You will also want to put srand((int)time(0)) in your main function. Hopefully that is what you need. I am also a newb, so I'm not 100% sure on my answer. Just trying to help.
You will need to include windows.h (if you're not using a Windows machine to create this, you will have to use a different include. I don't remember what it is for non-windows computers, but can easily look it up and edit this later if that's an issue for you.)
They're using visual studio :P
@OP It may be more readable for you to use int main() since 1) you are not defining unicode so _tmain converts to main instead of wmain and 2) you are not using command line arguments so you don't necessarily need them.
Also, as mentioned earlier you should generally avoid using system("anything").
@Gingy when I input your modifications it worked fine until I inputed this
randomTest(); //this line calls the function randomTest
void randomTest(){//function prints a randomly generated number
default_random_engine randomGenerator(time(NULL)); //this is essentially the shell of the generator
uniform_int_distribution<int>randomInt(1, 10); //this line uses the random engine above to generate an integer between the provided minimum and maximum numbers
int randInt = randomInt(randomGenerator); //the generated number is then set equal to an integer for functionality
cout<<randInt; //finally, the integer is printed to the screen
}
I got a bunch of errors after that and I have no idea what they mean
#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
#include <windows.h>
usingnamespace std;
void randomTest(); // Moved. Can't have anything between int main()
// and the curly bracket that starts the program
int main() // Changed. Giblit explained why, already
{
int Choice;
int Mode;
cout << "Welcome to The Realm of Zaad" << endl;
cout << "This is a text based rpg game. Meaning you don't actually see what's happening it is like a pen and paper role playing game but digital. ;)" << endl;
Sleep(2000); //game pauses for 2 seconds
cout << "1.Single Player" << endl;
cout << "2.Multiplayer Player" << endl;
Sleep(2000); //game pauses for 2 seconds
cout << "" << endl;
randomTest();
return 0;
}
void randomTest(){//function prints a randomly generated number
default_random_engine randomGenerator(time(NULL)); //this is essentially the shell of the generator
uniform_int_distribution<int>randomInt(1, 10); //this line uses the random engine above to generate an integer between the provided minimum and maximum numbers
int randInt = randomInt(randomGenerator); //the generated number is then set equal to an integer for functionality
cout<<randInt; //finally, the integer is printed to the screen
}
i suggest you to not use that code, is not really noob friendly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <ctime>//header for using time(NULL);
#include <cstdlib> //header for using rand and srand;
int RanomGenerator(int max)
{
srand(time(NULL))
/*this is for making the "seed" of random
computers cannot make RANDOM numbers, so you just take a seed(a random number) from the current time.
*/
return rand() % max + 1;
/* this is the actual random number generator, as you can see rand, stands for random, and basiccaly using the module (%) you say random number between 0 and max
i added the +1 since if it is a dice roll, dices can't roll 0, so if the number is 0, will increment to +1
*/
}
//and to use the function:
int myrandom = RandomGenerator(6);
//or
cout << RandomGenerator(232);