Jump to content
  • Advertisement
Sign in to follow this  
kev670

slot machine code check

This topic is 4252 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Im coding a slot machine but my betting function is working wrong. i was wondering if some fresh eyes could look at the code and maybe find a problem with the betting function. the betting works like this: • 3 strawberries - 10x the bet • 3 bells - 50x the bet • 3 7's - 80x the bet • 3 bars - 100x the bet • 2 bells - 10x the bet code:
#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <string>
using namespace std;
//----------------------------------------------------------------------
string slot1(int random_integer, string symbol);
string slot2(int random_integer, string symbol);
string slot3(int random_integer, string symbol);
double winMoney(double bet, int random_integer, string symbol);

//----------------------------------------------------------------------

int main() 
{ 
	double bet; int random_integer; string symbol; 
	
	srand(time(NULL));
	
	
	cout << "******* Welcome To the Slot Machine *******";
	for(;;)
	{
	cout << "\n----------------------------------------------------------------------\n";
	cout << "\n\nPlease enter your bet? ";
	cin  >> bet;
		
    cout << slot1(random_integer, symbol) << endl;
    cout << slot2(random_integer, symbol) << endl;
    cout << slot3(random_integer, symbol) << endl;
    
     
    
    cout << "\n\n" <<  winMoney(bet, random_integer, symbol);
    cout << "\n----------------------------------------------------------------------\n";

    }
     
    
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
string slot1(int random_integer, string symbol)

{
	
	random_integer = (rand()%10)+1; 
	if (random_integer  == 1 || random_integer == 2 || random_integer == 3 || random_integer == 4 )
	
	{	
		symbol = "strawberries";

	}
	
	else if ( random_integer == 5 || random_integer == 6 || random_integer == 7 )
	{
		  symbol ="Bell" ;
	}
	
	else if ( random_integer == 8 || random_integer == 9 )
	{
		 symbol = "7";
	}
	
	else    
	{
		symbol = "Bar";
	}   
	return symbol; 
} 
//----------------------------------------------------------------------

//----------------------------------------------------------------------
string slot2(int random_integer, string symbol)
{
	
	random_integer = (rand()%10)+1; 
	if (random_integer  == 1 || random_integer == 2 || random_integer == 3 || random_integer == 4 )
	
	{	
		symbol = "strawberries";

	}
	
	else if ( random_integer == 5 || random_integer == 6 || random_integer == 7 )
	{
		  symbol ="Bell" ;
	}
	
	else if ( random_integer == 8 || random_integer == 9 )
	{
		 symbol = "7";
	}
	
	else    
	{
		symbol = "Bar";
	}   
	return symbol; 
} 
//----------------------------------------------------------------------

//----------------------------------------------------------------------
string slot3(int random_integer, string symbol)
{

	random_integer = (rand()%10)+1; 
	if (random_integer  == 1 || random_integer == 2 || random_integer == 3 || random_integer == 4 )
	
	{	
		symbol = "strawberries";

	}
	
	else if ( random_integer == 5 || random_integer == 6 || random_integer == 7 )
	{
		  symbol ="Bell" ;
	}
	
	else if ( random_integer == 8 || random_integer == 9 )
	{
		 symbol = "7";
	}
	
	else    
	{
		symbol = "Bar";
	}   
	return symbol; 
} 
//----------------------------------------------------------------------

double winMoney(double bet, int random_integer, string symbol)
{
	if	   ((slot1(random_integer, symbol) == "strawberries") && 
	        (slot2(random_integer, symbol) == "strawberries") && 
	        (slot3(random_integer, symbol) == "strawberries"))
   
    {
    	bet = bet * 10;
    	
    	return bet;
    }   
    else if((slot1(random_integer, symbol) == "Bell") && 
	        (slot2(random_integer, symbol) == "Bell") && 
	        (slot3(random_integer, symbol) == "Bell"))
    {
    	bet = bet * 50;
    	
    	return bet;
    }
    else if((slot1(random_integer, symbol) == "7") && 
	        (slot2(random_integer, symbol) == "7") && 
	        (slot3(random_integer, symbol) == "7"))
    {
    	bet = bet * 80;
    	
    	return bet;
    }
    else if((slot1(random_integer, symbol) == "Bar") && 
	        (slot2(random_integer, symbol) == "Bar") && 
	        (slot3(random_integer, symbol) == "Bar"))
       		
    {
    	bet = bet * 100;
    	
    	return bet;
    }
    else if((slot1(random_integer, symbol) == "Bell"  && slot2(random_integer, symbol) == "Bell")
	         ||
	        (slot2(random_integer, symbol) == "Bell"  && slot3(random_integer, symbol) == "Bell")
	         ||
	        (slot1(random_integer, symbol) == "Bell"  && slot3(random_integer, symbol) == "Bell"))
    
    
    {
    	bet = bet * 10;
    	
    	return bet;
    }
    
       
    else
    
    {
    	return bet;  
    }   
}

Share this post


Link to post
Share on other sites
Advertisement
fixed it incase anyones interested;


#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
//----------------------------------------------------------------------
string slot1(int random_integer, string symbol);
string slot2(int random_integer, string symbol);
string slot3(int random_integer, string symbol);
double winMoney(double bet, string a,string b,string c);

//----------------------------------------------------------------------

int main()
{
double bet; int random_integer; string symbol, a, b, c;

srand(time(NULL));


cout << "******* Welcome To the Slot Machine *******";
for(;;)
{
cout << "\n----------------------------------------------------------------------\n";
cout << "\n\nPlease enter your bet? ";
cin >> bet;

a = slot1(random_integer, symbol);
b = slot2(random_integer, symbol);
c = slot3(random_integer, symbol);

cout << a << endl;
cout << b << endl;
cout << c << endl;



cout << "\n\n" << winMoney(bet, a, b, c);
cout << "\n----------------------------------------------------------------------\n";

}


}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
string slot1(int random_integer, string symbol)

{

random_integer = (rand()%10)+1;
if (random_integer == 1 || random_integer == 2 || random_integer == 3 || random_integer == 4 )

{
symbol = "strawberries";

}

else if ( random_integer == 5 || random_integer == 6 || random_integer == 7 )
{
symbol ="Bell" ;
}

else if ( random_integer == 8 || random_integer == 9 )
{
symbol = "7";
}

else
{
symbol = "Bar";
}
return symbol;
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
string slot2(int random_integer, string symbol)
{

random_integer = (rand()%10)+1;
if (random_integer == 1 || random_integer == 2 || random_integer == 3 || random_integer == 4 )

{
symbol = "strawberries";

}

else if ( random_integer == 5 || random_integer == 6 || random_integer == 7 )
{
symbol ="Bell" ;
}

else if ( random_integer == 8 || random_integer == 9 )
{
symbol = "7";
}

else
{
symbol = "Bar";
}
return symbol;
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
string slot3(int random_integer, string symbol)
{

random_integer = (rand()%10)+1;
if (random_integer == 1 || random_integer == 2 || random_integer == 3 || random_integer == 4 )

{
symbol = "strawberries";

}

else if ( random_integer == 5 || random_integer == 6 || random_integer == 7 )
{
symbol ="Bell" ;
}

else if ( random_integer == 8 || random_integer == 9 )
{
symbol = "7";
}

else
{
symbol = "Bar";
}
return symbol;
}
//----------------------------------------------------------------------

double winMoney(double bet, string a,string b,string c)
{
if ((a == "strawberries") &&
(b == "strawberries") &&
(c == "strawberries"))

{
bet = bet * 10;

return bet;
}
else if((a == "Bell") &&
(b == "Bell") &&
(c == "Bell"))
{
bet = bet * 50;

return bet;
}
else if((a == "7") &&
(b == "7") &&
(c == "7"))
{
bet = bet * 80;

return bet;
}

else if((a == "Bar") &&
(b == "Bar") &&
(c == "Bar"))
{
bet = bet * 100;

return bet;
}

else if((a == "Bell" && b == "Bell")
||
(b == "Bell" && c == "Bell")
||
(a == "Bell" && c == "Bell"))
{
bet = bet * 10;

return bet;
}


else

{
return bet;
}
}

Share this post


Link to post
Share on other sites
What precisely isn't working? Do you get a compiler error, linker error, runtime error, bad output? I'm guessing it's output from the looks of your code.

One suggestion I could make already is that you keep re-calling your slot functions when it looks like you mean to keep one value after the first call. For instance, you call slot1...3 to display the string results, but you call it again in winMoney(). What you probably want to do is call it once and store that result in a variable.

In addition, you could consolidate your slot1...3 functions into a single SpinSlot() function and call it three times, each time storing it in a variable that represents a slot.

Share this post


Link to post
Share on other sites
It looks like your WinMoney function is recalling your slot funtions and therefore generating new random values to generate your win, rather than using those you rolled initially.

For starters, you don't need three separate slot functions, since all three do exactly the same thing: make one and call it three times. Since it returns a string, return it to a waiting std::string and pass those three strings to your WinMoney function.


std::string Slot() {
int random_number = (rand%10) + 1;

if (random_number == 1 || random_number == 2 || random_number == 3 ||
random_number == 4) {
return "strawberry";

//etc...
}
int WinMoney (int bet, std::string slot1, std::string slot2, std::string slot 3) {
//you know what goes in here
}
int main() {
int bet, winnings;
std::string a, b, c;

std::cin >> bet;

a = slot();
b = slot();
c = slot();

winnings = WinMoney(bet, a, b, c);

std::cout << winnings;

return 0;
}


You'll notice a few changes, most noticably the single slot function, but you may notice the lack of arguments for that slot function. If you don't need to feed the function anything for it to work, don't declare arguments. You'll then see that I used slot() to initialize three separate strings, which are then passed to WinMoney: this will ensure the slots you see are the slots you're checking.

Hope that helps :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!