Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

roulette game

This topic is 2548 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

Advertisement
Not really. The most laborious part would be to implement the rules I guess. Have you considered a text adventure though? I think those are a good starting point and fun to make.

Share this post


Link to post
Share on other sites
It's easy, assuming you make a program that has this sort of interaction (user entry after ?):


Welcome to Roulette!

You have $1000. What would you like to do:
1) Bet on Black
2) Bet on Red
3) Bet on Even
4) Bet on Odd
5) Bet on a number
Selection? 5
What number would you like to bet on (1 to 36)? 14
How much do you want to bet (1 to 1000)? 80
Spinning Wheel!
...
It lands on 10, Black!
You lose $80

You have $920. What would you like to do:

1) Bet on Black
2) Bet on Red
3) Bet on Even
4) Bet on Odd
5) Bet on a number
Selection? 2
How much do you want to bet (1 to 920)? 100

Spinning Wheel!
...
It lands on 28, Black!
You win $100!!


You have $1020. What would you like to do:

1) Bet on Black
2) Bet on Red
3) Bet on Even
4) Bet on Odd
5) Bet on a number
Selection?


And so on. From your previous posts, if you don't have a firm grasp on variables, loops, conditionals, then you will have trouble programming this, but it might be a good way to learn it.

Just remember, when you hit a problem, don't come straight here, try and figure it out yourself.

Share this post


Link to post
Share on other sites
I'm a beginner aswell and thought this would be good practice so i've tried to replicate the example above, below is the completed code but there is probably a much better way of doing it. I thought about creating
a poker game aswell for practice, but i'm not sure how I would go about creating a deck of cards and shuffling, keeping track about what is dealt and so on, I am assuming something like a 52 sized array but does anyone have any ideas how to implement it?



[source]
#include <stdio.h>
#include <time.h
#define LINE "____________________________________"
char name[20];
int balance;
char *colour;
char *stringoddeven;
int oddeven;
int redblack;

void selection();
void checkcolour(int spin);
void checkoddeven(int spin);

int main()
{
srand(time(NULL));
printf(LINE); printf("\n\tRoulette Simulator\n"); printf(LINE);
printf("\n\nEnter your name: ");
scanf("%s", &name);
printf("\n\nEnter your starting balance: ");
scanf("%d", &balance);
printf("\n\nDealer: Welcome to the casino, %s!", name);
selection();
return 0;
}

void selection()
{
int choice;
int bet;
int result;
int number;
int winloss;
char ckey;
while (balance > 0)
{
printf("\n\nName: %s Balance: $%d",name,balance);
printf("\n\nWhat would you like to do?\n\n");
printf("1. Bet on red\n2. Bet on black\n3. Bet on even\n4. Bet on odd\n5. Bet on a number\n");
printf("\nChoice: ");
scanf("%d", &choice);
if (choice == 5) { printf("\nWhat number would you like to bet on: "); scanf("%d", &number); }
printf("\nHow much do you want to bet (1 to %d)? ", balance);
scanf("%d", &bet);
printf("\n\nSpinning Wheel!\n.....\n");
do { result = (rand() % 38); } while (result==0);
checkcolour(result);
checkoddeven(result);
printf("It lands on %d, %s!", result, colour);
if (choice == 1 && redblack == 0) { winloss = (bet * 1); printf("\n\nYou won $%d!",winloss); balance += winloss; }
else if (choice == 2 && redblack == 1) { winloss = (bet * 1); printf("\n\nYou won $%d!",winloss); balance += winloss; }
else if (choice == 3 && oddeven == 1) { winloss = (bet * 1); printf("\n\nYou won $%d!",winloss); balance += winloss; }
else if (choice == 4 && oddeven == 0) { winloss = (bet * 1); printf("\n\nYou won $%d!",winloss); balance += winloss; }
else if (choice == 5 && number == result) { winloss = (bet * 3); printf("\n\nYou won $%d!",winloss); balance += winloss; }
else { balance -= bet; printf("\n\nYou lost $%d!", bet); }
printf("\n\nenter c to continue: "); scanf("%s", &ckey);
}
printf("\n\n%s, you have lost all your money, game over!\n",name);
}

void checkcolour(int spin)
{
if (spin == 1 || spin == 3 || spin == 5 || spin == 7 || spin == 9 || spin == 12 || spin == 14 || spin == 16 || spin == 18 || spin == 19 || spin == 21 || spin == 23 || spin == 25 || spin == 27 || spin == 30 || spin == 32 || spin == 34 || spin == 36)
{
colour = "Red";
redblack = 0;
}
else
{
colour = "Black";
redblack = 1;
}
}

void checkoddeven(int spin)
{
if (spin % 2) { oddeven = 0; stringoddeven = "odd"; } else { oddeven = 1; stringoddeven = "even"; }
}
[/source]

Share this post


Link to post
Share on other sites

I'm a beginner aswell and thought this would be good practice so i've tried to replicate the example above, below is the completed code but there is probably a much better way of doing it. I thought about creating
a poker game aswell for practice, but i'm not sure how I would go about creating a deck of cards and shuffling, keeping track about what is dealt and so on, I am assuming something like a 52 sized array but does anyone have any ideas how to implement it?


Hi Jonathan,

Nice work. There are always things that you can do better, but, that's a good start! One suggestion that will make life a little easier for you in the future, instead of making all those checks if a number is red, I'd suggest creating a const array that stores all the numbers that are red, like this (and some other changes):

const int RedNumbers[] =
{
1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36
};



// returns 1 if it Is Red, 0 if it's black
int CheckColour(int spin)
{
int i;
int IsRed = 0;
for (i = 0; i < sizeof(RedNumbers)/sizeof(int); i++) {
if (RedNumbers == spin) {
IsRed = 1;
break;
}
}

return IsRed;
}

// When you call CheckColour, you assign it your IsRed value
IsRed = CheckColour(result);


The one piece to explain is sizeof(). sizeof returns the size of the variable given, in bytes. So, an array of ints will be the number of ints in the array times the size of an int. An int is almost always 4 bytes long. So, if you only used sizeof(RedNumbers), it would return 60 (18/4). That's why you have to divide it by sizeof(int).

I would not suggest making a poker game, I would suggest making a blackjack game. The difficult part of poker is the evaluation of hands. There's a library named poker-eval (look it up, I'm blocked from work), that you could use when you are more experienced, but I wouldn't do it now.

Blackjack is good because it's much easier to determine the value of hands (just add). Shuffling isn't quite as easy as you believe. You can google some techniques, but a general idea would be to halve the deck (the deck being an array of 52 cards), and put cards from each 1/2 deck from random locations, back into the main deck, starting at 0. Re-do that process, basically, just like shuffling in real life.

Share this post


Link to post
Share on other sites
thanks alot for the feedback beer, I appriciate it. Very clever sizeof way of doing it!

I will take your advice and try and make blackjack next, to practice making a deck and shuffling before moving onto poker :)

thanks again for the tips

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!