<mosaic.cnfolio.com>
PROGRAM
1. /*
2. START
3.    Display greeting message
5.       IF amount1 is greater than 10
6.          THEN Display too many cards for player 1 error.
7.          ELSE
8.             IF amount1 is less than 2
9.                THEN Display not enough cards for player 1 error.
10.             END IF
11.       END IF
12.    IF amount1 is less than or equal to 10 AND
13.       is greater than 2
14.       THEN obtain the amount of card values equal to amount1
15.    END IF
16.    IF any card value for player 1 is greater than 10
17.       THEN Display card value too high for player 1 error.
18.       ELSE
19.          IF any card value for player 1 is less than or equal to 0
20.             THEN Display card value too low for player 1 error.
21.          END IF
22.    END IF
24.       IF amount2 is greater than 10
25.          THEN Display too many cards for player 2 error.
26.          ELSE
27.             IF amount2 is less than 2
28.                THEN Display not enough cards for player 2 error.
29.             END IF
30.       END IF
31.    IF amount2 is less than or equal to 10 AND
32.       is greater than 2
33.       THEN obtain the amount of card values equal to amount2
34.    END IF
35.    IF any card value for player 2 is greater than 10
36.       THEN Display card value too high for player 2 error.
37.       ELSE
38.          IF any card value for player 2 is less than or equal to 0
39.             THEN Display card value too low for player 2 error.
40.          END IF
41.    END IF
42.    Get all cards to sort for player 1
43.    Set the position number to 0
44.    WHILE the position number is 1 less than the total number of cards
45.       Look at every position higher than the current position to find the lowest number
46.       Exchange the found lowest number with the current position number
47.       Add 1 to the position number
48.    END WHILE
49.    Get all cards to sort for player 2
50.    Set the position number to 0
51.    WHILE the position number is 1 less than the total number of cards
52.       Look at every position higher than the current position to find the lowest number
53.       Exchange the found lowest number with the current position number
54.       Add 1 to the position number
55.    END WHILE
56.    IF the highest card value for player 1 is greater than the highest card value of player 2
57.       THEN Display Player 1 wins.
58.    END IF
59.    IF the highest card value for player 2 is greater than the highest card value of player 1
60.       THEN Display Player 2 wins.
61.    END IF
62.    IF amount1 is greater than amount2
63.       THEN Display Player 1 wins.
64.       ELSE
65.          IF amount2 is greater than amount1
66.             THEN Display Player 2 wins.
67.             ELSE
68.                Display Draw.
69.          END IF
70.    END IF
71. END
72. */
73.
74. #include <stdio.h>
75. int main( void )
76. {
77.    printf("Welcome to the game! Please input first the number of cards in player 1's hand, then the values of all these cards. Then do the same for player 2.\n");
78.    /*This is the welcoming message that is displayed when the program is run, instructing the player of what to do*/
79.       int amount1;
80.       scanf( "%d", &amount1);
81.          if ( amount1 > 10 ) {
82.             printf("Too many cards in player 1's hand, the value has to be less than 10 for each player.\n");
83.             exit(-1);
84.             }
85.          else if ( amount1 < 2 ) {
86.             printf("Not enough cards in player 1's hand, the value has to be at least 2 for each player.\n");
87.             exit(-1);
88.             }
89.          /*else printf( "The number of cards in player 1's hand is %d.\n", amount1 );*/
90.    /*The code above is error checking to make sure the amount of cards in player 1's hand is not less than 2 or over 10, as per the rules in the assignment brief*/
91.    int value1, value2, value3, value4, value5, value6, value7, value8, value9, value10;
92.    /*I decided to use a SWITCH command here to better organise and structure this process, than if I used multiple IF commands*/
93.    switch( amount1 ) {
94.    case 10:
95.       if(scanf( "%d", &value10 ) == 1) {
96.          if ( value10 > 10 ) {
97.             printf("The first card value for player 1 is too high, the value has to be less than 10 for each card.\n");
98.             exit(-1);
99.             }
100.          else if ( value10 <= 0 ) {
101.             printf("The first card value for player 1 is too low, the value has to be at least 1 for each card.\n");
102.             exit(-1);
103.             }
104.       }
105.       /*This is a repeated error check segment, that makes sure the card values are between or equal to 1 and 10 as stated in the rules,
106.       and exits the program if a card is not between or equal to 1 and 10*/
107.       else printf("Insufficient number of cards for player 1.\n");
108.       /*This is a repeated check that tells the user that they have not entered enough cards
109.       for a player if there is not a correct number of cards*/
110.    case 9:
111.       if(scanf( "%d", &value9 ) == 1) {
112.       /*This IF command verifies the existence of the specific card value input,
113.       so that if it is not input the code does not run to prevent errors down the line*/
114.          if ( value9 > 10 ) {
115.             printf("The ninth from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
116.             exit(-1);
117.             }
118.          else if ( value9 <= 0 ) {
119.             printf("The ninth from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
120.             exit(-1);
121.             }
122.       }
123.       else printf("Insufficient number of cards for player 1.\n");
124.    case 8:
125.       if(scanf( "%d", &value8 ) == 1) {
126.          if ( value8 > 10 ) {
127.             printf("The eighth from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
128.             exit(-1);
129.             }
130.          else if ( value8 <= 0 ) {
131.             printf("The eighth from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
132.             exit(-1);
133.             }
134.       }
135.       else printf("Insufficient number of cards for player 1.\n");
136.    case 7:
137.       if(scanf( "%d", &value7 ) == 1) {
138.          if ( value7 > 10 ) {
139.             printf("The seventh from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
140.             exit(-1);
141.             }
142.          else if ( value7 <= 0 ) {
143.             printf("The seventh from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
144.             exit(-1);
145.             }
146.       }
147.       else printf("Insufficient number of cards for player 1.\n");
148.    case 6:
149.       if(scanf( "%d", &value6 ) == 1) {
150.          if ( value6 > 10 ) {
151.             printf("The sixth from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
152.             exit(-1);
153.             }
154.          else if ( value6 <= 0 ) {
155.             printf("The sixth from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
156.             exit(-1);
157.             }
158.       }
159.       else printf("Insufficient number of cards for player 1.\n");
160.    case 5:
161.       if(scanf( "%d", &value5 ) == 1) {
162.          if ( value5 > 10 ) {
163.             printf("The fifth from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
164.             exit(-1);
165.             }
166.          else if ( value5 <= 0 ) {
167.             printf("The fifth from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
168.             exit(-1);
169.             }
170.       }
171.       else printf("Insufficient number of cards for player 1.\n");
172.    case 4:
173.       if(scanf( "%d", &value4 ) == 1) {
174.          if ( value4 > 10 ) {
175.             printf("The fourth from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
176.             exit(-1);
177.             }
178.          else if ( value4 <= 0 ) {
179.             printf("The fourth from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
180.             exit(-1);
181.             }
182.       }
183.       else printf("Insufficient number of cards for player 1.\n");
184.    case 3:
185.       if(scanf( "%d", &value3 ) == 1) {
186.          if ( value3 > 10 ) {
187.             printf("The third from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
188.             exit(-1);
189.             }
190.          else if ( value3 <= 0 ) {
191.             printf("The third from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
192.             exit(-1);
193.             }
194.       }
195.       else printf("Insufficient number of cards for player 1.\n");
196.    case 2:
197.       if(scanf( "%d %d", &value2, &value1 ) == 2){
198.          if ( value2 > 10 ) {
199.             printf("The second from last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
200.             exit(-1);
201.             }
202.          else if ( value1 <= 0 ) {
203.             printf("The second from last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
204.             exit(-1);
205.             }
206.          if ( value1 > 10 ) {
207.             printf("The last card value for player 1 is too high, the value has to be less than 10 for each card.\n");
208.             exit(-1);
209.             }
210.          else if ( value1 <= 0 ) {
211.             printf("The last card value for player 1 is too low, the value has to be at least 1 for each card.\n");
212.             exit(-1);
213.             }
214.       }
215.       else {
216.       printf("Insufficient number of cards for player 1.\n");
217.       exit(-1);
218.       }
219.    }
220.    /* This would be better done using a function to get the input data */
221.    int amount2;
222.       scanf( "%d", &amount2 );
223.       if ( amount2 > 10 ) {
224.          printf("Too many cards in player 2's hand, the value has to be less than 10 for each player.\n");
225.          exit(-1);
226.          }
227.       else if ( amount2 < 2 ) {
228.          printf("Not enough cards in player 2's hand, the value has to be at least 2 for each player.\n");
229.          exit(-1);
230.          }
231.       /*else printf( "The number of cards in player 2's hand is %d.\n", amount2 );*/
232.    int value11, value12, value13, value14, value15, value16, value17, value18, value19, value20;
233.    switch( amount2 ) {
234.    case 10:
235.       if(scanf( "%d", &value20 ) == 1) {
236.          if ( value20 > 10 ) {
237.             printf("The first card value for player 2 is too high, the value has to be less than 10 for each card.\n");
238.             exit(-1);
239.             }
240.          else if ( value20 <= 0 ) {
241.             printf("The first card value for player 2 is too low, the value has to be at least 1 for each card.\n");
242.             exit(-1);
243.             }
244.       }
245.       else printf("Insufficient number of cards for player 2.\n");
246.    case 9:
247.       if(scanf( "%d", &value19 ) == 1) {
248.          if ( value19 > 10 ) {
249.             printf("The ninth from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
250.             exit(-1);
251.             }
252.          else if ( value19 <= 0 ) {
253.             printf("The ninth from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
254.             exit(-1);
255.             }
256.       }
257.       else printf("Insufficient number of cards for player 2.\n");
258.    case 8:
259.       if(scanf( "%d", &value18 ) == 1) {
260.          if ( value18 > 10 ) {
261.             printf("The eighth from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
262.             exit(-1);
263.             }
264.          else if ( value18 <= 0 ) {
265.             printf("The eighth from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
266.             exit(-1);
267.             }
268.       }
269.       else printf("Insufficient number of cards for player 2.\n");
270.    case 7:
271.       if(scanf( "%d", &value17 ) == 1) {
272.          if ( value17 > 10 ) {
273.             printf("The seventh from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
274.             exit(-1);
275.             }
276.          else if ( value17 <= 0 ) {
277.             printf("The seventh from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
278.             exit(-1);
279.             }
280.       }
281.       else printf("Insufficient number of cards for player 2.\n");
282.    case 6:
283.       if(scanf( "%d", &value16 ) == 1) {
284.          if ( value16 > 10 ) {
285.             printf("The sixth from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
286.             exit(-1);
287.             }
288.          else if ( value16 <= 0 ) {
289.             printf("The sixth from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
290.             exit(-1);
291.             }
292.       }
293.       else printf("Insufficient number of cards for player 2.\n");
294.    case 5:
295.       if(scanf( "%d", &value15 ) == 1) {
296.          if ( value15 > 10 ) {
297.             printf("The fifth from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
298.             exit(-1);
299.             }
300.          else if ( value15 <= 0 ) {
301.             printf("The fifth from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
302.             exit(-1);
303.             }
304.       }
305.       else printf("Insufficient number of cards for player 2.\n");
306.    case 4:
307.       if(scanf( "%d", &value14 ) == 1) {
308.          if ( value14 > 10 ) {
309.             printf("The fourth from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
310.             exit(-1);
311.             }
312.          else if ( value14 <= 0 ) {
313.             printf("The fourth from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
314.             exit(-1);
315.             }
316.       }
317.       else printf("Insufficient number of cards for player 2.\n");
318.    case 3:
319.       if(scanf( "%d", &value13 ) == 1) {
320.          if ( value13 > 10 ) {
321.             printf("The third from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
322.             exit(-1);
323.             }
324.          else if ( value13 <= 0 ) {
325.             printf("The third from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
326.             exit(-1);
327.             }
328.       }
329.       else printf("Insufficient number of cards for player 2.\n");
330.    case 2:
331.       if(scanf( "%d %d", &value12, &value11 ) == 2){
332.          if ( value12 > 10 ) {
333.             printf("The second from last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
334.             exit(-1);
335.             }
336.          else if ( value12 <= 0 )   {
337.             printf("The second from last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
338.             exit(-1);
339.             }
340.          if ( value11 > 10 ) {
341.             printf("The last card value for player 2 is too high, the value has to be less than 10 for each card.\n");
342.             exit(-1);
343.             }
344.          else if ( value11 <= 0 ) {
345.             printf("The last card value for player 2 is too low, the value has to be at least 1 for each card.\n");
346.             exit(-1);
347.             }
348.       }
349.       else {
350.       printf("Insufficient number of cards for player 2.\n");
351.       exit(-1);
352.       }
353.    }
354.    int data[] = {value1, value2, value3, value4, value5, value6, value7, value8, value9, value10};
355.    int initial, inside, highest, tmpSwapValue, cardnumber;
356.
357.    for ( initial = 0; initial < ( amount1 - 1 ); initial++ )
358.    {
359.       highest = initial;
360.
361.       for ( inside = (initial + 1); inside < amount1; inside++ )
362.          if ( data[inside] > data[highest] ) highest = inside;
363.
364.       tmpSwapValue = data[initial];
365.       data[initial] = data[highest];
366.       data[highest] = tmpSwapValue;
367.    }
368.    for ( cardnumber = 0; cardnumber < amount1; cardnumber++ ); /*printf("%d\n", data[cardnumber]);*/
369.
370.    int data2[] = {value11, value12, value13, value14, value15, value16, value17, value18, value19, value20};
371.    int initial2, inside2, highest2, tmpSwapValue2, cardnumber2;
372.
373.    for ( initial2 = 0; initial2 < ( amount2 - 1 ); initial2++ )
374.    {
375.       highest2 = initial2;
376.
377.       for ( inside2 = ( initial2 + 1 ); inside2 < amount2; inside2++ )
378.          if ( data2[inside2] > data2[highest2] ) highest2 = inside2;
379.
380.       tmpSwapValue2 = data2[initial2];
381.       data2[initial2] = data2[highest2];
382.       data2[highest2] = tmpSwapValue2;
383.    }
384.    for ( cardnumber2 = 0; cardnumber2 < amount2; cardnumber2++ ); /*printf("%d\n", data2[cardnumber2]);*/
385.
386.    for ( cardnumber = 0; cardnumber < amount1; cardnumber++ ) {
387.       if ( data[cardnumber] > data2[cardnumber] ) {
388.          printf("Player 1 wins.");
389.          exit(0);
390.          }
391.       if ( data2[cardnumber] > data[cardnumber] ) {
392.          printf("Player 2 wins.");
393.          exit(0);
394.          }
395.    }
396.    if( amount1 > amount2 ) printf("Player 1 wins.\n");
397.    else if( amount2 > amount1 ) printf("Player 2 wins.\n");
398.    else printf("Draw.\n");
399. }