Thread: Slot Game

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Slot Game

    I've developed a slot machine game. It is perfect now except for 1 thing, the random numbers don't change. Have you got any advice?
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        srand(time(0));
        int num1=rand()%10+1;
            srand(time(0));
        int num2=rand()%10+1;
            srand(time(0));
        int num3=rand()%10+1;
        int coins=10;
        cout<<"---------------------------------------------------------------------"<<endl;
        cout<<"                 Welcome to slot machine"<<endl;
        cout<<"                    You have 10 coins"<<endl;
        cout<<"      If all 3 numbers that appear are the same, you get 10 coins"<<endl;
        cout<<"          If 2 numbers are the same, you get 3 coins"<<endl;
        cout<<"          If all 3 numbers are different, you lose 1 coin"<<endl;
        cout<<"                     Hit enter to play"<<endl;
        cout<<"---------------------------------------------------------------------"<<endl;cin.get();
        cout<<""<<endl;
        while(coins<100)
        {
        cout<<num1<<"     "<<num2<<"     "<<num3<<"     "<<endl;
        if(coins<1)
        {
            coins=coins+10;
            cout<<"You are bankrupt, your money is reset. You now have "<<coins<<endl;
            cout<<"coins"<<endl;
        }    
        else if(num1==num2&&num1==num3)
        {
            coins=coins+10;
            cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();
        }    
        else if(num1==num3||num1==num2||num2==num3)
        {
            coins=coins+3;
            cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();
        }
        else
        {
            coins=coins-1;
            cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;
        }
    }
    cout<<"You have won the game. Congratulations.";cin.get();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    call srand only once in the program. And in that whole whileloop you have there you never change the values of num1, num2 or num3.
    Last edited by Shakti; 09-24-2004 at 05:03 AM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I've edited it so that srand is called only once and the rand calls are placed inside the loop.
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int num1;
        int num2;
        int num3;
        int coins=10;
        srand(time(0));
        cout<<"---------------------------------------------------------------------"<<endl;
        cout<<"                 Welcome to slot machine"<<endl;
        cout<<"                    You have 10 coins"<<endl;
        cout<<"      If all 3 numbers that appear are the same, you get 10 coins"<<endl;
        cout<<"          If 2 numbers are the same, you get 3 coins"<<endl;
        cout<<"          If all 3 numbers are different, you lose 1 coin"<<endl;
        cout<<"                     Hit enter to play"<<endl;
        cout<<"---------------------------------------------------------------------"<<endl;cin.get();
        cout<<""<<endl;
        while(coins<100)
        {
        num1=rand()%10+1;
        num2=rand()%10+1;
        num3=rand()%10+1;
        cout<<num1<<"     "<<num2<<"     "<<num3<<"     "<<endl;
        if(coins<1)
        {
            coins=coins+10;
            cout<<"You are bankrupt, your money is reset. You now have "<<coins<<endl;
            cout<<"coins"<<endl;
        }    
        else if(num1==num2&&num1==num3)
        {
            coins=coins+10;
            cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();
        }    
        else if(num1==num3||num1==num2||num2==num3)
        {
            coins=coins+3;
            cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();
        }
        else
        {
            coins=coins-1;
            cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;
        }
    }
    cout<<"You have won the game. Congratulations.";cin.get();
    return 0;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    you guys are brilliant

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You still have one problem: If I have one coin, I insert it, I get results, but they are never evaluated. It tells me that I'm bankrupt and resets my coins, then another round is started.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    good point, ill get on it right away

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    fixed
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int num1;
        int num2;
        int num3;
        int coins=10;
        srand(time(0));
        cout<<"|------------------------------------------------------------------------------|"<<endl;
        cout<<"|                           Welcome to slot machine                            |"<<endl;                    
        cout<<"|                                By Paul Leone                                 |"<<endl;
        cout<<"|                              You have 10 coins                               |"<<endl;
        cout<<"|          If all 3 numbers that appear are the same, you get 10 coins         |"<<endl;
        cout<<"|                  If 2 numbers are the same, you get 3 coins                  |"<<endl;
        cout<<"|               If all 3 numbers are different, you lose 1 coin                |"<<endl;
        cout<<"|                              Hit enter to play                               |"<<endl;
        cout<<"|------------------------------------------------------------------------------|"<<endl;cin.get();
        cout<<""<<endl;
        while(coins<100)
        {
        num1=rand()%10+1;
        num2=rand()%10+1;
        num3=rand()%10+1;
        cout<<num1<<"     "<<num2<<"     "<<num3<<"     "<<endl;
        if(coins<1)
        {
            if(num1!=num2&&num1!=num3&&num2!=num3)
            {
                coins=coins+10;
            cout<<"You are bankrupt, your money is reset. You now have "<<coins<<endl;
            cout<<"coins"<<endl;cin.get();cin.get();
        }    
            else if(num1==num2&&num1==num3)
            {
                    coins=coins+10;
            cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();
        }
        else if(num1==num2||num1==num3||num2==num3)
        {   
                    coins=coins+3;
            cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();
        }
        else
        {
            coins=coins-1; 
            cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();
        }
        }    
        else if(num1==num2&&num1==num3)
        {
            coins=coins+10;
            cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();
        }    
        else if(num1==num3||num1==num2||num2==num3)
        {
            coins=coins+3;
            cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();
        }
        else
        {
            coins=coins-1;
            cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();
        }
    }
    cout<<"You have won the game. Congratulations.";cin.get();
    return 0;
    }
    Last edited by pizzapie; 09-24-2004 at 07:05 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Unnecessary code duplication. It should be more like (in pseudo-code):
    Code:
    main()
    {
      init and welcome;
    
      do {
        generate and display random numbers;
    
        if(jackpot) {
          jackpot message;
          coins += 10;
        } else if(minor win) {
          minor win message;
          coins += 3;
        } else {
          loss message;
          coins -= 1;
    
          if(coins <= 0) {
            bankrupt message;
            reset coins;
          }
        }
    
      } while(coins < 100);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    fair enough but i can't be arsed changing it, besides it doesn't matter because i decided to make it to see if i really understood the stuff i was learning. this proves that i do because i wasn't even learning random numbers
    Last edited by pizzapie; 09-24-2004 at 07:06 AM.

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. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM