<mosaic.cnfolio.com>
PROGRAM
1. /*Start*/
2.
3.         /*sorting Arrays with for loops*/
4.
5.
6.         /*Sorting the number of cards for CardA and CardB*/
7.
8.         /*Declaer varibles*/
9.
10.
11.         /*Get the user input*/
12.         /*while the user input is less the then2 and more 10, using for loop in relation to the first input*/
13.
14.         /*Sorting numbers in ascending order*/
15.
16.
17.         /*calculte the winner*/
18.         /*using while loop and if statement to show if one card is greater then the other*/
19.
20.            /*if so
21.            set the winner*/
22.
23.
24.         /*Decrement the card arrys*/
25.
26.           /*if both players run out of cards set winner to 0 */
27.
28.           /* else if
29.              CardA have not run out of cards set the winner to CardA
30.
31.              esls
32.              set the winner to CardB*/
33.
34.              /* if wineer=CardA
35.                 than announcent player1 to be the winner
36.
37.                 if winner=CardB
38.                 then announce player2 to be the winner
39.
40.                 else
41.                 if neither of them wins
42.                 the announce Drew
43.
44.
45. /*END*/
46.
47.
48.
49. #include <stdio.h>
50.
51.
52. //Sort function
53. void sortArray(int cards[],int n)
54. {
55.     int i,j,temp;
56.     for(i=0;i<n;i++){
57.         for(j=0;j<n-i-1;j++){
58.             if(cards[j] > cards[j+1]){
59.                 temp = cards[j];
60.                 cards[j] = cards[j+1];
61.                 cards[j+1] = temp;
62.             }
63.         }
64.     }
65. }
66.
67.
68. int gameBegins(int cardsA[], int numOfcardsA, int cardsB[], int numOfcardsB)
69.
70. {
71.
72. numOfcardsA--;
73. numOfcardsB--;
74.   while(numOfcardsA>=0&&numOfcardsB>=0){
75.    if(cardsA[numOfcardsA]!=cardsB[numOfcardsB]){
76.     if(cardsA[numOfcardsA]>cardsB[numOfcardsB]){
77.       return 1;
78.      }
79.
80.      else{
81.       return 2;
82.       }
83.      }
84.
85. numOfcardsA--;
86. numOfcardsB--;
87.
88.  }
89.     if(numOfcardsA<0&& numOfcardsB<0){
90.        return 0;
91.    }
92.
93.     else if(numOfcardsB>=0){
94.          return 2;
95.   }
96.
97.     else {
98.     return 1;
99.  }
100.   }
101.
102. int main()
103. {
104. //declare vairables
105.      int numOfcardsA,numOfcardsB;
106.      int cardsA[10];
107.      int cardsB[10];
108.      int i;
109.      int winner;
110.
111.        printf("Welcome to Ameers War Gmae\n AND THE WINNER IS:");
112.
113.
114. //Get the user input
115.    while(1){
116.       scanf("%d\n",&numOfcardsA);
117.       if(numOfcardsA>10);
118.       else(numOfcardsA<2);{
119.         printf("\n");
120.
121.  }
122.
123.       for(i=0;i<numOfcardsA;i++){
124.          scanf("%d",&cardsA[i]);
125.  }
126.
127.     while(1){
128.
129.      scanf("%d\n",&numOfcardsB);
130.      if(numOfcardsB>10);
131.      else(numOfcardsB<2);{
132.         printf("\n");
133.  }
134.
135.      for(i=0;i<numOfcardsB;i++){
136.         scanf("%d",&cardsB[i]);
137. }
138.
139. //Sort the cards in ascending order
140.
141.       sortArray(cardsA,numOfcardsA);
142.       sortArray(cardsB,numOfcardsB);
143.       winner =gameBegins(cardsA,numOfcardsA,cardsB,numOfcardsB);
144.
145.
146. //calculate the Winner
147.
148.     if(winner==1){
149.      printf("Player1 wins\n");
150.    }
151.     else if(winner==2){
152.      printf("player2 wins\n");
153.   }
154.     else {
155.      printf("Draw\n");
156.
157.      }
158.
159.      return 0;
160.      }
161.      }
162.      }
163.
164.
165. //References:
166. //https:www.w3resource.com/c-programming-exercises/array/c-array-exercise-11.php