<mosaic.cnfolio.com>
PROGRAM
1. /* START
2.    < Beginning of the sequence >
3.
4.    Applying the rules of American style Roulette, which contains the number"00"
5.    4 bets in total will be placed.
6.    Read the bets placed by the player.
7.    Read the numbers chosen by the player.
8.
9.
10.    < ERROR CHECK HERE >
11.
12.       Check that the correct amount of valid numbers have been chosen.
13.        Check that the minimum number of bets have been placed.
14.       Check the number chosen is withing the range of greater than 37.
15.     Check the the bet value is a positive whole number.
16.
17.    IF the values chosen by the player are incorrect .
18.        THEN
19.          Display an error message describing the nature of the problem.
20.
21.       ELSE
22.
23.           continue with the game.
24.   END IF
25.
26.   < Core of the game >
27.
28. There are 4 bets to be made.
29.
30.    IF At least 1 round remains
31.          THEN
32.       Simulate the winning number from the roulette wheel.
33.           Compare the winning number with the numbers chosen by the player.
34.
35.           IF the winning number does not match
36.             THEN  Display the chosen player number
37.                   Display the amount the player bet on that number
38.                 Display a net profit for that round
39.           IF the winning number does match
40.             THEN  Display the chosen player number
41.                   Display the amount the player bet on that number
42.                 Display the amount the player has won if successful
43.                Display a net profit for that round
44.
45.
46.    END IF
47.    < Summary >
48.
49.       At the end of the 4 rounds of betting the results should be displayed.
50.           Display the total amount bet
51.       Display the total amount won
52.       Display the Net profit for the player.
53.
54.
55.   END*/
56.
57.
58.
59.
60. /*START*/
61.
62. #include <stdio.h>
63. #include <time.h>
64. #include <string.h>
65. #include <math.h>
66. #include <stdlib.h>
67.
68. void main(void)
69. {
70. srand( time(0) );
71. char numbers[38][4]; /* establish array for 38 spaces each space is 4characters big*/
72. char input[4][4];   /*reads the numbers picked by the user*/
73. char money_bet [4][4];    /*reads bet amounts from user*/
74. float  winnings[4] = {0}; /* to store any succesful bet winnings*/
75. int  converted_input[4];
76. float  converted_money[4];
77.
78. int i = 0;
79. int j = 0;
80. int lucky;
81.
82. float total_money = 0;
83. float total_winnings = 0;
84. float net_profit = 0;
85. float current_profit = 0;
86. int error = 0;
87. float factor = 36;
88.
89. for( int i =0; i<4; i++)
90. {
91.    for( int j =0; j<6; j++)
92.    {
93.       input[i][j] = '\0';
94.    }
95. }
96.
97.
98. for( int i = 0; i< 37; i++) /* this sets up the array of numbers from 0-37 ready for comparrison to the randomly generated number*/
99. {
100.
101.    sprintf(numbers[i], "%d", i);/*generates the list of valid numbers*/
102.
103. }
104. sprintf(numbers[37], "00"); /* seperate for 00 number assigned to random number 37 */
105.
106. scanf("%s %s %s %s %s %s %s %s", input[0], money_bet[0], input[1], money_bet[1], input[2], money_bet[2], input[3], money_bet[3] );/*assign each user input to its associated array*/
107.    for( i = 0; i < 4; i++)
108.    {
109.       for(  j = 0; j<38; j++) /* second loop*/
110.      {
111.
112.         if(strcmp(input[i],numbers[j])==0) /*  0 == true. Compares the bet to each of the generated numbers in the list.*/
113.         {
114.
115.              converted_input[i] = j; /*checks the number chosen is valid by comparing it to the array of generated numbers 0-36 + 0, 00*/
116.
117.              break;
118.            }
119.         }
120.   }
121. /*                             ERROR CHECKING          */
122.
123.
124. for( i = 0; i < 4; i++ ) /*checking every string contains numbers*/
125. {
126.       for( j = 0; j < strlen(money_bet[i]); j++ ) /* checks every character within each string for numbers, only checks the number of characters we need.*/
127.       {
128.             if(isdigit(money_bet[i][j]) == 0)/*if numbers are present returns true( 1 ) */
129.                {
130.                   error = 1;
131.                   printf("\ninvalid bet value");
132.                   break;/* exits nearest loop*/
133.                }
134.
135.        }
136.        for( j = 0; j < strlen(input[i]); j++ )
137.            {
138.             if(isdigit(input[i][j])== 0)
139.                {
140.                   error = 1;
141.                   printf("\ninvalid chosen number");
142.                   break;
143.                 }
144.
145.              }
146.
147.  if(error == 1)
148. {
149.    break;
150. }
151.
152. /*          THE CORE OF THE GAME                    */
153.
154. else
155. {
156.    converted_money[i] = atof(money_bet[i]);
157.
158. }
159. }
160.
161. if(error == 0)
162. {
163. for( i = 0; i < 4; i++) /* creates a loop to run 4 times. Compares inputs to the valid list of numbers*/
164. {
165.       lucky = rand() %38;
166.       printf("\nrandon number is %d",lucky);
167.       if(converted_input[i] != lucky)
168.       {
169.          printf("\nnumber chosen is %s", input[i]);
170.          printf("\nbet is %.2f\n", converted_money[i]);
171.          printf("\nWinning number is %s", numbers[lucky]);         /*prints what the random number is this round*/
172.          printf("\nThis round you won £%.2f", winnings[i]); /* displays the contents f the winnings array at current location*/
173.          total_money = total_money + converted_money[i];       /* calculates the total bet so far then prints value*/
174.        printf("\ntotal bet so far is %.2f", total_money);
175.           current_profit = current_profit + (winnings[i] - converted_money[i])/*calcs the net profit*/
176.          printf("\nNet profit this round is £ %.2f\n", current_profit);
177.       }
178.
179.
180.     if(converted_input[i] == lucky) /* compares number picked with random number generated */
181.    {
182.       printf("\nnumber chosen is %s", input[i]);
183.       printf("\nbet is %.2f\n", converted_money[i]);
184.       winnings[i] = converted_money[i]*factor;           /*current bet multiplied by the odds 36-1 */
185.       printf("\nWinning number is %s", numbers[lucky]);         /*prints what the random number is this round*/
186.       printf("\nThis round you won £%.2f", winnings[i]); /* displays the contents f the winnings array at current location*/
187.       total_money = total_money + converted_money[i];       /* calculates the total bet so far then prints value*/
188.       printf("\ntotal bet this round is %.2f", total_money);
189.       current_profit = current_profit + (winnings[i] - converted_money[i])/*calcs the net profit*/
190.       printf("\nNet profit this round is £ %.2f\n", current_profit);
191.
192.
193.
194.    }
195. /*          THE SUMMARY OF THE GAME AFTER 4 ROUNDS                    */
196.
197.
198. }
199.
200. total_money = converted_money[0]+converted_money[1]+converted_money[2]+converted_money[3];
201. net_profit = net_profit + ((winnings[0] +winnings[1] +winnings[2] +winnings[3]) - (converted_money[0] + converted_money[1] + converted_money[2] + converted_money[3]));
202.
203.
204.
205.
206. printf("\n\n\ntotal amount bet is £%.2f",total_money /*converted_money[0]+ converted_money[1] + converted_money[2] + converted_money[3]*/);
207. printf("\ntotal amount won is £%.2f", winnings[0] + winnings[1] + winnings[2] + winnings[3] );
208. printf("\nFinal Net profit is £%.2f", net_profit);
209.
210.
211. }
212. }
213. /*END*/