<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. * * * * Introduction to Roulette * * * *
  3. * No respectable land based casino can be without a roulette table.
  4. * Roulette is one of the most popular games of chance there is.
  5. * American casinos introduced the American roulette wheel
  6. * with a double zero to make it more difficult for the players to win.
  7. * Double-zero wheel
  8. * 0-28-9-26-30-11-7-20-32-17-5-22-34-15-3-24-36-13-1-00-27-10-25-29-12-8-19-31-18-6-21-33-16-4-23-35-14-2
  9. *
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <math.h>
  16. #include <ctype.h>
  17. #include <time.h>
  18.  
  19. /* Function to check whether a character is digit */
  20. int checkchar(char * s)
  21. {
  22.     int i;                            //Take a number as input from user
  23.     for (i = 0; i < strlen(s); i++)   //The strlen function returns the length of the string pointed to by s
  24.         if (s[i] < '0' || s[i] > '9')
  25.         {
  26.             return -1;
  27.         }
  28.     return 0;
  29. }
  30. /* Function to check input numbers
  31. * For example, if you input two numbers a and b.
  32. * It will have a space between two numbers that looks clearer
  33. */
  34. int checkb(char *s)
  35. {
  36.     int i, count = 0;
  37.     for (i = 0; i < strlen(s); i++)   //The strlen function returns the length of the string pointed to by s
  38.         if (s[i] == ' ')
  39.         {
  40.             count++;
  41.         }
  42.     return count;
  43. }
  44.  
  45. /* Function to check the length of bet numbers from 0 to 36 and number 00 */
  46. int getnumber(char * check)
  47. {
  48.     char gett[500];
  49.     if (strlen(check) == 2 || strlen(check) == 1)
  50.     {
  51.         int checkcharacter = checkchar(check);
  52.         if (checkcharacter == 0)
  53.         {
  54.             int num = atoi(check);      //atoi() converts an ascii character array to an integer
  55.             if (num == 0 && strlen(check) == 2) return 37;
  56.             if (num <= 36)
  57.                 return num;
  58.         }
  59.     }
  60.     return -1;
  61. }
  62. /* Function to check bet amount at least greater than zore and less or equal chip bonus */
  63. int getmoney(int total, char * check)
  64. {
  65.     if (strlen(check) >= 1)
  66.     {
  67.         int checkcharacter = checkchar(check);
  68.         if (checkcharacter == 0)
  69.         {
  70.             int num = atoi(check);      //atoi() converts an ascii character array to an integer
  71.             if (num > 0 && num <= total)
  72.                 return num;
  73.  
  74.         }
  75.     }
  76.     return -1;
  77. }
  78.  
  79. /*
  80. * * * * Pseudo code * * * *
  81. * Main function *
  82. START
  83. Initialize bet number
  84. Initialize bet amouth to zero
  85. Initialize chip bonus to 500
  86. Initialize chis won to zero
  87. Initialize final chip to zero
  88. Initialize number of bets to one
  89. WHILE number of bets is less than 5 and chip bonus greater than zero
  90.   Get bet numbers and bet amouth
  91.   Spin wheel using American roulette wheel has 38 divisions, where numbers from 1 to 36, 0 and 00
  92.    THEN
  93.     Display bet number, bet amouth and winning number is 00
  94.    ELSE
  95.     Display bet number, bet amouth and another winning number
  96.   IF bet number equals winning number
  97.    THEN
  98.     Adding chips won equal bet amouth multiplied by 35
  99.     Display chips won as payout value
  100.    ELSE
  101.     Subtracting bet amouth from chip bonus
  102.     Display chips lost as payout value
  103.   END IF
  104.   Increase number of bets by one
  105. END WHILE
  106. Adding two numbers chip bonus and chips won
  107. Display current worth as final value
  108. END
  109. */
  110.  
  111. int main(void)
  112. {
  113.     int num;
  114.     int money = 0;
  115.     long total = 500;
  116.     int ChipsWon = 0;
  117.     int ChipsLost = 0;
  118.     int final = 0;
  119.     int count = 1;
  120.    
  121.     printf("=== WELCOME TO ROULETTE GAME ===");
  122.     printf("\n===========================");
  123.     printf("\n       Chip worth: %ld", total);
  124.     printf("\n===========================");
  125.     printf("\n____________________________________");
  126.     printf("\n|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|","00", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36");
  127.     printf("\n____________________________________");
  128.     printf("\n|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|","0 ", "2", "5", "8", "11", "14", "17", "20", "23", "26", "29", "32", "35");
  129.     printf("\n_____________________________________");
  130.     printf("\n|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|","  ", "1", "4", "7", "10", "13", "16", "19", "22", "25", "28", "31", "34");
  131.     printf("\n_____________________________________");
  132.     printf("\n===========================");
  133.     /*Initialise the random number generation with the current time
  134.      *This only needs to be called once
  135.      *Read array elements one by one using for loop and
  136.      *the rand() function is called each time a new random number is required.
  137.      *The modulus operator generates a number between 0 and 37
  138.      */
  139.     srand ( time(NULL) );
  140.     int win[4];
  141.     for (int i=0;i<4;i++)
  142.     win[i]= rand() % 38;
  143.    
  144.     while (count < 5 && total > 0)
  145.     {
  146.         printf("\nROUND: %d", count);
  147.        
  148.         char input[100];
  149.         char *array[10];
  150.         int i = 0;
  151.         int checkstring;
  152.         do
  153.         {
  154.             i = 0;
  155.             checkstring = 0;           
  156.             memset(input, 0, sizeof(input))//This function returns a pointer to the memory area input
  157.             gets(input);                     //sizeof(input) equals 400 bytes in this instance   
  158.             /* Input Validation */
  159.             if (strlen(input)==0 || input[0]=='\n')
  160.             {
  161.                checkstring=-1;
  162.                printf("\nPlease input bet number and bet value again!!");
  163.                break;
  164.             }
  165.             else
  166.             {
  167.                int check = checkb(input);
  168.                int length = strlen(input);
  169.                if (check == 1)
  170.                {                       //strtok() divides the string into tokens
  171.                   array[i] = strtok(input, " "); //that 'input' is searched until the space character is found
  172.                   while (array[i] != NULL)    //the first token is returned array[i] and i points to that token (string)
  173.                   {                        //in order to get next token and to continue with the same string
  174.                      array[++i] = strtok(NULL, " "); // NULL is passed as first argument since strtok maintains a static pointer to your previous passed string
  175.                   }
  176.                }
  177.                if (i == 2)
  178.                {
  179.                   num = getnumber(array[0]);
  180.                   money = getmoney(total, array[1]);
  181.                   if (num == -1 || money == -1)
  182.                   {
  183.                      printf("\nMissing input !!");
  184.                      checkstring = 1;
  185.                   }
  186.                }
  187.                if (length < 3 || i != 2 || check != 1)
  188.                {
  189.                   printf("\nMissing input !!");
  190.                   checkstring = 1;
  191.                }
  192.             }
  193.         } while (checkstring == 1);
  194.         if (checkstring!=-1)
  195.         {
  196.            total = total - money;
  197.            int win_number = win[count-1];
  198.            srand ( time(NULL) );
  199.  
  200.            if (win_number == 37)
  201.            {
  202.                printf("\n_________________________________________________");
  203.                printf("\n|Bet number: %d|Bet Amount: %d|Winning number: 00|",num,money);
  204.                printf("\n_________________________________________________");
  205.                printf("\n===========================");
  206.            }
  207.            else
  208.            {   
  209.                printf("\n_________________________________________________");
  210.                printf("\n|Bet number: %d|Bet Amount: %d|Winning number: %d|",num,money,win_number);
  211.                printf("\n_________________________________________________");
  212.                printf("\n===========================");
  213.            }
  214.            if (win_number == num)
  215.            {
  216.                ChipsWon = ChipsWon + money * 35;
  217.                printf("\nChips Won: %d", ChipsWon);
  218.            if (money >= 0)
  219.                {
  220.                   if (num == 37)
  221.                   {
  222.                      //printf("\n in round %d you get %d $ with you choice : 00 ", count, money);
  223.                   }
  224.                   else
  225.                   {
  226.                      //printf("\n in round %d you get %d $ with you choice : %d ", count, money, num);
  227.                   }
  228.                }
  229.            }
  230.            else 
  231.            { 
  232.                ChipsLost = money;
  233.                printf("\nChips Lost: %d", ChipsLost);
  234.            }
  235.         }
  236.         printf("\n===========================");
  237.         count++;
  238.     }
  239.     final = total + ChipsWon;
  240.     printf("\nCurrent worth: %d ", final);
  241.    return 0;
  242. }