<mosaic.cnfolio.com>
PROGRAM
1. //START
2. //   Two players in a game
3. //   The players have value in their card
4. //   Insert variable loop to obtain selection order
5. //   Determine the number of card that player1 obtain
6. //   IF
7. //      Player1 has less than two card or more than ten card
8. //      The value of card of player1 is out of range between one to ten
9. //      THEN
10. //         Display Error message
11. //   END IF
12. //   WHILE
13. //      Arrange the card of player1 from the biggest to the smallest according to their value
14. //   END WHILE
15. //   Determine the number of card that player2 obtain
16. //   IF
17. //      Player2 has less than two card or more than ten card
18. //      The value of card of player2 is out of range between one to ten
19. //      THEN
20. //         Display Error message
21. //   END IF
22. //   WHILE
23. //      Arrange the card of player2 from the biggest to the smallest according to their value
24. //   END WHILE
25. //   Two players will have leftover cards after each duel
26. //   Insert variable loop when comparing card between two players in duel
27. //   Set the condition of draw initially as not possible
28. //   While
29. //      In the duel players comparing one of their card in one round as a loop
30. //      IF
31. //         The value of cards of both players are the same
32. //         THEN
33. //            Both players lost one card
34. //            IF
35. //               Player1 has zero leftover card
36. //               THEN
37. //                  Player1 is run out of cards
38. //            END IF
39. //            IF
40. //               Player2 has zero leftover card
41. //               THEN
42. //                  Player2 is run out of cards
43. //            END IF
44. //    END IF
45. //      IF
46. //         The value of card of player1 is higher than player2
47. //         THEN
48. //            Player1 wins
49. //      END IF
50. //      IF
51. //         The value of card of player1 is smaller than player2
52. //         THEN
53. //            Player2 wins
54. //      END IF
55. //      IF
56. //         Both player have the same amount of card
57. //         THEN
58. //            The condition of draw is possible
59. //         IF
60. //            Both players run out of cards
61. //            THEN
62. //               The result is draw
63. //         END IF
64. //      END IF
65. //   END WHILE
66. //END
67.
68.
69. #include <stdio.h>
70.
71. int main( void )
72. {
73.    int player1NoC, player2NoC;                  //The number of cards of player1 and player2
74.    int player1[10], player2[10];      //The value of cards of player1 and player2
75.    int x, y, temp;                              //Variable for loop
76.
77.
78.    scanf("%d", &player1NoC);              //Input of number of cards of player1
79.    if(player1NoC < 2 || player1NoC > 10)      //Input is unaccepted when its value is smaller than 2 or greater than 10
80.    {
81.       printf("\nError... Player1 can only have 2 to 10 cards in hand.\n");
82.       return;
83.    }
84.    for(x = 0 ; x < player1NoC ; x++)            //User input loop for the value of cards of player1
85.    {
86.       scanf("%d", &player1[x]);            //Input of the value of cards of player1
87.       if(player1[x] < 1 || player1[x] > 10)    //Input is unaccepted when its value is out of range of 1 to 10
88.       {
89.       printf("\nError... Player1 can only have cards with the value between 1 to 10.\n");
90.       return;
91.       }
92.    }
93.
94.    for(x = 0; x < player1NoC; x++)          //Selection sort to determine the arrangement of cards of player1 in descending order
95.    {
96.       for(y = x + 1; y < player1NoC; y++)
97.       {
98.          if(player1[x] < player1[y])
99.          {
100.          temp = player1[x];
101.          player1[x] = player1[y];
102.          player1[y] = temp;
103.          }
104.       }
105.    }
106.
107.
108.    scanf("%d", &player2NoC);              //Input of number of cards of player2
109.    if(player2NoC < 2 || player2NoC > 10)      //Input is unaccepted when its value is smaller than 2 or greater than 10
110.    {
111.       printf("\nError... Player2 can only have 2 to 10 cards in hand.\n");
112.       return;
113.    }
114.    for(x = 0 ; x < player2NoC ; x++)            //User input loop for the value of cards of player2
115.    {
116.       scanf("%d", &player2[x]);            //Input of the value of cards of player2
117.       if(player2[x] < 1 || player2[x] > 10)    //Input is unaccepted when its value is out of range of 1 to 10
118.       {
119.       printf("\nError... Player2 can only have cards with the value between 1 to 10.\n");
120.       return;
121.       }
122.    }
123.
124.    for(x = 0; x < player2NoC; x++)          //Selection sort to determine the arrangement of cards of player2 in descending order
125.    {
126.       for(y = x + 1; y < player2NoC; y++)
127.       {
128.          if(player2[x] < player2[y])
129.          {
130.          temp = player2[x];
131.          player2[x] = player2[y];
132.          player2[y] = temp;
133.          }
134.       }
135.    }
136.
137.    int remainedcard1, remainedcard2;            //Varaible of number of cards for player1 and player2 after each comparing
138.    int duel;                                    //Variable for loop when comparing the cards between player1 and player2
139.    int EndofCards = 0;                  //Variable for draw
140.
141.    remainedcard1 = player1NoC;
142.    remainedcard2 = player2NoC;
143.
144.    for(duel = 0; duel < 10; duel++)     //Looping after each comparing
145.    {
146.       if(player1[duel] == player2[duel])      //Move to the next round if the value of card of player1 and player2 are the same
147.       {
148.          remainedcard1 = remainedcard1 - 1;
149.          remainedcard2 = remainedcard2 - 1;
150.          if(remainedcard1 == 0)
151.             player1[duel] = 0;                  //player1 run out of card
152.          if(remainedcard2 == 0)
153.             player2[duel] = 0;                  //player2 run out of card
154.       }
155.
156.       if(player1[duel] > player2[duel])         //Show output of player1 winning since player1 has higher value of card than player2
157.       {
158.          printf("\nPlayer 1 wins\n");
159.          return;
160.       }
161.
162.       if(player1[duel] < player2[duel])         //Show output of player2 winning since player1 has lower value of card than player2
163.       {
164.          printf("\nPlayer 2 wins\n");
165.          return;
166.       }
167.
168.       if(player2[duel] == player1[duel] && player1NoC == player2NoC)  //When player1 and player2 have same number of card and same value of card
169.       {
170.          EndofCards++;
171.          if(player1NoC == EndofCards || player2NoC == EndofCards)   //When player1 and player2 run out of cards
172.          {
173.             printf("\nDraw\n");                            //Show output of draw
174.             return;
175.          }
176.       }
177.    }
178.
179. }