A simple game using RNG

This is a discussion on A simple game using RNG within the C++ Programming forums, part of the General Programming Boards category; Hi there, I am designing this simple game to generate two random numbers using RNG and get their sum, if ...

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question A simple game using RNG

    Hi there,
    I am designing this simple game to generate two random numbers using RNG and get their sum, if it is 7 or 11 the player wins and the budget of the game is incremented by the bet the player has input at the beginning of the game, if the sum is 2 or 3 or 12 the player loses and the budget is decremented by the bet the player has made, if the sum is otherwise other 2 random numbers are generated and their sum is got using the same bet the user has made at the beginning without re-asking the user for the bet..
    Now, the game is working but the problem is that when the sum is otherwise and it repeats the RNG process it keeps getting the same results for so many times and the 2 randomly generated numbers change after a long time.. what is wrong with that?
    All thanks,

    -Amy


    Code:
    // Including librarie(s) needed for running the program
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    // declaring a prototype functions
    int rng();
    int winorlose();
    int budgetcalculator(char, float, float);
    
    // starting the main function
    int main()
    {
    	float budget = 10000;
    	float bet;
    	char winlose;
    
    	cin>>bet;
    
    	do
    	{
    		winlose = winorlose();
    	}while(winlose=='n');
    
    	budget = budgetcalculator(winlose, budget, bet);
    	cout<<budget<<endl;
    
    	return 0;
    }
    // end of the main function
    
    int rng()
    {
    	// declaring local variables
    	const int n = 6; // the number of faces of the dice
    	int x1, x2, sum; // the 2 number generated by the RNG (x1 & x2), and their sum
    	
    	srand ( (unsigned) time (NULL) ); //Initialize RNG
    	
    	x1 = rand( ) % n + 1;	// Generate a number from the sequence
    	cout<<"The 2 generated numbers are: "<<x1<<" ";			// Print it
    			
    	x2 = rand( ) % n + 1;	// Generate another number from the sequence
    	cout<<"& "<<x2<<endl;			// Print it
    
    	sum = x1 + x2; // summing the two randomly generated numbers
    	cout<<"Thir sum is: "<<sum<<endl;
    
    	return sum;
    }
    //end of function
    
    int winorlose()
    {
    	int dicesum;
    
    	dicesum = rng();
    	
    	// if statment for checking if the sum of the 2 randomly generated numbers is 7 or 11
    	if((dicesum==7) || (dicesum==11))
    		return 'w';
    	else if((dicesum==2) || (dicesum==3) || (dicesum==12))
    		return 'l';
    	else
    		return 'n';
    	// end of if statment
    }
    
    int budgetcalculator(char state, float credit, float moneybet)
    {
    	if(moneybet<=credit)
    	 	if(state=='w')
    	 		credit += moneybet;
    		else if(state=='l')
    			credit -= moneybet;
    		
    	return credit;
    }
    It ain't illegal until you get caught..

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > srand ( (unsigned) time (NULL) ); //Initialize RNG
    Put this at the start of main()
    You only really need to do this ONCE per run of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Now, the game is working but the problem is that when the sum is otherwise and it repeats the RNG process it keeps getting the same results for so many times and the 2 randomly generated numbers change after a long time.. what is wrong with that?
    The problem is that your program isn't genarting a different random number in every single loop which causes it to test two numbers multiple times before generating another two different random numbers. This happens because you're seeding the random number generator in each loop. The random number generator should be seeded ONLY ONCE.

    take this line off the rng() function :
    Code:
    srand ( (unsigned) time (NULL) );
    place it before your do-while loop and see if your program starts working the way you wanted it.


    btw , your winorlose() function always returns a 'char' ; why did you declare it as an 'int'?


    hope this helps


    *EDIT* bah , nevermind. Salem was faster (as usual)
    Last edited by Brain Cell; 03-12-2005 at 03:55 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Many thanks guys
    It ain't illegal until you get caught..

  5. #5
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question

    In the game I wanted to like put the win's & lose's in a report file so I used file streams, but there are some errors which I don't know the reason of..
    All the error comes in this part when I put it outside the main (i.e. gloabal):
    Code:
    ofstream target;
    string fname = "report.txt";
    target.open(fname.c_str());
    But when I put this code inside the main I get no errors from the compiler.. but the problem is that when I put it inside the main function everytime I call the main function the file is re-opened so all the old data inside it are erased.. what can I do..
    Thanks

    -Amy

    The whole program code:
    Code:
    // Including librarie(s) needed for running the program
    #include <iostream>
    #include <ctime>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    // declaring a prototype functions
    int rng();
    char winorlose();
    int budgetcalculator(ofstream, char, float, float);
    
    // Start report file preparation
    ofstream target;
    string fname = "report.txt";
    target.open(fname.c_str());
    // End report file preparation
    
    // declaring a global variable
    float budget = 10000;
    
    // starting the main function
    int main()
    {
    	// declaring local variables
    	float bet;
    	char winlose;
    
    	cout<<"Your budget is: $"<<budget<<endl;
    	cout<<"Please input the amount of money you want to bet: ";
    	cin>>bet;
    
    	srand ( (unsigned) time (NULL) ); //Initialize RNG
    	do
    	{
    		winlose = winorlose();
    	}while(winlose=='n');
    
    	budget = budgetcalculator(target, winlose, budget, bet);
    	cout<<"Your budget now is: "<<budget<<endl<<endl;
    
    	// Start play again option
    	char playagain;
    	cout<<"Do you want to play again? [press (y) for yes, and (n) for no]: ";
    	toupper(playagain);
    	cin>>playagain;
    
    	if(playagain=='y')
    		main();
    	// End play again option
    	
    	// closing the output stream
    	
    	// Start showing game report	
    	string line;
    	string filename = "report.txt";
    	ifstream source;
    	source.open(filename.c_str());
    
    	cout<<endl<<"This is a report for money won and lost from the orignial budget: "<<endl;
    	cout<<"Original budget: $10000"<<endl;
    	cout<<"Final budget: $"<<budget<<endl;
    	cout<<"---------------------"<<endl;
    
    	while(!source.eof())
    	{
    		getline(source,line);
    		cout<<line<<endl;
    	}
    	// End showing game report
    
    	target.close();
    
    	return 0;
    }
    // end of the main function
    
    int rng()
    {
    	// declaring local variables
    	const int n = 6; // the number of faces of the dice
    	int x1, x2, sum; // the 2 number generated by the RNG (x1 & x2), and their sum
    	
    	x1 = rand( ) % n + 1;	// Generate a number from the sequence
    	cout<<"The 2 dice threw have the numbers: "<<x1<<" ";			// Print it
    			
    	x2 = rand( ) % n + 1;	// Generate another number from the sequence
    	cout<<"& "<<x2<<endl;			// Print it
    
    	sum = x1 + x2; // summing the two randomly generated numbers
    	cout<<"Thir sum is: "<<sum<<endl;
    
    	return sum;
    }
    //end of function
    
    char winorlose()
    {
    	int dicesum;
    
    	dicesum = rng();
    	
    	// if statment for checking if the sum of the 2 randomly generated numbers is 7 or 11
    	if((dicesum==7) || (dicesum==11))
    		return 'w';
    	else if((dicesum==2) || (dicesum==3) || (dicesum==12))
    		return 'l';
    	else
    		return 'n';
    	// end of if statment
    }
    
    int budgetcalculator(ofstream destination, char state, float credit, float moneybet)
    {
    	if(moneybet<=credit)
    	{
    	 	if(state=='w')
    		{
    			credit += moneybet;
    			destination<<"$"<<moneybet<<" won."<<"\n";
    		}
    		else if(state=='l')
    		{
    			credit -= moneybet;
    			destination<<"$"<<moneybet<<" lost."<<"\n";
    		}
    	}
    	else
    	{
    		cout<<"Bet is larger than your current budget."<<endl;
    		main();
    	}
    
    	
    	return credit;
    }
    It ain't illegal until you get caught..

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    In the game I wanted to like put the win's & lose's in a report file so I used file streams, but there are some errors which I don't know the reason of..
    All the error comes in this part when I put it outside the main (i.e. gloabal):
    you can't make a procedure global (target.open(fname.c_str());) . It must be within a function ,regardless whether its inside main or another function. If you want to use the same ofstream and string objects in multiple functions then make them global , open the file within main (or any other function) then output data to it. Another way would be sending the string and ofstream object by reference\address so you can use them.

    everytime I call the main function the file is re-opened so all the old data inside it are erased
    The problem is with your "open" mode , not main(). You only sent the file name to "open" member function , the other argument (mode) didn't recieve anything so its assumed ios::out (default mode).

    ios::out writes to the file you specified discarding the old contents , and if the file isn't found , it creates a new file with that name. You just need to use the "app" mode (append) so the new contents are appended to the end of the file. You need to specify the opening mode in "open" like this:
    Code:
    target.open("output.txt", ios::app);
    this will append to the file instead.


    hope this helps
    Last edited by Brain Cell; 03-12-2005 at 07:37 AM. Reason: typos
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Thumbs up

    I did not need this command (which outputs data to files without erasing what inside it) in this program, but I strongly needed it in another program of mine, so really thank you so much Brain Cell
    It ain't illegal until you get caught..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a simple Windows game
    By ejohns85 in forum C Programming
    Replies: 1
    Last Post: 05-22-2009, 01:46 PM
  2. A "guess my number" game, with simple AI
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 10:45 PM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 09:24 PM
  5. Replies: 1
    Last Post: 11-06-2001, 02:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21