AllExperts > Object Oriented Programming 
Search      
Object Oriented Programming
Volunteer
Answers to thousands of questions
 Home · More Object Oriented Programming Questions · Answer Library  · Encyclopedia ·
More Object Oriented Programming Answers
Question Library

Ask a question about Object Oriented Programming
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About OOExpert
Expertise
I can answer questions about Object Oriented Programming, use of Design patterns, general C++ programming. I am probably not the better to answer specific question about Windows API programming, MFC, OWL, ...

Experience
10 years of C++ programming in Automobile, Telecom, Defense.

 
   

You are here:  Experts > Computing/Technology > Perl/PHP > Object Oriented Programming > Programming in Structured Basic

Object Oriented Programming - Programming in Structured Basic


Expert: OOExpert - 12/2/2003

Question
I need to write a BASIC program for a slot machine.
The slot machine has three windows where the results appear. The slot machine will haev six results- cherry, orange, bell, joker, green, and bar. On any pull of the handle each of those results has an equal probability of occuring. If any two of the three are the same, the machine pays five dollars. If all three are teh same, we hit the fifty dollar jackpot. Also, it costs two dollars to play the machine.

I think I should use two arrays and the "random" function to generate the results for each pull of the handle. Also, I should generate numbers for one to six, then let one equal "cherry", two equal "orange" and so.

I need to start with a bankroll of ten dollars, and have the program play the machine either until I'm broke,, hit the jackpot, or my arms gets tired (fifty tries). For each try, I need to print the three results, the money for that try, and the amount in the bankroll.

Answer
Hey,

Nice problem, but I don't see any question in your text ... I guess you want to know how I would solve that pb ...

First, I would create a class for the slot machine:

file cslotmachine.h
--------------------

#ifndef _CSLOTMACHINE_
#define _CSLOTMACHINE_

class CSlotMachine
{
public:
  CSlotMachine();
  ~CSlotMachine();

   unsigned short int GetResult(void);


protected:
private:
  static const unsigned short int MaxValue = 6;
  static const unsigned short int MaxSlot = 3;
};

#endif

Implememtation file for this class:

#include <iostream>
#include <stdlib.h>

#include "cslotmachine.h"

using namespace std;

//----------------------------------------------------
CSlotMachine::CSlotMachine()
{
  time_t seed;
  (void) time(&seed);
  
  srand(seed);      /*initialize random number generator*/
}

//----------------------------------------------------
CSlotMachine::~CSlotMachine()
{
}

//----------------------------------------------------
unsigned short int
CSlotMachine::GetResult(void)
{
  unsigned short int result[3];
  
  // get random result
  for (unsigned short int i = 0; i < MaxSlot; i++)
  {
     double r = ( (double)rand() / (double)(RAND_MAX+1) );
       result[i] = (unsigned short int) (r * (MaxValue + 1));
       cout << " -" << result[i] << "- ";
  }
  cout << endl;
  
  // analyse results
  unsigned short int nb_match = 0;
  
  if ((result[0] == result[2]) ||
     (result[0] == result[1]) ||
      (result[1] == result[2])) nb_match = 2;

  if ((result[0] == result[2]) &&
     (result[0] == result[1]) &&
      (result[1] == result[2])) nb_match = 3;

  
  return nb_match;
}


Main program:

#include <iostream>
#include <stdlib.h>

#include "cslotmachine.h"

using namespace std;





int main(int argc, char *argv[])
{
  CSlotMachine machine;
  
  int myBankRoll = 10; // $10
  
  do
  {
     myBankRoll -= 2;
     
      unsigned short int res = machine.GetResult();
      
      cout << "result = " << res << endl;
      
      switch (res)
      {
         case 0:
            break;
         
         case 2:
            myBankRoll += 5;
            break;
            
         case 3:
            myBankRoll += 10;
            break;
            
           default:
              cout << "Error in process ..." << endl;
              break;
      };
      
      cout << "Bank = " << myBankRoll << endl;
      system("PAUSE");
      
  } while (myBankRoll > 2); // if bank < 2, can't play anymore
 
 system("PAUSE");   
 return 0;
}


This program compiled and tested OK using dev-C++ 4.9.8.4.

You can see that we don't care in the program about cherry, orange, ... we just want to know the result. You can add functions to match integers with possible result and display them in the CSlotMachine::GetResult function.

Rgds.  

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.