[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2472
  • Last Modified:

slot machine game in visual c++

hi i need help in making a non graphic slot machine program. I have to use object oriented programming but i am confused about what classes to use and to relate them.Also i have no idea how to implement the debug version. please help me.



In a real slot machine the player pulls a handle to rotate the reels that have pictures printed on them. Winning or losing is determined by which pictures line up with the pay line, a line in the middle of a viewing window. If each reel shows the same winning picture along the pay line, then the player wins (certain single images are sometimes winners as well). The payout (the amount the player wins) depends on what pictures fall along the pay line.

In this project, the reels will be labeled with numbers from 1 through 22. A user can place a bet for one or more of units of currency (N), up to 10. The payout is 2*N in every case the same number falls along the pay line in each wheel, EXCEPT for the following combinations where the payout is different:

1st Reel 2nd Reel 3rd Reel Payout
2            2              2          3*N
7            7               7         20*N
7            7             any         5*N
13        13              13          4*N
19        19              19          4*N

Upon placing a bet, your slot machine will "spin the wheels", print the outcome, the payout and the current player's balance.

Before the game begins the player will be prompted to enter the amount of currency (s)he has available to play on the slot machine. The slot machine always starts with 1,000 unites of currency.


Testing

To facilitate testing, the executable will accept an option --debug which will tell the application to print the slot machine balance every time messages are printed.
The spinning of the wheel will be simulated through the use of the srand and rand functions, which are found in cstdlib.
 the player's account balance should be updated, as well as the machine's, at the end of the round.




0
hassaanb
Asked:
hassaanb
  • 5
  • 3
1 Solution
 
Svetlin_PanayotovCommented:
Here's a sample class.I coded it in a hurry and I can't guarantee that everything will work fine - but it will be some help at least.

Svetlin

#include <iostream>
#include <math.h>


using namespace std;

class CSlotMachine
{
private:
      int iMachineBalance, iPlayerBalance, iBet, iBetOn;
      bool bDebug;

protected:
      void DoBet();
      void Spin();

public:
      CSlotMachine(int iNewBalance, bool bIsInDebug = false)      //constructor
      {
            iMachineBalance = 1000;      //the machine always starts with 1000
            iPlayerBalance = iNewBalance;
            bDebug = bIsInDebug;
      }

      ~CSlotMachine()
      {
            //Well...nothing to do here
      }

      void Run()
      {
            this->DoBet();
            this->Spin();
      }

      int GetPlayerBalance()
      {
            return iPlayerBalance;
      }

};


void CSlotMachine::DoBet()
{
      cout << "Bet on which number?";
      cin >> iBetOn;
      cout << "How many?";
      cin >> iBet;
      if (iBet > iPlayerBalance)
      {
            cout << "You don't have " << iBet << "$";
            //Bet = what's left
            iBet = iPlayerBalance;
      }
}

void CSlotMachine::Spin()
{
      srand(23);
      int iFirst = 100;
      int iSecond = 100;
      int iThird = 100;
      int iPayout;
      //Generate numbers
      
      while (iFirst > 22)
            iFirst = rand();
      
      while (iSecond > 22)
            iSecond = rand();
      
      while (iThird > 22)
            iThird = rand();

      cout << iFirst << "\t" << iSecond << "\t" << iThird << endl;

      if (iFirst == iSecond == iThird == iBetOn)
      {
            switch (iFirst)      //check for those special cases
            {
            case (2):
                  iPayout = 3*iBet;
                  break;

            case (7):
                  iPayout = 20*iBet;
                  break;

            case (13):
                  iPayout = 4*iBet;
                  break;

            case (19):
                  iPayout = 4*iBet;
                  break;
            
            default:
                  iPayout = 2*iBet;
                  break;
            }

            cout << "You won " << iPayout << endl;
            iPlayerBalance += iPayout;
            iMachineBalance -= iPayout;
      }
      else
      {
            if (iFirst == iSecond == iBetOn == 7)
            {
                  //Special case
                  iPayout = 5*iBet;
                  cout << "You won " << iPayout << endl;
                  iPlayerBalance += iPayout;
                  iMachineBalance -= iPayout;
            }
            cout << "You lose" << endl;
            iPlayerBalance -= iBet;
      }

      cout << "Your balance is : " << iPlayerBalance << endl;

      if (bDebug)
            cout << "DEBUG: Machine balance = " << iMachineBalance;
}
0
 
hassaanbAuthor Commented:
i am getting this error


--------------------Configuration: slot machine - Win32 Debug--------------------
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/slot machine.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

slot machine.exe - 2 error(s), 0 warning(s)
when i add int main() i get
--------------------Configuration: slot machine - Win32 Debug--------------------
Compiling...
slot machine.cpp
C:\Documents and Settings\nasihas\slot machine\slot machine.cpp(11) : error C2601: 'DoBet' : local function definitions are illegal
C:\Documents and Settings\nasihas\slot machine\slot machine.cpp(25) : error C2601: 'Spin' : local function definitions are illegal
Error executing cl.exe.

slot machine.exe - 2 error(s), 0 warning(s)
0
 
Svetlin_PanayotovCommented:
Well...you know - there's a main() in all console apps :)
I sent you only the class - and the <include>'s it needs - and I suppose that you should be able to add your own main()
and use the class.OK.Here's everything.

#include "stdafx.h"

#include <iostream>
#include <math.h>


using namespace std;


class CSlotMachine
{
private:
     int iMachineBalance, iPlayerBalance, iBet, iBetOn;
     bool bDebug;

protected:
     void DoBet();
     void Spin();

public:
     CSlotMachine(int iNewBalance, bool bIsInDebug = false)     //constructor
     {
          iMachineBalance = 1000;     //the machine always starts with 1000
          iPlayerBalance = iNewBalance;
          bDebug = bIsInDebug;
     }

     ~CSlotMachine()
     {
          //Well...nothing to do here
     }

     void Run()
     {
          this->DoBet();
          this->Spin();
     }

     int GetPlayerBalance()
     {
          return iPlayerBalance;
     }

};


void CSlotMachine::DoBet()
{
     cout << "Bet on which number?";
     cin >> iBetOn;
     cout << "How many?";
     cin >> iBet;
     if (iBet > iPlayerBalance)
     {
          cout << "You don't have " << iBet << "$";
          //Bet = what's left
          iBet = iPlayerBalance;
     }
}

void CSlotMachine::Spin()
{
     srand(23);
     int iFirst = 100;
     int iSecond = 100;
     int iThird = 100;
     int iPayout;
     //Generate numbers
     
     while (iFirst > 22)
          iFirst = rand();
     
     while (iSecond > 22)
          iSecond = rand();
     
     while (iThird > 22)
          iThird = rand();

     cout << iFirst << "\t" << iSecond << "\t" << iThird << endl;

     if (iFirst == iSecond == iThird == iBetOn)
     {
          switch (iFirst)     //check for those special cases
          {
          case (2):
               iPayout = 3*iBet;
               break;

          case (7):
               iPayout = 20*iBet;
               break;

          case (13):
               iPayout = 4*iBet;
               break;

          case (19):
               iPayout = 4*iBet;
               break;
         
          default:
               iPayout = 2*iBet;
               break;
          }

          cout << "You won " << iPayout << endl;
          iPlayerBalance += iPayout;
          iMachineBalance -= iPayout;
     }
     else
     {
          if ((iFirst == iSecond) && (iSecond == iBetOn) && (iBetOn == 7))
          {
               //Special case
               iPayout = 5*iBet;
               cout << "You won " << iPayout << endl;
               iPlayerBalance += iPayout;
               iMachineBalance -= iPayout;
          }
          cout << "You lose" << endl;
          iPlayerBalance -= iBet;
     }

     cout << "Your balance is : " << iPlayerBalance << endl;

     if (bDebug)
          cout << "DEBUG: Machine balance = " << iMachineBalance;
}


int main(int argc, char* argv[])
{
      CSlotMachine* oMachine = new CSlotMachine(300,false);

      int iTest, iTestTwo;

      
      while (oMachine->GetPlayerBalance() > 0)
            oMachine->Run();
      
      delete oMachine;
      return 0;
}
0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
hassaanbAuthor Commented:
ok great
but it doesn't generate random numbers and i am suppose to break it in slot machine and spin classes. i don't get what RUN is.
0
 
Svetlin_PanayotovCommented:
Well...I don't get what you mean by "doesn't generate random numbers"...
And about Run() - it sort of wrapper for the private methods - if you for example decide to extend the class - write your private method and just add it in Run().It's sort of personal choice - I prefer to design my classes like that.
Svetlin
0
 
hassaanbAuthor Commented:
what i mean is that it generates same number every time i place a bet.If i seed it with time null instead of 23 it always makes you win at 0. Also i don't need to ask the user to select a number
so the machine can generate any number from 1-21. i only need them to place a bet between 1-10.
0
 
Svetlin_PanayotovCommented:
Well I hope you can fix that yourself.
0
 
Svetlin_PanayotovCommented:
For the random numbers - it can be something like

srand( time( NULL ) );
iFirst = rand();
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now