Roulette!

This is a discussion on Roulette! within the C++ Programming forums, part of the General Programming Boards category; hey guys, i've been working on this program for like a week now, and decided to show you guys danielc.aspfreeserver.com/roulette.exe ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    Roulette!

    hey guys, i've been working on this program for like a week now, and decided to show you guys

    danielc.aspfreeserver.com/roulette.exe

    that is the link to it, just open it up and play!

    when you save the game, it sends certain variables that are needed to load the game to an output file (.txt) called save in the same directory as the roulette.exe file

    there are cheats for it too! (or just one cheat, lol)

    if you type in 123 as an option, you get a 90% chance of getting 100 dollars added to the bank, a 9% chance of 1000 dollars, and a 1% chance of 10,000 dollars!

    so, what do you guys think? and comments? have any ideas, maybe some more stats that I can add in that "stats" section? any feedback apreciated

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Mmmm... don't post an executable expecting much of a response. The internet is a haven for cynics and lets just face it... DanC... sounds like a hacker name. *wink wink*

    Post your code, it's safer, it's smoother, and it's really what we're here to help you with. You could make a good roulette game with really bad code and we'd probably say you're a better programmer than you are. Or perhaps vica versa.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    ok, here is the code...i worked hard on it, lol lemme know how it works out for you! (if you try it )

    *EDIT*

    if you get an error about the console.h file, just delete the include for that, and the jadedhoboconsole, that is just for the colored text


    Code:
    #include <iomanip>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctime>
    #include "Console.h"
    namespace con = JadedHoboConsole;
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    #include <string.h>
    using namespace std;
    
    int PickNum();
    int SpinWheel(int choice);
    int StraightBet();
    int RedBet();
    int BlackBet();
    int EvenBet();
    int OddBet();
    int First12();
    int Second12();
    int Third12();
    void Stats();
    double HighScore();
    void Save();
    void Load();
    
    
    double bank=100;
    double amountBet;
    int SaveArr[5];
    double HighScore1=0;
    int NumBets=0;
    int NumWon=0;
    int NumLost=0;
    double PercentWon;
    double PercentLost;
    
    int main ()
    {  
    	cout<<setiosflags(ios::fixed|ios::showpoint);
    	cout<<setprecision(2);
    	int option;
    	srand(unsigned(time(0)));
    	do
    	{
    	  bank=100.00;
    	  do
    	  {
    		  int choice=PickNum();
    		  SpinWheel(choice);
    	  }
    	  while(bank!=0);
    	  system("cls");
    	  if(bank==0.00||bank<0.00)
    		  cout<<"\n\n\n\t\tYou lose, you don't have any more money!\n\n\n\n\n";
    	  getch();
    	  cout<<"\n\n\tDo you want to play again? (1 for yes, 2 for no): ";
    	  cin>>option;
    	}
    	while(option!=2);
    	  return 0;
    }
    
    int PickNum()
    {
    	system("cls");
    	int choice;
    	cout<<con::fg_yellow<<"\n\n\t\tWhat do you want to bet on?\n";
    	cout<<"\n\tStraight bet(#00-36)...1"<<endl;
    	cout<<"\tRed....................2"<<endl;
    	cout<<"\tBlack..................3"<<endl;
    	cout<<"\tEven...................4"<<endl;
    	cout<<"\tOdd....................5"<<"\t\tBank: $"<<bank<<"\n";
    	cout<<"\tFirst 12...............6"<<endl;
    	cout<<"\tSecond 12..............7"<<endl;
    	cout<<"\tThird 12...............8"<<endl;
    	cout<<"\n\tStats..................9"<<endl;
    	cout<<"\tSave Game.............10"<<endl;
    	cout<<"\tLoad Game.............11"<<endl;
    	cout<<"\n\tChoose an option: ";
    	cin>>choice;
    	HighScore();
    	if(choice<=8)
    	{
    		cout<<"\n\n    Amount to bet from bank: ";
    		cin>>amountBet;
    		if(amountBet>bank)
    		{
    			cout<<"\n\tYou bet more than the bank! Press any key and try again.";
    			getch();
    			PickNum();
    		}
    	}
    	if(amountBet<0)
    	{
    		cout<<"\n\tYou can't bet a negative number! Try again...";
    		getch();
    		PickNum();
    	}
    	if(choice==10)
    	{
    		cout<<"\n\t\t....Game Saved! Press any key to continue";
    		getch();
    	}
    	if(choice==11)
    	{
    		cout<<"\n\t\t....Game Loaded! Press any key to continue.";
    		getch();
    	}
    	if(choice==123)
    	{
    		int cheat=rand()%100+1;
    		if(cheat==1)
    			bank=bank+10000;
    		if(cheat<99&&cheat>75)
    			bank=bank+1000;
    		else
    			bank=bank+100;
    	}
    	if(choice>11&&choice!=123)
    	{
    		cout<<"\n\tInvalid choice, try again.";
    		getch();
    		PickNum();
    	}
    	return choice;
    }
    
    int SpinWheel(int choice)
    {
    	if(choice==1)
    		StraightBet();
    	if(choice==2)
    		RedBet();
    	if(choice==3)
    		BlackBet();
    	if(choice==4)
    		EvenBet();
    	if(choice==5)
    		OddBet();
    	if(choice==6)
    		First12();
    	if(choice==7)
    		Second12();
    	if(choice==8)
    		Third12();
    	if(choice==9)
    		Stats();
    	if(choice==10)
    		Save();
    	if(choice==11)
    		Load();
    	return 0;
    }
    
    int StraightBet()
    {
    	system("cls");
    	NumBets++;
    	int WinningNum=rand()%38+1;
    	//cout<<WinningNum;
    	int StraightBetChoice;
    	cout<<"\n\n\tChoose a number from 0-36, or 00: ";
    	cin>>StraightBetChoice;
    	if(StraightBetChoice==WinningNum)
    	{
    		NumWon++;
    		cout<<"\n\tWinner!";
    		bank=bank+amountBet*25;
    		cout<<"\tYou now have $"<<bank;
    		getch();
    	}
    	if(StraightBetChoice!=WinningNum)
    	{
    		NumLost++;
    		cout<<"\n\tLoser! The winning number was "<<WinningNum<<".\n\n";
    		bank=bank-amountBet;
    		cout<<"\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    int RedBet()
    {
    	system("cls");
    	//1 is red and 2 is black
    	NumBets++;
    	int WinningNum=rand()%2+1;
    	if(WinningNum==1)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled ("<<WinningNum<<") was red, you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum==2)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tYou lose, the number rolled ("<<WinningNum<<") was black!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int BlackBet()
    {
    	system("cls");
    	//1 is red and 2 is black
    	NumBets++;
    	int WinningNum=rand()%2+1;
    	if(WinningNum==2)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled ("<<WinningNum<<") was black, you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum==1)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tYou lose, the number rolled ("<<WinningNum<<") was red!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int EvenBet()
    {
    	system("cls");
    	NumBets++;
    	int WinningNum=rand()%38+1;
    	if(WinningNum%2==0)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled was even ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum%2!=0)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tYou lose, the number rolled was odd! ("<<WinningNum<<").";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int OddBet()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	NumBets++;
    	if(WinningNum%2!=0)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled was odd ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*2);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum%2==0)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tYou lose, the number rolled was even! ("<<WinningNum<<").";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;	
    }
    
    int First12()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	NumBets++;
    	if(WinningNum<=12)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled was in the first 12 ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*3);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum>12)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tThe number rolled was NOT in the first 12 ("<<WinningNum<<"), you lose!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    int Second12()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	NumBets++;
    	if(WinningNum>=13&&WinningNum<=24)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled was in the second 12 ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*3);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum<13||WinningNum>24)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tThe number rolled was NOT in the second 12 ("<<WinningNum<<"), you lose!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    int Third12()
    {
    	system("cls");
    	int WinningNum=rand()%38+1;
    	NumBets++;
    	if(WinningNum>24)
    	{
    		NumWon++;
    		cout<<"\n\n\n\n\tThe number rolled was in the third 12 ("<<WinningNum<<"), you are a winner!";
    		bank=bank+(amountBet*3);
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	if(WinningNum<=24)
    	{
    		NumLost++;
    		cout<<"\n\n\n\n\tThe number rolled was NOT in the third 12 ("<<WinningNum<<"), you lose!";
    		bank=bank-amountBet;
    		cout<<"\n\n\t\tYou now have $"<<bank;
    		getch();
    	}
    	return 0;
    }
    
    
    
    void Save()
    {
    	ofstream outf("save.txt");
    	outf<<bank<<endl;
    	outf<<HighScore1<<endl;
    	outf<<NumBets<<endl;
    	outf<<NumWon<<endl;
    	outf<<NumLost<<endl;
    	outf.close();
    }
    
    void Load()
    {
    	ifstream inf("save.txt");
    	inf>>bank;
    	inf>>HighScore1;
    	inf>>NumBets;
    	inf>>NumWon;
    	inf>>NumLost;
    	inf.close();
    }
    
    void Stats()
    {
    	system("CLS");
    	PercentWon=(double(NumWon)/NumBets)*100;
    	PercentLost=(double(NumLost)/NumBets)*100;
    	cout<<"\n\n\t\tStats\n\n";
    	cout<<"\tHighest Amount Ever in Bank: $"<<HighScore();
    	cout<<"\n\tNumber of Bets Made: "<<NumBets;
    	cout<<"\n\tNumber of bets won: "<<NumWon;
    	if(NumBets!=0)
    		cout<<" ("<<PercentWon<<"%)";
    	cout<<"\n\tNumber of bets lost: "<<NumLost;
    	if(NumBets!=0)
    		cout<<" ("<<PercentLost<<"%)";
    	getch();
    	system("CLS");
    }
    
    double HighScore()
    {
    	if(bank>HighScore1)
    		HighScore1=bank;
    	return HighScore1;
    }
    
    
    
    //old save/load funtions
    
    /*void Load()
    {
    	int num1;
    	ifstream inf("save.txt");
    	inf>>num1;
    	bank=num1;
    	inf.close();
    }*/
    
    /*void Save(int bank)
    {
    	ofstream outf("save.txt");
    	outf<<bank;
    	outf.close();
    }*/

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Well, right off the bat the first thing I notice is it's not very standard. Using system calls is not a good idea because they can be dangerous and using system("CLS") is only applicable on windows, but then I suppose the whole program pretty much is.

    Another thing I noticed immediately in the code is you're mixing your header names. You use <ctime> meanwhile you're using <stdlib.h>. Why not <cstdlib>? If you're going to go modern with your header names you might as well do it around the board. I also couldn't compile your code because it has a user defined library that you didn't post.

    Next you seem to be using a whole lot of unnecessary int functions that could be void. There is no reason to have a function return an arbitrary 0 to main. If it's going to return a value, it should be a value that means something. Also, though I don't know that it's really defined as truly bad practice, I'm a believer of not doing as much as your doing in such simple functions. All that output of winner and loser is very repetitive in your functions. You could easily make your functions return a flag that says whether you won or lost to main and then just have a single output there.

    You should try to avoid making global variables the way you do. If you need them in functions, they should be passed as arguements.

    Now onto the actual program. It looks pretty. If you are on the right OS and can make it work right, it's well formatted, it's clean, and it seems to cover most of the angles. You obviously haven't given all the possible roulette options, yet, but what you have so far works nicely. I did notice however that the odds appear to be off. While they seem right in the code, I couldn't help but feel like I was winning a lot more than I should be. Maybe I just got really lucky, though.

    Not bad, though. You should start thinking more object oriented now, but for an early program it's pretty cool.
    Last edited by SlyMaelstrom; 03-08-2006 at 07:53 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    oo thanks for the advice, i like the idea of having the program return lost or won and having one single plave to print it out, that is a good idea that i didnt think of, and probably wouldnt know how to go about doing it exactly (i've only been learning c++ for about 6 months)

    right now im gonna go change all the unnecessary "int" funtions that could be void, i think i actually notcied that, but was too lazy at the time to fix it

    thanks for all of the comments, much apreciated



    PS, my high score is $15,460,000,000, lol
    Last edited by DanC; 03-08-2006 at 07:58 PM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by DanC
    PS, my high score is $14,50,46,00,00,000, lol
    Oh, well if that's the case, you should not only fix the odds, but also how the program handles commas.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    lol my bad, i typed the number without commas at first and then put them in fast after...i guess i only pressed the arrow key 2 times every time i typed a ","

    oh yeah, i made a topic about the commad thing, right now the program has no comma format, would you know by any chance how to set it to print out the commas automatically?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Your easiest bet would be with string streams. Convert your float to a std::string and read it in a 3 character substrings and a comma sequentially. But don't worry about that, yet.
    Sent from my iPadŽ

  9. #9
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Isn't it better to use

    Code:
    var1 += var2
    instead of

    Code:
    var1 = var1 + var2
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it's not really better... just saves ink when printing...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Isn't it better

    Yes, it's better. Other than being slightly more clear, it doesn't matter much for built-in types like int or double. It does make an efficiency difference for more complex objects like string or your own classes. And so as long as you'll be doing it that way for some cases, why not be consistent. It's similar to the general rule of preferring ++i instead of i++. Of course, in the code above, it is an extremely minor issue.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i prefer i++ actually

    and you can make a case both ways for readability. some people think it's easier to see it written out. for example, to calculate a 50% increase:
    Code:
    x+=x/2;
    //as opposed to
    x=(x/2)+x;
    I prefer += either way, but sometimes it's just not as clear as writing it out.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: Text-Based Roulette Game
    By mfm1983 in forum C++ Programming
    Replies: 30
    Last Post: 11-27-2006, 11:39 PM
  2. Help with roulette program
    By clc529 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 06:20 PM
  3. Don't know what to do
    By jlmac2001 in forum C++ Programming
    Replies: 7
    Last Post: 01-31-2003, 10:40 PM
  4. How do I make a spinning roulette wheel?
    By Yoshi in forum Game Programming
    Replies: 3
    Last Post: 01-13-2002, 07:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21