<mosaic.cnfolio.com>
PROGRAM
1. /*
2. INPUT number of cards player 1 has
3. IF number of cards is lower than 2 or higher than 10
4.    OUTPUT error message
5.    END GAME
6. ENDIF
7.
8. INPUT player 1's cards
9. FOR each of player 1's cards
10.    IF card number is less than 1 or higher than 10
11.        OUTPUT error message
12.        END GAME
13.    ENDIF
14. ENDFOR
15.
16. INPUT number of cards player 2 has
17. IF number of cards is lower than 2 or higher than 10
18.    OUTPUT error message
19.    END GAME
20. ENDIF
21.
22. INPUT player 2's cards
23. FOR each of player 1's cards
24.    IF card number is less than 1 or higher than 10
25.        OUTPUT error message
26.        END GAME
27.    ENDIF
28. ENDFOR
29.
30. SORT player 1's cards from highest to lowest
31. SORT player 2's cards from highest to lowest
32.
33. IF number of player 1's cards is greater than player 2's
34.    biggestDeck = number of player 1's cards
35. ELSE
36.    biggestDeck = number of player 2's cards
37. ENDIF
38.
39. FOR the size of the biggest deck
40.    IF player 1's card is greater than player 2's card or player 2 has run out of cards
41.       OUTPUT "Player 1 Wins"
42.    ELSEIF player 2's card is greater than player 1's card of player 1 has run out of cards
43.       OUTPUT "Player 2 Wins"
44.    ENDIF
45. ENDFOR
46.
47. IF winner has not been found
48.    OUTPUT "Draw"
49. ENDIF
50. */
51.
52.
53.
54. #include <stdio.h>
55. #include <stdbool.h>
56. #include <string.h>
57.
58. //Bubble Sort
59. int *sortArray(int array[], int length) {
60.     length--;
61.     for (int i = 0; i < length; i++) {
62.         for (int j = 0; j < length; j++) {
63.             if (array[j] < array[j + 1]) {
64.                 int temp = array[j];
65.                 array[j] = array[j + 1];
66.                 array[j + 1] = temp;
67.             }
68.         }
69.     }
70.     return array;
71. }
72.
73. int main()
74. {
75.     bool validInputs = true;
76.     char errorMessage[300] = "Error:";
77.
78.     int player1CardCount;
79.     scanf("%d", &player1CardCount);
80.     if (player1CardCount < 2 || player1CardCount > 10) {
81.         validInputs = false;
82.         strcat(errorMessage, "\nPlayer 1 must have between 2 and 10 cards.");
83.     }
84.
85.     int player1Cards[player1CardCount];
86.     for (int i = 0; i < player1CardCount; i++) {
87.         scanf("%d", &player1Cards[i]);
88.         if (player1Cards[i] < 1 || player1Cards[i] > 10) {
89.             validInputs = false;
90.             strcat(errorMessage, "\nPlayer 1 has an invalid card in their deck! A cards's value must be between 1 and 10.");
91.             break;
92.         }
93.     }
94.
95.     int player2CardCount;
96.     scanf("%d", &player2CardCount);
97.     if (player2CardCount < 2 || player2CardCount > 10) {
98.         validInputs = false;
99.         strcat(errorMessage, "\nPlayer 2 must have between 2 and 10 cards.");
100.     }
101.
102.     int player2Cards[player2CardCount];
103.     for (int i = 0; i < player2CardCount; i++) {
104.         scanf("%d", &player2Cards[i]);
105.         if (player2Cards[i] < 1 || player2Cards[i] > 10) {
106.             validInputs = false;
107.             strcat(errorMessage, "\nPlayer 2 has an invalid card in their deck! A cards's value must be between 1 and 10.");
108.             break;
109.         }
110.     }
111.
112.     if (validInputs) {
113.
114.         int *player1CardsSorted = sortArray(player1Cards, player1CardCount);
115.         int *player2CardsSorted = sortArray(player2Cards, player2CardCount);
116.
117.         int biggestDeck = player1CardCount;
118.         if (player2CardCount > player1CardCount) {
119.              biggestDeck = player2CardCount;
120.         }
121.
122.         bool foundWinner = false;
123.         for (int i = 0; i < biggestDeck; i++) {
124.          if (player1Cards[i] > player2Cards[i] || i > player2CardCount - 1){
125.                foundWinner = true;
126.                 printf("Player 1 Wins");
127.                 break;
128.             } else if (player2Cards[i] > player1Cards[i] || i > player1CardCount - 1){
129.                 foundWinner = true;
130.                 printf("Player 2 wins");
131.                 break;
132.           }
133.        }
134.
135.        if (!foundWinner)
136.         printf("Draw");
137.
138.     } else {
139.         printf(errorMessage);
140.     }
141.
142.     return 0;
143. }