<mosaic.cnfolio.com>
PROGRAM
1. #include<stdio.h>
2.
3. int main( void )
4. {
5.
6.    int input = {0};
7.    int P1C = {0};
8.    int P2C = {0};
9.    int P1H, P2H, PSH; /*players hand sizes and finding the smallest of the 2*/
10.    int out, in, highest, tmpSwapValue;
11.    int error = 0;
12.    int E1 = 0, E2 = 0, E3 = 0, E4 = 0, E5 = 0;
13.    int inputamount = 0;
14.
15.    for(int i = 0; i <= 21; i++) /*maximum 10 each hand including the amount, account for zero*/
16.    {
17.       scanf( "%d", &input[i]); /*put all our input values into an array so we can access them easily*/
18.
19.       if(input[i]>0)
20.       {
21.          inputamount++; /*counting how many values are input*/
22.       }
23.    }
24.
25.    P1H = input;
26.    /*printf("Player 1 has %d cards\n", P1H);*/
27.    if(P1H < 2 || P1H > 10)
28.    {
29.       E1 = 1;
30.       error++; /*so we can later display all errors without displaying the win/lose scenario*/
31.    }
32.
33.    for(int f = 0; f <= (P1H-1); f++) /*put the card values into the individual players array*/
34.    {
35.       P1C[f] = input[(f+1)]; /*plus 1 to input to ignore player 1's hand size value*/
36.       if(P1C[f] < 1 || P1C[f] > 10)
37.       {
38.          E3 = 1;
39.          error++;
40.       }
41.    }
42.
43.    for(int out = 0; out < 9; out++)
44.    {
45.       highest = out;
46.
47.       for(in = (out+1); in < 10; in++)
48.       {
49.          if (P1C[in] > P1C[highest])
50.          {
51.             highest = in;
52.          }
53.       }
54.
55.       tmpSwapValue = P1C[out];
56.       P1C[out] = P1C[highest];
57.       P1C[highest] = tmpSwapValue;
58.    }
59.
60.    /*for (int j = 0; j <= 9; j++)
61.    {
62.       if(P1C[j]>0)
63.       printf( "%d ", P1C[j] );
64.    }*/
65.
66.
67.
68.
69.    P2H = input[P1H+1];
70.    /*printf("\nPlayer 2 has %d cards\n", P2H);*/
71.    if(P2H < 2 || P2H > 10)
72.    {
73.       E2 = 1;
74.       error++;
75.    }
76.
77.
78.    for(int h = 0; h <= (P2H-1); h++) /*put the card values into the individual players array*/
79.    {
80.       P2C[h] = input[h+P1H+2]; /*plus the length of player 2's hand, plus 1 for each hand length value*/
81.       if(P2C[h] < 1 || P2C[h] > 10)
82.       {
83.          E4 = 1;
84.          error++;
85.       }
86.    }
87.
88.    for(int out = 0; out < 9; out++)
89.    {
90.       highest = out;
91.
92.       for(in = (out+1); in < 10; in++)
93.       {
94.          if (P2C[in] > P2C[highest] )
95.          {
96.             highest = in;
97.          }
98.       }
99.
100.       tmpSwapValue = P2C[out];
101.       P2C[out] = P2C[highest];
102.       P2C[highest] = tmpSwapValue;
103.    }
104.
105.    /*for (int g = 0; g <= 9; g++ )
106.    {
107.       if(P2C[g]>0)
108.       printf( "%d ", P2C[g] );
109.    }*/
110.
111.
112.    if(P1H > P2H)
113.       PSH = P1H; /*smallest hand size, used to determine whether the game should keep going after a draw*/
114.    else
115.       PSH = P2H;
116.
117.    if((P1H+P2H+2) != inputamount) /*check if the amount of numbers input is the same as what the players think they've typed in*/
118.    {
119.       error++; /*if not, throw up an error*/
120.       E5 = 1;
121.    }
122.
123.    if(error == 0) /*doesn't matter how many errors, 1 or more will prevent this from running*/
124.    {
125.        for(int x = 0; x <= 9; x++)
126.        {
127.           if(P1C[x] > P2C[x])
128.           {
129.              printf("Player 1 wins!\n");
130.              break; /*no need to continue comparing cards*/
131.           }
132.
133.           if(P1C[x] < P2C[x])
134.           {
135.              printf("Player 2 wins!\n");
136.              break; /*no need to continue comparing cards*/
137.           }
138.
139.           if((P1C[x] == P2C[x])&&(x == (PSH-1))) /*if we draw then keep comparing until we run out of cards*/
140.           {
141.              printf("Players draw!\n");
142.           }
143.        }
144.    }
145.    else
146.    {
147.       printf("\nerror:")
148.       if(E1)
149.       {
150.          if(P1H==0)
151.             printf("\nPlayer 1 needs some cards!");
152.          else
153.             printf("\nPlayer 1: amount of cards per hand must be a number between 2 and 10");
154.       }
155.
156.       if(E2)
157.       {
158.          if(P2H == 0)
159.             printf("\nPlayer 2 needs some cards!");
160.          else
161.             printf("\nPlayer 2: amount of cards per hand must be a number between 2 and 10");
162.       }
163.
164.       if(E3)
165.       {
166.          printf("\nPlayer 1: each card must be a number between 1 and 10");
167.       }
168.
169.       if(E4)
170.       {
171.          printf("\nPlayer 2: each card must be a number between 1 and 10");
172.       }
173.
174.       if(E5)
175.       {
176.          printf("\nYou may have input an incorrect number of cards, or one of your players hand sizes is wrong!");
177.          if(P1H != 0 && P2H != 0)
178.          {
179.              if((P1H+P2H)>=4)
180.                 printf("\namount of cards drawn is %d, but ", (P1H+P2H));
181.              else
182.                 printf("\nThere isn't enough cards to play");
183.              if(inputamount>2)
184.                 printf("\namount of cards input is %d ", (inputamount-2));
185.              else
186.                 printf("\nno valid card numbers have been input");
187.          }
188.        }
189.    }
190. }
191.
192.
193. /*START
194.
195. how many cards does player 1 have?
196.    IF (amount is less than 2 or greater than 10)
197.       print error message, "amount of cards per hand must be between 2 and 10"
198.    ELSE
199.       store as P1H
200.
201.    what is the value of P1 card X? X is between first card and final card determined by value P1H)
202.    set value X to 0
203.    WHILE X < P1H where P1H may need to be subtracted by 1 to match the array which starts at 0 instead of 1
204.       IF (amount is less than 1 or greater than 10)
205.          print error message, "values of each card must be between 1 and 10"
206.       ELSE
207.          store as P1CX use array, P1C[X]
208.          increase X by 1
209.       END IF
210.    END WHILE
211.
212.    how many cards does player 2 have?
213.    IF (amount is less than 2 or greater than 10)
214.       print error message, "amount of cards per hand must be between 2 and 10"
215.    ELSE
216.       store as P2H
217.
218.    what is the value of P2 card X? X is between first card and final card determined by value P2H)
219.    set value X back to 0
220.    WHILE X < P2H P2H may need to be subtracted by 1 to match the array which starts at 0 instead of 1
221.       IF (amount is less than 1 or greater than 10)
222.          print error message, "values of each card must be between 1 and 10"
223.       ELSE
224.          store as P2CX use array, P2C[X]
225.          increase X by 1
226.       END IF
227.    END WHILE
228.
229.    IF(P1H < P2H)
230.       store P1H as PSH player with the smallest hand size, use for determining the maximum amount of times we are able to compare cards
231.    ELSE
232.       store P2H as PSH doesn't matter if P1H is greater or if theyre equal
233.    END IF
234.
235.
236.    get player 1 array
237.    set X to zero
238.    WHILE X is one less than the length of player 1 array
239.       look in all positions higher than the current position to find the highest number
240.       swap the highest number and the number at the current position
241.       increase X by 1
242.    END WHILE
243.
244.   get player 2 array
245.    set X to zero
246.    WHILE X is one less than the length of player 2 array
247.       look in all positions higher than the current position to find the highest number
248.       swap the highest number and the number at the current position
249.       increase X by 1
250.    END WHILE
251.
252.
253.    set X to zero
254.    compare player 1 card X to player 2 card X where X starts at 0 and increments until the smallest hand size PSH is reached
255.    WHILE X < PSH minus 1 to match array length
256.       IF(P1CX > P2CX) written as P1C[X] so we can step through the array
257.          print "player 1 wins"
258.          end game no longer need to loop, regardless of whether there are cards left to compare
259.
260.       IF(P1CX < P2CX)
261.          print "player 2 wins"
262.          end game no longer need to loop, regardless of whether there are cards left to compare
263.
264.       IF(P1CX == P2CX)
265.          increment X
266.          IF X == PSH minus 1
267.             IF P1H > PSH
268.                print "player 1 wins"
269.                end game we have compared all our cards and it was a draw every time. player 1 has the most cards so wins by default
270.             IF P2H > PSH
271.                print "player 2 wins"
272.                end game we have compared all our cards and it was a draw every time. player 2 has the most cards so wins by default
273.             ELSE
274.                print "players draw"
275.                end game we have compared all our cards and it was a draw every time
276.          ELSE
277.             loop again
278.          END IF
279.       END IF
280.    END WHILE
281.
282. END
283. */