<mosaic.cnfolio.com>
PROGRAM
1. //                    CourseWork 1 (Final Work)
2.
3. //             Introduction to Algorithms and Programming
4.
5. /*
6.                            >>pseudo code<<
7.
8. >>  once the game is turned on it will show 'Card Game' in a display.
9.                                    and
10.
11. >>  In 1st line, number of card that player 1 has should be enter.
12.             NUMBER OF CARD MUST BE IN BETWEEN 2 TO 10
13.
14.
15. >>  In 2nd line, once the number of card is entered, enter the cards that player 1 has.
16.             cards must be in between 1 to 10 and must be seperated by space
17.
18.
19. >>  In 3rd line, number of card that player 2 has should be enter.
20.             NUMBER OF CARD MUST BE IN BETWEEN 2 TO 10
21.
22.
23. >>  In 4th line, once the number of card is entered, enter the cards that player 2 has.
24.             CARD MUST BE IN BETWEEN 1 TO 10 AND MUST BE SEPERATED BY SPACE
25.
26.
27. >>  all the cards of each player will be sort from highest number to lowest number.
28.
29.
30. >>  if the number of card is entered as less than 2 or more than 10, the screen will show.
31.             'YOU CAN ONLY HAVE 2 TO 10 CARDS'
32.
33.
34. >>  once the input data from both player is entered and program is run, the system will show the output.
35.     player with higher card will win.
36.
37.
38. >>  if both player has a same high card, it will cancel out and the one with second higher card will
39.     win.and if the second card is also same, the system will run until the higher card are found and
40.     the one with higher card will win.
41.
42.
43. >>  if the both player has a same number of card and have the same cards with them, the system will
44.     show it as a draw.
45.
46.
47. >>  if the input numbers are wrong the program will display informative error messages.
48.
49. */
50.
51. #include <stdio.h>
52.
53. #define MAX_SIZE 10
54.
55. //Maximun number of cards
56.
57. int main(void)
58.
59. {
60.
61.       int player1[MAX_SIZE], player2[MAX_SIZE]; //Array of Number of cards
62.       int i,j,x,y,z,trash,decide;
63.
64.       printf("************Card Game************");
65.
66.       //Taking input Player1 Number of Cards
67.       printf("\nPlayer1 Enter the number of Card");
68.       scanf("%d",&x);
69.
70.       //Applying condition players must have 2 to 10 cards
71.       if(x<2 || x>10){
72.          printf("\n\nPlayer 1 you entered %d, \nYou can only have 2 to 10 Cards",x);
73.          return(0);
74.          scanf("%d",&x);
75.       }
76.
77.       //Taking input Player1 Cards
78.       printf("\nPlayer1 Enter yours Cards");
79.
80.       for(i=0; i<x; i++)
81.       {
82.          scanf("%d",&player1[i]);
83.
84.          //Only Numbered Cards 1 to 10 must be used
85.          if(player1[i]<1 || player1[i]>10){
86.             printf("\nCards must be in between 1 to 10");
87.             scanf("%d",&player1[i]);
88.          }
89.       }
90.
91.
92.
93.
94.       //Taking input Player2 Number of Cards
95.       printf("\nPlayer2 Enter the number of Card");
96.       scanf("%d",&y);
97.       if(y<2 || y>10){
98.          printf("\n\nPlayer 2 you entered %d, You can only have 2 to 10 Cards",y);
99.          scanf("%d",&y);
100.       }
101.
102.       //Taking input Player2 Cards
103.       printf("\nPlayer2 Enter yours Cards");
104.
105.       for(i=0; i<y; i++)
106.       {
107.          scanf("%d",&player2[i]);
108.          if(player2[i]<1 || player2[i]>10){
109.             printf("\nCards must be in between 1 to 10");
110.             scanf("%d",&player2[i]);
111.          }
112.       }
113.
114.
115.
116.        //Sorting the Player 1 Cards from highest to lowest
117.       for(i=0; i<x; i++)
118.       {
119.
120.          for(j=i+1; j<x; j++){
121.
122.             if(player1[i] < player1[j]){
123.
124.                trash = player1[i];
125.                player1[i] = player1[j];
126.                player1[j]=trash;
127.
128.             }
129.          }
130.
131.       }
132.
133.
134.
135.       //Sorting the Player 2 Cards from highest to lowest
136.       for(i=0; i<y; i++)
137.       {
138.
139.          for(j=i+1; j<y; j++){
140.
141.             if(player2[i] < player2[j]){
142.
143.                trash = player2[i];
144.                player2[i] = player2[j];
145.                player2[j]=trash;
146.
147.             }
148.          }
149.
150.       }
151.
152.
153.       //Printing player1 Sorted Cards
154.
155.       printf("\n\nPlayer1 has %d cards: ",x);
156.       for(i=0; i<x; i++)
157.       {
158.          printf("%d ",player1[i]);
159.       }
160.
161.       //Printing Player2 Sorted Cards
162.
163.       printf("\n\nPlayer2 has %d cards: ",y);
164.       for(i=0; i<y; i++)
165.       {
166.          printf("%d ",player2[i]);
167.       }
168.
169.
170.       //Checking highest number of cards between players
171.
172.       if(x>y){
173.
174.          z = x;
175.       }
176.       else if(y>x){
177.          z = y;
178.       }
179.       else{
180.          z = x;
181.       }
182.
183.       for(i=MAX_SIZE; i>=x; i--){
184.          player1[i]=0; //Filling rest array of cards 0 to make easy comparison
185.      }
186.
187.      for(i=MAX_SIZE; i>=y; i--){
188.         player2[i]=0;
189.       }
190.
191.
192.       // Deciding the winner
193.
194.       for(i=0; i<z; i++)
195.       {
196.          if(player1[i] > player2[i])
197.             {
198.                decide = 1; //decide 1 player1 win
199.                break;
200.             }
201.
202.          else if(player1[i] < player2[i])
203.             {
204.                decide = 2; //decide 2 player1 win
205.                break;
206.             }
207.
208.       }
209.
210.       printf("\n\n************************");
211.       if(decide == 1){
212.          printf("\n*      Player1 win     *");
213.       }
214.
215.       else if(decide == 2){
216.          printf("\n*      Player2 win     *");
217.       }
218.
219.       else{
220.          printf("\n*         Draw         *");
221.       }
222.
223.       printf("\n************************");
224.
225. }