Roulette in C
The aim here is to develop a roulette engine in C. The engine will be able to generate a random result based on the rules of roulette. The user will be able to place bets on the result and the engine will determine if the bet is a win or a loss. Their winnings will be calculated and stored for the duration of their session.
Git Repository⌗
Fork or browse the complete code yourself at github.com/danielhookins/cRoulette
Random spin of the wheel⌗
The first step is generating a random number between 0 and 37 - as there are 37 different options that the ball can land on.
The result can be a number from 0 to 36, plus an additional possibility of a double zero (00).
Number | Value |
---|---|
0 | 0 |
1 | 00 |
2 | 1 |
… | … |
37 | 36 |
To get the result we can generate a random number between 0 and 37 and then ascertain what the properties of the result are based on the number returned.
In C we can use the rand()
function from stdlib
to generate a random number.
#include <stdlib.h>
#include <time.h>
First we need to see the random number generator. We can do this by calling srand()
with a seed value.
// seed the random number generator
srand(time(NULL));
Then we can generate the random number.
/*
* Spin the wheel and return a number
*/
int get_result(void)
{
// spin wheel and return number between 0 and 37
int num = rand() % 38;
return num;
}
Ascertaining properties from the result⌗
In roulette each result has a number of properties that we can use to determine if a bet is a win or a loss. These include:
- The number itself
- The colour of the number
- The type of number (even, odd, high, low), etc.
The number itself⌗
This part may seem obvious but there are two caveats to be aware of:
- We need to be able to handle the double zero (00) result
- We need to return the number as a string
/*
* Get a human readable string for the result
* this is a number between 0 and 36, or 00 for 37
*/
char *get_result_string(int result){
// Malloc a string to hold the result
char *result_string = malloc(2);
if (result == 37) {
result_string = "00";
} else {
sprintf(result_string, "%d", result);
}
return result_string;
}
The colour of the number⌗
In roulette the numbers are split into two colours: red and black. The colours are split as follows:
- 1-10, 19-28: odd numbers are red, even numbers are black
- 11-18, 29-36: odd numbers are black, even numbers are red
- 0, 00: green
/*
* Determine whether the result is red or black or green
*/
char *get_colour(int result)
{
// Malloc a string to hold the result
char *colour = malloc(5);
// number ranges 1-10 and 19-28,
// even numbers are black and
// odd numbers are red
if (result >= 1 && result <= 10 || result >= 19 && result <= 28) {
if (result % 2 == 0) {
colour = "black";
} else {
colour = "red";
}
}
// number ranges 11-18 and 29-36,
// odd numbers are red and
// even numbers are black
else if (result >= 11 && result <= 18 || result >= 29 && result <= 36) {
if (result % 2 == 0) {
colour = "red";
} else {
colour = "black";
}
}
// 0 and 00 are green
else {
colour = "green";
}
return colour;
}
The parity of the number⌗
The result can be either even or odd. Note that 0 and 00 are neither even nor odd.
/*
* Determine whether the result is odd, even or N/A
*/
char *get_odd_even(int result)
{
// Malloc a string to hold the result
char *odd_even = malloc(4);
if (result == 37 || result == 0) {
odd_even = "N/A";
} else if (result % 2 == 0) {
odd_even = "even";
} else {
odd_even = "odd";
}
return odd_even;
}