<mosaic.cnfolio.com>
PROGRAM
1. /*
2. Sorts the cards in the player's array
3. START
4.    Get all of player’s cards in the array called cards
5.    Set cardnum equal to value of cards that player has
6.    Initialise position_num equal to 0
7.    Initialise position_num2 equal to 0
8.    Initialise temporary equal to 0
9.
10.    WHILE position_num is less than value of cards that player has minus one
11.       Increase position_num by 1
12.       WHILE position_num2 is less than value of cards that player has minus position_num minus 1
13.          Look in all positions higher than current position in array to find highest
14.          IF the value in array cards at position_num2 is less than the value in array cards at position_num2 add 1
15.             THEN
16.                Set temporary equal to value in array cards at position_num2
17.                Set value in array cards at position_num2 equal to value in array cards at position_num2 add 1
18.                Set value in array cards at position_num2 add 1 equal to temporary
19.       Increase position_num by 1
20.          END IF
21.       END WHILE
22.    END WHILE
23. END
24.
25. Asks for player 1's total cards value and then the values of the cards
26. START
27.    Initialise position_num equal to 0
28.    Initialise player 1’s total cards value as an integer
29.    Initialise the array player 1 cards equal to 0
30.
31.    Player 1 enters total cards value
32.
33.    IF player 1’s total cards value is less than 2
34.       THEN
35.          Display error message that input is too small
36.          End game
37.       ELSE
38.          IF player 1’s total cards value is greater than 10
39.             THEN
40.                Display error message that input is too large
41.                End game
42.          END IF
43.    END IF
44.
45.    Player 1 enters all of the values of their cards
46.
47.    WHILE position_num is less than player 1's total cards value
48.         Add each card value to the array player 1 cards at the position_num
49.         Increase position_num by 1
50.    END WHILE
51.
52.    Set position_num equal to 0
53.
54.    WHILE position_num is less than player 1's total cards value
55.     IF the array player 1 cards at position_num is less than 1
56.          THEN
57.                Display error message that a card value input is too small
58.                End game
59.          ELSE
60.             IF the array player 1 cards at position_num is greater than 10
61.                THEN
62.                   Display error message that a card value input is too large
63.                   End game
64.                ELSE
65.                         Display error message that a card value input is not valid
66.                         End game
67.             END IF
68.       END IF
69.    END WHILE
70. END
71.
72. Asks for player 2's total cards value and then the values of the cards
73. START
74.    Initialise position_num equal to 0
75.    Initialise player 2’s total cards value
76.    Initialise the array player 2 cards equal to zero
77.
78.    Player 2 enters total cards value
79.
80.    IF player 2’s total cards value is less than 2
81.       THEN
82.          Display error message that input is too small
83.          End game
84.       ELSE
85.          IF player 2’s total cards value is greater than 10
86.             THEN
87.                Display error message that input is too large
88.                End game
89.          END IF
90.    END IF
91.
92.    Player 2 enters all of the values of their cards
93.
94.    WHILE position_num is less than player 2's total cards value
95.         Add each card value to the array player 2 cards at the position_num
96.         Increase position_num by 1
97.     END WHILE
98.
99.    Set position_num equal to 0
100.
101.    WHILE position_num is less than player 2's total cards value
102.         IF the array player 2 cards at position_num is less than 1
103.             THEN
104.                 Display error message that a card value input is too small
105.                 End game
106.                 ELSE
107.                     IF the array player 2 cards at position_num is greater than 10
108.                         THEN
109.                             Display error message that a card value input is too large
110.                             End game
111.                         ELSE
112.                             Display error message that a card value input is not valid
113.                             End game
114.                     END IF
115.         END IF
116.    END WHILE
117. END
118.
119. Decides who wins
120. START
121.    Calls the function that sorts the cards in the player's array function and passes through the array player 1 cards and player 1’s total cards value
122.    Calls the function that sorts the cards in the player's array function and passes through the array player 2 cards and player 2’s total cards value
123.    Initialise position number equal to 0
124.
125.    WHILE position number is less than 10
126.         IF the array player 1 cards at position_num is greater than the array player 2 cards at position number
127.             THEN
128.                 Display player 1 wins
129.                 End game
130.                 ELSE
131.                     IF the array player 1 cards at position number is less than the array player 2 cards at position number
132.                         THEN
133.                             Display player 2 wins
134.                             End game
135.                     END IF
136.         END IF
137.    END WHILE
138.    Display draw
139.    End game
140. END
141. */
142.
143. /*This imports the library stdio into the file*/
144. #include <stdio.h>
145.
146. /*Calls the function that sorts the cards into descending order,
147. passing through an empty list and integer value*/
148. void card_sort(int[], int);
149.
150. /*This defines the function main,
151. that takes no arguments*/
152. int main()
153. {
154. /*Initialises the variables as integers that will be used in the function*/
155.   int x, p1cardsnum, p2cardsnum;
156. /*Initialises the arrays for player 1 and player 2's card values and initialises all the values to 0*/
157.   int p1Cards={0};
158.   int p2Cards={0};
159.
160. /*Reads input entered for player 1's card total value*/
161.   scanf("%2d", &p1cardsnum);
162. /*Displays relevant error message if necessary, and ends game if there is an error*/
163.   if ((p1cardsnum<2) || (p1cardsnum>10))
164.   {
165.       printf("Erroneous input for player 1. Please enter an integer between 2 and 10.");
166.       return 0;
167.   }
168.
169. /*Loops through all inputs for player 1's card values and reads each one into player 1's array*/
170.   for (x=0; x<p1cardsnum; x++)
171.   {
172.       scanf("%2d", &p1Cards[x]);
173.   }
174. /*Loops through all inputs for player 1's cards and then displays relevant error messages if necessary, and ends game if there is an error*/
175.   for (x=0; x<p1cardsnum; x++)
176.   {
177.       if ((p1Cards[x]<1) || (p1Cards[x]>10))
178.       {
179.           printf("Erroneous input for player 1. Please enter an integer between 1 and 10.");
180.           return 0;
181.       }
182.   }
183.
184. /*Reads input entered for player 2's card total value*/
185.   scanf("%2d", &p2cardsnum);
186. /*Displays relevant error message if necessary, and ends game if there is an error*/
187.   if ((p2cardsnum<2) || (p2cardsnum>10))
188.   {
189.       printf("Erroneous input for player 2. Please enter an integer between 2 and 10.");
190.       return 0;
191.   }
192.
193. /*Loops through all inputs for player 2's card values and reads each one into player 2's array*/
194.   for (x=0; x<p2cardsnum; x++)
195.   {
196.     scanf("%2d", &p2Cards[x]);
197.   }
198. /*Loops through all inputs for player 2's cards and then displays relevant error messages if necessary, and ends game if there is an error*/
199.   for (x=0; x<p2cardsnum; x++)
200.   {
201.       if ((p2Cards[x]<1) || (p2Cards[x]>10))
202.       {
203.           printf("Erroneous input for player 2. Please enter an integer between 1 and 10.");
204.           return 0;
205.       }
206.   }
207.
208. /*Sorts player 1's cards by calling the function that sorts the card values entered*/
209.   card_sort(p1Cards, p1cardsnum);
210.
211. /*Sorts player 2's cards by calling the function that sorts the card values entered*/
212.   card_sort(p2Cards, p2cardsnum);
213.
214. /*Loops through all values in player 1 and player 2's array,
215. then displays the relevant winner depending on who has a higher card and if their cards are all the same then it must be a draw,
216. then ends game*/
217.   for (int i=0; i<10; i++)
218.   {
219.       if (p1Cards[i]>p2Cards[i])
220.       {
221.           printf("Player 1 wins.");
222.           return 0;
223.       }
224.       else if (p1Cards[i]<p2Cards[i])
225.       {
226.           printf("Player 2 wins.");
227.           return 0;
228.       }
229.   }
230.   printf("Draw.");
231.   return 0;
232. }
233.
234. /*Defines the function that sorts the card,
235. that takes the arguments the players list and their total cards value*/
236. void card_sort(int cards[], int cardnum)
237. {
238. /*Initialises the variables as integers that will be used in the function*/
239.   int x, y, tempSwapValue;
240.
241. /*Creates a for loop while the current position is less than the value of cards that the player has minus one*/
242.   for (x=0; x<(cardnum-1); x++)
243.   {
244. /*Creates a for loop while the position number is less than the value of cards that the player has minus the original position number minus 1*/
245.     for (y=0; y<(cardnum-x-1); y++)
246.     {
247. /*Checks to see if the current position card value is less than the next position's card value*/
248.       if (cards[y] < cards[y+1])
249.       {
250. /*Swaps the cards so that the highest is moved to the front*/
251.         tempSwapValue=cards[y];
252.         cards[y]=cards[y+1];
253.         cards[y+1]=tempSwapValue;
254.       }
255.     }
256.   }
257. }