How do I add this to my game?

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;


system("pause");

cout << "1.Single Player" << endl;
cout << "2.Multiplayer Player" << endl;

system("pause");

cout << "" << endl;



return 0;
}
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.

For generating numbers, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
#include <windows.h>

using namespace 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.
His is a much better way to do it. Haha. Like I said, I'm a newb.
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").

I would also be weary of learning c++ on youtube. I would suggest you check out the tutorials on this site at http://www.cplusplus.com/doc/tutorial/ , the tutorials on http://www.learncpp.com/ and http://www.parashift.com/c++-faq/ when you get towards classes
They're using visual studio :P


Shouldn't they still need to use windows.h?
they are, stdafx.h includes it.
@Gingy I was referring to the "if you're not using a windows machine."
I am on a windows machine
Last edited on
@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
@DedSec

Could you tell me what error messages you are getting?
@DedSec
This runs, after my modification.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
#include <windows.h>

using namespace 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
}
Last edited on
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);

Hope this Helps
Do not call srand before every call to rand. That effectively reseeds the rng every time you want to generate a new number which is a terrible idea.

You should call srand exactly once: when your program starts.

whitenite's C++11 example is good, but I agree C++11 RNGs are not newbie-friendly. Though they are better and much more flexible RNGs.
Topic archived. No new replies allowed.