<mosaic.cnfolio.com>
PROGRAM
1. /*(
2. PSEUDO CODE
3. START
4.   Initialise variables and declare functions
5.
6.   Read the number of cards of the first player
8.   First player turn
9.   Call play()
10.   Sort the cards of the first player
11.   Call bubbleSort()
12.
13.   Read the number of cards of the Second player
15.   Second player turn
16.   Call play()
17.   Sort the cards of the second player
18.   Call bubbleSort()
19.
20.   Compare the cards of the two Players
21.   Call compareArrays()
22.
23.   Display the winner
24.   Call printWinner()
25.
26. END
27. )
28.
30. {
31.   DO
32.     input the number of cards with a size of 4 characters
33.     IF the size is not correct
34.       THEN
35.         re-input
36.       ELSE
37.         IF the characters are valid
38.           THEN
39.             IF the values are integers between 2 and 10
40.               THEN
41.                 set valid to true
42.             END IF
43.         END IF
44.     END IF
45.   }WHILE(not valid)
46.   Return value
47. }
48.
49. play(arguments: card number, cards list)
50. {
51.   WHILE (iterate until you reach card number)
52.     DO
53.       input integers
54.        IF enter is pressed
55.          THEN
56.            IF card number is not correct
57.              reinitialise the variables
58.          ELSE
59.            break
60.      WHILE (there is a space character)
61.
62.      IF the character is invalid
63.        THEN
64.          initialise the iteration
65.       END IF
66.
67.       IF the values are between 1 and 10
68.         THEN
69.           write the value in the cards list
70.   END WHILE
71.   Return the card list
72. }
73.
74. bubbleSort(arguments: list, iterations)
75. {
76.   iterate for every element of the list
77.   and check if the value of this element is less than
78.   the value of the next one.
79.   If yes swap their values
80. }
81.
82. compareArrays(arguments: cards of 1st player, cards of 2 player, number of cards of 1st player, number of cards of the 2nd player)
83. {
84.   FOR the maximum value of number of cards between the two players
85.      IF card of player 1 greater than card of player 2
86.       THEN
87.         return Player 1 wins
88.       ELSE IF card of player 1 less than card of player 2
89.         return Player 2 wins
90.       ELSE
91.         return Draw
92.      END IF
93. }
94. */
95.
96.
97.
98. #include<stdio.h>
99. #include<string.h>
100. #include<ctype.h>
101. #include<stdlib.h>
102. #include<stdbool.h>
103.
104. #define MAX_CARD_NO 10
105. #define MAX_DIGITS 2
106.
108. int play();
109. void bubbleSort(int [], int );
110. int compareArrays(int [], int [], int, int);
111. void printWinner();
112. int cardsP1[MAX_CARD_NO], cardsP2[MAX_CARD_NO];
113. int cardNoP1 = 0, cardNoP2 = 0, winner;
114.
115. int main(void)
116. {
117.   // Read the number of cards of the first player
118.   cardNoP1 = readNoCards();
119.   // First player turn
120.   cardsP1[cardNoP1] = play(cardNoP1, cardsP1);
121.   // Sort the cards of the first player
122.   bubbleSort(cardsP1, cardNoP1);
123.
124.   // Read the number of cards of the Second player
125.   cardNoP2 = readNoCards();
126.   // Second player turn
127.   cardsP2[cardNoP2] = play(cardNoP2, cardsP2);
128.   //Sort the cards of the second player
129.   bubbleSort(cardsP2, cardNoP2);
130.
131.   // Compare the lists of cards between the two players
132.   winner = compareArrays(cardsP1, cardsP2, cardNoP1, cardNoP2);
133.
134.   // Print Winner
135.   printWinner();
136.
137. return 0;
138. }
139.
141. {
142.   int val = 0;
143.   bool validInput = false;
144.   char noOfCards[MAX_DIGITS+2]; // Account for newline and 0 terminator
145.   do
146.   {
147.     if (!fgets(noOfCards, sizeof noOfCards, stdin))
148.     {
149.       // read error on input - panic
150.       exit(-1);
151.     }
152.     else
153.     {
154.       /*
155.       * Chech that the string entered is shorter than the buffer. If a newline
156.       * isn't present at the end of the buffer, input is regected and is re-read
157.       * from the stream until a newline is present.
158.       */
159.       if (!strchr(noOfCards, '\n'))
160.       {
161.         // input too long
162.         printf ("Error: Too long input - Enter a number between 2 and 10\n");
163.         while (fgets(noOfCards, sizeof noOfCards, stdin) && !strchr(noOfCards, '\n'));
164.       }
165.       else
166.       {
167.         char *chk = NULL; // points to the first character *not* converted by strtol
168.         val = (int) strtol(noOfCards, &chk, 10);
169.         if (isspace(*chk) || *chk == 0)
170.         {
171.           // Input was a valid integer string
172.           if (val > 1 && val <= MAX_CARD_NO)
173.           {
174.             validInput = true;
175.           }
176.         }
177.         else
178.         {
179.           printf ("Error: Invalid input - Enter a number between 2 and 10\n");
180.         }
181.       }
182.     }
183.   } while (!validInput);
184.   return val;
185. }
186.
187.
188. int play(int cardNo_, int cardsPlayer_[])
189. {
190.   int i = 0, iter;
191.
192.   while( i <= cardNo_ - 1)
193.   {
194.     int ch;
195.     do
196.     {
197.       ch = fgetc( stdin );
198.       if( ch == '\n')
199.       {
200.         // validate if the inputs are correct
201.         if(i != cardNo_)
202.         {
203.           printf("You should insert %d cards\n", cardNo_);
204.           //reinitialise the array
205.           for (iter = 0; iter < 10; ++iter)
206.           {
207.             cardsPlayer_[iter] = 0;
208.           }
209.             i = 0;
210.         }
211.         else
212.         {
213.           break;
214.         }
215.       }
216.     } while( isspace(ch) ) ;
217.     ungetc( ch, stdin ) ;
218.
219.     // rerun on non-integer input
220.     int number ;
221.     if( scanf( "%d", &number ) != 1 )
222.     {
223.       printf("Error: Invalid Input while inserting card numbers\n");
224.       char c;
225.       while((c = getchar()) != '\n' && c != EOF)
226.       i=0;
227.       continue;
228.     }
229.
230.     // Validate the cards in terms of their values
231.     if (number > 0 && number <= 10)
232.     {
233.       cardsPlayer_[i] = number;
234.       ++i ;
235.     }
236.     else
237.     {
238.       printf("Insert valid card numbers only\n");
239.     }
240.   }
241.   return cardsPlayer_;
242. }
243.
244. void bubbleSort(int list_[], int n_)
245. {
246.   int iter, d, tmp;
247.
248.   for (iter = 0 ; iter < ( n_ - 1 ); iter++)
249.   {
250.     for (d = 0 ; d < n_ - iter - 1; d++)
251.     {
252.       if (list_[d] < list_[d+1])
253.       {
254.         // Swapping
255.         tmp        = list_[d];
256.         list_[d]   = list_[d+1];
257.         list_[d+1] = tmp;
258.       }
259.     }
260.   }
261. }
262.
263. // Compare Arrays value by value
264. int compareArrays(int cardsP1_[], int cardsP2_[], int cardNoP1_, int cardNoP2_)
265. {
266.   int iter;
267.   bool equal = false;
268.   int size = (cardNoP1_ > cardNoP2_) ? cardNoP1_ : cardNoP2_;
269.
270.   for (iter = 0; iter < size; iter++){
271.     if (cardsP1_[iter] > cardsP2_[iter])
272.     {
273.       return 1;
274.     }
275.     else if (cardsP1_[iter] < cardsP2_[iter])
276.     {
277.       return 2;
278.     }
279.     else
280.     {
281.       equal = true;
282.       continue;
283.     }
284.   }
285.
286.   if ((size = iter -1 ) && (equal == true))
287.   {
288.     return 3;
289.   }
290. }
291.
292. // Print Winner
293. void printWinner()
294. {
295.   if (winner == 1)
296.   {
297.     printf("Player 1 wins\n");
298.   }
299.   else if (winner == 2)
300.   {
301.     printf("Player 2 wins\n");
302.   }
303.   else if (winner == 3)
304.   {
305.     printf ("Draw\n");
306.   }
307.   else
308.   {
309.     printf("Invalid winner\n");
310.   }
311. }