Thread: Help with a script!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    Help with a script!

    Im doing an exercise of a book which says:

    Implement a function that returns a random integer in the range [low, high], where low and high are input parameters. The function prototype should look like this:

    int Random(int low, int high);

    Be sure to verify that your function implementation works by testing it.

    Using your Random function, write a virtual slot machine program. The program should start the player off with $1000, and should display a menu like this:

    Player's chips: $1000
    1) Play slot. 2) Exit.

    If the player enters "1", the program should ask the user to enter in his or her bet. The program needs to verify that a legal bet was placed; that is, a bet greater than 0 and less than or equal to the amount of money the player has. After the player has input his or her bet, the program must calculate three random numbers in the range [2, 7] and output them neatly to the screen. If all three numbers are sevens, then the player wins ten times their betting money; else if, the three numbers are all the same, but not sevens, then the player wins five times their betting money; else if, two out of the three numbers are the same then the player wins three times their betting money; else, the player loses his or her bet. At this point, calculate the player’s new chip amount and redisplay the menu. If at any point the player loses all of his or her chips, a message should be displayed to the player and the program should exit. Also, if the player enters “2” from the menu then the program should exit. Here is an example of what the output should look like:

    Player’s chips: $1000
    1) Play slot. 2) Exit. 1
    Enter your bet: 1500
    You did not enter a valid
    Place your bet: 1000
    3 3 7
    You win!
    Player’s chips: $3000
    1) Play slot. 2) Exit. 2
    Exiting…


    This is my script(sorry for the layout, i know its quite a mess but i changed things so many times that it messed up):

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    int Random(int low, int high);
    
    int main()
    {
    	srand (time(0));
    	int chips = 1000;
    	int o = 0;
    	
    	for(int exit = 0; exit != 1 || chips == 0; )
    	{
    	cout << "Player's chips: $" << chips << endl;
    	cout << "1) Play slot.  2) Exit.  ";
    	cin >> o;
    
        if(o == 1)
    	{
    		int bet = 0;
    		while(bet > chips || bet ==0)
    		{
    		cout << "Enter your bet: ";
    		cin >> bet;
    		if(bet > chips || bet == 0)
    		{
    			cout << "You did not enter a valid bet." << endl;
    		}
    		}
    
    		int r1 =0;
    		int r2 =0;
    		int r3 =0;
    		r1, r2, r3 = Random(2, 7);
    		cout << r1 << "   " << r2 << "   " << r3 << endl;
    
    		if (r1 == r2 == r3 == 7)
    		{
    			chips += bet * 10;
    			cout << "You win!" << endl;
    		}
    		else if(r1 == r2 == r3 != 7)
    		{
    			chips += bet * 5;
    			cout << "You win!" << endl;
    		}
    		else if(r1 == r2 || r1 == r3 || r2 == r3)
    		{
    			chips += bet * 3;
    			cout << "You win!" << endl;
    		}
    		else
    		{
    			chips -= bet;
    			cout << "You lost!" << endl;
    		}
    	}
    	else if (o == 2)
    	{
    		exit = 1;
    		continue;
    	}
    	else
    	{
    		cout << "Invalid option." << endl;
    	}
    	}
    
    		if(chips == 0)
    	{
    		cout << "Sorry, but you lost all your chips. You have to leave." << endl;
    	}
    	cout << "Exiting..." << endl;
    
    	system("PAUSE");
    }
    
    int Random(int low, int high)
    {
    	int result = low + rand() % (high - 1 - low);
    	return result;
    }
    But there is a problem. If you test it, the numbers each time you play are almost the same: " 0 0 3" or "0 0 4" or "0 0 5". Whats wrong with it? And, is there a way to simplify it? cause its quite a mess.

    Thank you,
    Cherry.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The following line:
    Code:
    r1, r2, r3 = Random(2, 7);
    probably doesn't do what you mean. It basically does the same as:
    Code:
    r1;
    r2;
    r3 = Random(2, 7);
    So, the first two parts really do nothing at all...

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are a few variable names that should be avoided at almost all cost:
    O (upper case o), l (lower case L), I (upper case I)
    Lower-case o is slightly less bad, but still not a good variable name.

    The reason for these names being bad is that they are really hard (particularly in some fonts) from the digits zero and one. Good programming fonts use zero with a slash through it and good "flag" on the number 1 to make it clear that those are digits, but we can not trust everyone to have those fonts on their machines.

    Following on to EVOEx's comments:
    Code:
    r1 == r2 == r3 == 7
    is probably not doing what you want either, as if we write it with parenthesis you'll see:
    Code:
    r1 == (r2 == (r3 == 7))
    So are you actually wanting to check if r1 is the same value as the truth value of r2 being equal to the truth value of r3 being equal to 7? (A truth value in C is either 0 (false) or 1 (true), although C accepts anything non-zero as true, and zero as false).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    thank you, i sloved it . and matsp what i tryed to do there was to check if r1,r2 and r3 where all = 7. but i solved it like this: r1 && r2 && r3 == 7.
    visual c++ 2005 still gives me 1 warning, its this 1:
    warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data.
    is that important?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cherry65 View Post
    thank you, i sloved it . and matsp what i tryed to do there was to check if r1,r2 and r3 where all = 7. but i solved it like this: r1 && r2 && r3 == 7.
    visual c++ 2005 still gives me 1 warning, its this 1:
    warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data.
    is that important?
    Yes, but not really. You could just add a cast to get rid of the warning.

    Code:
     r1 && r2 && r3 == 7
    I'm pretty sure this is not what you want:
    It's the same as:
    Code:
    r1 != 0 && r2 != 0 && r3 == 7
    It would be true EVERY time r3 == 7, I'd expect, since r1, r2 and r3 should all be random numbers between 2 and 7.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    oh you are right, i changed
    Code:
     r1 && r2 && r3 == 7
    for

    Code:
    r1 == r2 && r1 == r3 && r1 ==7
    .

    is that correct?
    and how should i add a cast to the time thing?
    Last edited by Cherry65; 11-23-2008 at 08:59 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cherry65 View Post
    oh you are right, i changed
    Code:
     r1 && r2 && r3 == 7
    for

    Code:
    r1 == r2 && r1 == r3 && r1 ==7
    .

    is that correct?
    and how should i add a cast to the time thing?
    Looks right.

    if t is a "time_t", then:
    Code:
    srand((unsigned int)t);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  2. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  3. how to implementate a registration script
    By TJa in forum C++ Programming
    Replies: 0
    Last Post: 10-28-2005, 02:33 AM
  4. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM