"Deal or No Deal" Game

This is a discussion on "Deal or No Deal" Game within the C++ Programming forums, part of the General Programming Boards category; Okay. This is my first project I am doing. I'm creating a simple console app that is not unlike the ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    8

    "Deal or No Deal" Game

    Okay. This is my first project I am doing. I'm creating a simple console app that is not unlike the "Deal or No Deal" television show.

    I have two sets of 26 different numbers that I would like to show.

    One of them containing the case numbers.

    The other containing what is inside the case.

    $0.01
    $1
    $5
    $10
    $25
    $50
    $75
    $100
    $200
    $300
    $400
    $500
    $750

    (split)


    $1,000
    $5,000
    $10,000
    $25,000
    $50,000
    $75,000
    $100,000
    $200,000
    $300,000
    $400,000
    $500,000
    $750,000
    $1,000,000

    The plan is to randomly assign one of the 26 cases to one of the 26 values. I am not exactly sure how to do this, and I was wondering if anyone would like to help.

    I didn't really understand pointers, but is this where I would use them?

    Cheers!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You can do this without pointers. In fact, you can do this with nothing more than an array and the rand function. Give it a try and tell us what you get.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Thanks for the hint. I'll think about it.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You can search the FAQs here if you need help using the rand function. Your most difficult task would be figuring out the algorithm you'll be using to decide how much money the bank offers after each round.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Quote Originally Posted by SlyMaelstrom
    You can search the FAQs here if you need help using the rand function. Your most difficult task would be figuring out the algorithm you'll be using to decide how much money the bank offers after each round.
    Yeah, I'm taking it in steps. I read something about array shuffling.

    Here is what I got (well just a part of it)...

    Code:
     
     
    //--- Shuffle elements by randomly exchanging each with one other.
            for (int i=0; i<26; i++) {
                int r = rand() % 26;  // generate a random position
                int temp = case[i]; case[i] = case[r]; case[r] = temp;
    ...and then manually, or find a way to automatically, assign each of those case numbers to one of the values. After that, use an algorithm to re-sort the cases.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Scratch that. I'm confusing myself.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Okay, I'm not as confused anymore.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    You could also use a switch statement to identify the box number. Use the random seed generator to decide on a box to open. But this is only one of a few ways.

    Code:
    for ( int open = 1; open <= 26' open++ )
    {
       box = rand() % 26 + 1; // variable to choose box to open
    
       switch ( box )
       {
       case 1:
       // box  contains... randomise this also
       break;
       };
    }
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The best way to randomly assign the values to the boxes is to put the values into a container in order, then randomly shuffle the data in the container.

    So put each value into a vector (or array). Then call random_shuffle to shuffle the data.

    If you can't or don't want to use the random_shuffle function, then you can simulate it yourself. Basically, you start from the end, then randomly pick an index in the vector/array and swap the value at that index with the value at the end. Then you decrement your counter by one and do it again with the smaller range. You keep doing this until you get to the front of the container and it will be completely shuffled. Be careful, it is easy to get the algorithm wrong and not get good randomness, which is why letting random_shuffle do it for you is better. If you do it yourself, feel free to post your attempt so we can help you verify that it is correct.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Close. Very close.

    The shuffle algorithm basically works with the following logic...
    • Randomly select one of the 26 elements.
    • Seperate it
    • Randomly select one of the remaining 25 elements.
    • Seperate it.
    • Randomly select one of the remaining 24 elements.

    'Seperating' basically amounts to sticking it at the end of the array, where it will not be selected for the next random roll.

    Code:
       for (int i= 0; i < 26; ++i) {
          // With each successive step, the number of unshuffled elements
          // decreases, so our random range decreases.
          int r = rand() % (26 - i);
    
          // Stick the randomly selected element at the end of the array,
          // where it can't be selected in the next iteration.
          int temp = case[(26 - 1) - i];
          case[(26 - 1) - i] = case[r];
          case[r] = case[(26 - 1) - i];
    A few points... they're sort of homework-y, but really, this is an algorithm to understand.
    • What happens when r = 26 - i?
    • What happens when i = 25?
    • There's really no reason this algorithm should use the back of the array for swapping instead of the front (the definition of 'r' would have to change). However, there's no difference since the result is random.


    Quote Originally Posted by jaromdl
    Yeah, I'm taking it in steps. I read something about array shuffling.

    Here is what I got (well just a part of it)...

    Code:
     
     
    //--- Shuffle elements by randomly exchanging each with one other.
            for (int i=0; i<26; i++) {
                int r = rand() % 26;  // generate a random position
                int temp = case[i]; case[i] = case[r]; case[r] = temp;
    ...and then manually, or find a way to automatically, assign each of those case numbers to one of the values. After that, use an algorithm to re-sort the cases.
    Callou collei we'll code the way
    Of prime numbers and pings!

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> There's really no reason this algorithm should use the back of the array for swapping instead of the front.
    That suggestion is for convenience as it makes the code easier to follow IMO.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    for reference, this is the shuffle algorithm I used in my poker game(changed to use integers):

    Code:
    #include <ctime>
    #include <cstdlib>
    
    void shuffle()
    {
    	int tempNum;
    
    	srand( time(NULL) ); // Seeds the random number generator with the current Time
    	for(int j = 0; j < size; j++) // loop through the array once
    	{
    		int k = rand() % size; //Produces a random integer number between 0 and size
    		tempNum = yourArray[k];		// Pick an int at random
    		yourArray[k] = yourArray[j]; // swaps the random int and the current int in the loop
    		yourArray[j] = tempNum;
    	}
    	return;
    }//! End of shufle Definition

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Unfortunately, that gives an uneven distribution. Try running it a thousand times times with a size of 5 and see how often each value gets assigned to each spot. They should be roughly even for a good shuffling algorithm.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    BTW, I wrote a simple test program to show the uneven distribution compared to random_shuffle which works pretty well:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <ctime>
    #include <cstdlib>
    
    void shuffle(int data[], int size)
    {
        int tempNum;
    
        for(int j = 0; j < size; j++) // loop through the array once
        {
            int k = std::rand() % size; //Produces a random integer number between 0 and size
            tempNum = data[k];        // Pick an int at random
            data[k] = data[j]; // swaps the random int and the current int in the loop
            data[j] = tempNum;
        }
        return;
    }//! End of shufle Definition
    
    void run_test(bool use_random_shuffle)
    {
        const int size = 5;
        const int iterations = 100000;
        int tallies[size][size] = { };
    
        if (use_random_shuffle)
            std::cout << "Using random_shuffle:\n";
        else
            std::cout << "Using shuffle:\n";
    
        for (int i = 0; i < iterations; ++i)
        {
            int data[size];
            for (int j = 0; j < size; ++j)
                data[j] = j;
    
            if (use_random_shuffle)
                std::random_shuffle(data, data + size);
            else
                shuffle(data, size);
    
            for (int j = 0; j < size; ++j)
                ++tallies[j][data[j]];
        }
    
        for (int i = 0; i < size; ++i)
        {
            std::cout << i << ": ";
            for (int j = 0; j < size; ++j)
            {
                std::cout << tallies[i][j] << ' ';
            }
            std::cout << '\n';
        }
        std::cout << '\n';
    }
    
    int main()
    {
        std::srand( static_cast<unsigned>(std::time(0)) );
    
        run_test(true);
        run_test(false);
        run_test(false);
        run_test(true);
    }
    One example of my output:
    Code:
    Using random_shuffle:
    0: 19941 19970 20079 19976 20034
    1: 19985 19953 20130 19981 19951
    2: 19828 20213 19943 20145 19871
    3: 20207 20018 19859 19977 19939
    4: 20039 19846 19989 19921 20205
    
    Using shuffle:
    0: 19991 24204 20765 18440 16600
    1: 20029 18017 23039 20659 18256
    2: 19983 18552 17648 22809 21008
    3: 20020 19190 18504 17836 24450
    4: 19977 20037 20044 20256 19686
    
    Using shuffle:
    0: 19723 24367 21115 18393 16402
    1: 19965 17808 23249 20619 18359
    2: 20258 18419 17323 22965 21035
    3: 19944 19290 18527 17978 24261
    4: 20110 20116 19786 20045 19943
    
    Using random_shuffle:
    0: 19943 20161 19959 19982 19955
    1: 19924 20078 20048 19866 20084
    2: 20176 19971 19980 19875 19998
    3: 19836 19823 20148 20081 20112
    4: 20121 19967 19865 20196 19851
    Notice that 4 (the last column) only comes in the 0 slot about 16.5% of the time, but it comes up in the 3 slot over 24% of the time with System_159's version (which is a very common mistake when trying to implement this algorithm). With random_shuffle they all will average about 20%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 11:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

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