Archived

This topic is now archived and is closed to further replies.

MTT

Why cant i win?

Recommended Posts


  
//slot.cpp

//a simple gambleing game

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int money = 100;			//the money you have to gamble with

int slot ();

int main ()
{
	cout << "\t\t\t*****WELCOME TO THE SLOTS*****" << endl; //Greeting screen

	
	slot ();				//goto slot machines

	return 0;
}

int slot ()
{
	int bet = 0;			//controls how much you bet

	int replay = 0;			//cheks if player wants to play again

	const int max = 1;
	int slot1 = 0, slot2 = 0, slot3 =0;
							//the 3 symbols that come up


	while (money != 0)		//game loop

	{
		cout << "\n\nYou have " << money << " dollars\n";
		cout << "how much would you like to bet?: ";
		cin >> bet;			//sees how much is being bet


		if (bet <= money)	//money in range check

		{
			srand((unsigned)time( NULL ));
							//resets random number

			
			slot1 = rand() % max; //gets random number

			srand((unsigned)time( NULL ));
							//resets random number

			
			slot2 = rand() % max; //gets random number

			srand((unsigned)time( NULL ));
							//resets random number


			slot1 = rand() % max; //gets random number

			srand((unsigned)time( NULL ));
							//resets random number


			if (slot1 && slot2 && slot3 == 1)
			{				//Regular win

				cout << "\nYou win";
				money = bet * 2;
			}

			else if (slot1 && slot2 && slot3 == 0)
			{				//Jackpot win

				cout << "\nJACKPOT";
				money = bet * 5;
			}

			else
			{				//loose

				cout << "\nSorry, you lose";
				money -= bet;
			}

			cout << "\nWould you like to play again(1 for yes, 2 for no)?: ";
			cin >> replay;	//checking if player wants to keep trying


			switch (replay)
			{
				case 1: continue;
				break;

				case 2: cout << "\n\nYou have " << money << " dollars\n";
				return 0;	//end game

				break;
			}
		}//end of the if statement


		else
		{					//overbet statement

			cout << "You do not have that much money to bet";
		}
	
	}//end while loop

}	 //end int slot()

  
K, my problem is i cant seem to win, the code compiles fine except some warning that i don''t really understand. But when i play nomatter how many times i try i just wont win. I looked at the wiining to loosing ratio and if i calculated right it isnt that bad: 000-jackpot 001 010 011 111-win 110 100 so i figure its a problem with my code (correct me if im wrong), and i cant find whats wrong with it so sombody please help me out.

Share this post


Link to post
Share on other sites
The value of max in your program is 1. Anything % 1 is zero. Try changing max to 2. That, and your &&'s are off. You need to do: (slot1 == 0 && slot2 == 0 && slot3 == 0) and (slot1 == 1 && slot2 == 1 && slot3 == 1).

[edited by - SiCrane on March 18, 2003 5:20:43 PM]

Share this post


Link to post
Share on other sites
Obviously, you''re doing WAAY to much srand(). Calling srand will reseed the random number generator. So, instead you call the stand only _ONCE_, at the beginning of the program. When you call rand() it will take the random number, and generates a new one with the previous random number(So in fact, you can calculate the next random number if you now the seeding).

Remove all the srand() functions and it will fix your program.

Sand Hawk

Share this post


Link to post
Share on other sites
Thnx alot guys, that really helped. I see my errors now and get how the random things work better now. I had just added in an extra line of code to see what numbers were picked and i saw they were all 0s and i still wasnt winning but its all clerified now. Oh and i changed the money = bet * 2(and 5) to, money += bet * 2(and 5) so that i make money.

Share this post


Link to post
Share on other sites