<mosaic.cnfolio.com>
PROGRAM
1. /*
2. Example input to the program:
3. 00 2
4. 4 4
5. 3 5
6. 5 10
7. */
8.
9. /*
10.    Sample out put:
11.    This program is written to bet on a single number. Format to enter a bet is:
12.    number amount i.e(1 1) on number one, the amount is \$1
13.    number amount i.e(0 0.5) on number zero, the amount fifty cents
14.    number amount i.e(00 2) on number double zero, the amount is \$2
15.    Player must make four bets
16.    Players can make bets
17.    User bet number 00, bet amount 2.000000, wining bet number 20 payout value -2.000000, Sub total -2.000000
18.    Players can make bets
19.    User bet number 4, bet amount 4.000000, wining bet number 26 payout value -4.000000, Sub total -6.000000
20.    Players can make bets
21.    User bet number 3, bet amount 5.000000, wining bet number 26 payout value -5.000000, Sub total -11.000000
22.    Players can make bets
23.    User bet number 5, bet amount 10.000000, wining bet number 5 payout value 380.000000, Sub total 369.000000
24. */
25.
26. /*
27.
28.
29.
30.    Algorithm:
31.    create a player structure to store bet information
32.    for(i<-1 to numberofbets
33.       read bet value and amount
34.       run validation check
35.       generate a random number as index between 0 and 37
36.       get wining string from double wheel array at the index
37.       Display winning number
38.       if(player not won)
39.          Do calculation for loosing game
40.       else
41.          Do calculation for winning game
42.
43.      Display payout value and sub total value
44.
45. */
46.
47. #include <stdio.h>
48. #include <string.h>
49. #include <stdlib.h>
50. #include <time.h>
51.
52. // number of values in double wheel
53. const short DoubleZeroWheelSize = 38;
54.
55. // values of double wheel
56. static const char *DoubleZeroWheel[] =
57. {
58.    "0","28","9","26","30","11","7",
59.    "20","32","17","5","22","34","15",
60.    "3","24","36","13","1","00","27",
61.    "10","25","29","12","8","19","31",
62.    "18","6","21","33","16","4","23",
63.    "35","14","2"
64. };
65.
66. // number of turns that user can make
67. #define NumberOfBets 4
68. // player bet structure to store bet number in each turn
69. struct PlayerBet
70. {
71.    char BetNumber;   // bet number on each turn
72.    float money;         // bet amount on each turn
73.    float PayOutValue;   // pay out value on each turn
74. };
75. // player structure to store each player bet and sub total.
76. struct Player
77. {
78.    struct PlayerBet bets[NumberOfBets];   // playerbet structure array to store each bet information
79.    float SubTotal;                        // sub total for all turns
80. };
81.
82. // this function is to validate user input
83. int IsValidBet(struct PlayerBet *bet)
84. {
85.    // money should be positive value
86.    if (bet->money <= 0.0)
87.    {
88.       return 0;
89.    }
90.    for (int i = 0; i<DoubleZeroWheelSize; i++)
91.    {
92.       // compare value entered by user with double zero wheel values
93.       // if there is no matching value it means its wrong input.
94.       if (strcmp(DoubleZeroWheel[i], bet->BetNumber) == 0)
95.       {
96.          return 1;
97.       }
98.    }
99.    return 0;
100. }
101.
102. // This function have the logic of making a bet
103. void MakeBet(struct Player *p1, int betnumber)
104. {
105.    // Initialise bet information for each turn
106.    p1->bets[betnumber].BetNumber = '\0';
107.    p1->bets[betnumber].money = 0.0;
108.    printf("Players can make bets\n");
109.    // read bet number and bet amount
110.    scanf("%2s %f", &(p1->bets[betnumber].BetNumber), &(p1->bets[betnumber].money));
111.    // check if user has entered valid number and amount
112.    if (!IsValidBet(&p1->bets[betnumber]))
113.    {
114.       // user has not entered valid information. Give a message to the user
115.       printf("Bet with number =%s and Money=%f is invalid\n", p1->bets[betnumber].BetNumber, p1->bets[betnumber].money);
116.    }
117.    else
118.    {
119.       //Generate a number between 0 and 37.
120.       int winingNumberIndex = rand() % DoubleZeroWheelSize;
121.       char const*const winingnumberstr = DoubleZeroWheel[winingNumberIndex];
122.       // display bet number, amount enterd by user and wining number
123.       printf("User bet number %s, bet amount %f, wining bet number %s", p1->bets[betnumber].BetNumber, p1->bets[betnumber].money, winingnumberstr);
124.       // check if player won or not
125.       if (strcmp(winingnumberstr, p1->bets[betnumber].BetNumber) == 0)
126.       {
127.          // player won the bet. caculate pay out value and store it
128.          p1->bets[betnumber].PayOutValue = p1->bets[betnumber].money * 37 + p1->bets[betnumber].money;
129.       }
130.       else
131.       {
132.          // player lost the bet. deduct the money
133.          p1->bets[betnumber].PayOutValue = -p1->bets[betnumber].money;
134.       }
135.       // add up sub total and payout vlaue
136.       p1->SubTotal = p1->SubTotal + p1->bets[betnumber].PayOutValue;
137.       // display payout and sub total value
138.       printf(" payout value %f, Sub total %f\n", p1->bets[betnumber].PayOutValue, p1->SubTotal);
139.    }
140. }
141. // this function is to run the simulation
142. void Run(void)
143. {
144.    // create structure for player
145.    struct Player p1;
146.    // initialise subtotal to zero value
147.    p1.SubTotal = 0.0;
148.    // run it for NumberOfBets=4 times
149.    // if you want to run for different number of times then change NumberOfBets to a different value
150.    for (int i = 0; i<NumberOfBets; i++)
151.    {
152.       MakeBet(&p1, i);
153.    }
154. }
155.
156. int main()
157. {
158.    // initialisation of random number generator
159.    srand(time(0));
160.    // print message for help. How to use this program
161.    printf("This program is written to bet on a single number. Format to enter a bet is: \n");
162.    printf("number amount i.e(1 1) on number one, the amount is \$1\n");
163.    printf("number amount i.e(0 0.5) on number zero, the amount fifty cents\n");
164.    printf("number amount i.e(00 2) on number double zero, the amount is \$2\n");
165.    printf("Player must make four bets\n");
166.    Run();
167.    return 0;
168. }