<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. -Starting off by assigning P1 with 2-10 cards
  3. -then we assign P2 with 2-10 cards
  4. -The next section with where we put a condtion on P1 where the value of the card has to be between 1-10
  5. -And the exact same for P2
  6. -The next section sorts the players cards from higest to lowest
  7. -sort if player one player is out of cards, while other player have more in hands
  8. -display player who have more cards wins
  9. -The next is a function where If P1's card is higher than P2, then it would display "P1 WINS"
  10. -                             Or if P2's card is higher than P1, then it would display "P2 WINS"
  11. -                             Or if P1's card is equal to P2's card then it would display "DRAW"
  12. */
  13. #include <stdio.h>
  14. #define MAX_SIZE 10
  15. //Maximun number of cards
  16. int main(void)
  17. {
  18.      
  19. int P1[MAX_SIZE], P2[MAX_SIZE];
  20. //Array of Number of cards
  21. int J,Q,K,A,B,bin,decide;               
  22. printf("Welcome to the card game");
  23. //Collecting the input of P1
  24. printf("\nPlayer1 Enter the number of Card");
  25. scanf("%d",&K);
  26. //This applies a condition that only the values from 2 to 10 are available
  27. if(K<2 || K>10){
  28. printf("\n\nP1 Number of Cards you want",K);
  29. return(0);
  30. scanf("%d",&K);
  31. }
  32. //Collecting the input value of P1
  33. printf("\nP1 Please enter the values of your cards");
  34. for(J=0; J<K; J++)
  35. {
  36. scanf("%d",&P1[J]);
  37. //This is a contidion where only the values from 1 to 10 are available
  38. if(P1[J]<1 || P1[J]>10){
  39. printf("\Sorry the only cards from the value 1 to 10 are available");
  40. scanf("%d",&P1[J]);
  41. }
  42. }
  43. // This is going to be identical to the coding for P1, just that the declared intergers are changed
  44. //This is a condition where the Player cannot put any other number than 2 to 10
  45. printf("\nP2 Enter the number of Card");
  46. scanf("%d",&A);
  47. if(A<2 || A>10){
  48. printf("\n\nP2 you entered %d, You can only have 2 to 10 Cards",A);
  49. scanf("%d",&A);
  50. }
  51. //Taking input P2 Cards
  52. printf("\nP2 Please enter the values of your cards");
  53. for(J=0; J<A; J++)
  54. {
  55. scanf("%d",&P2[J]);
  56. if(P2[J]<1 || P2[J]>10){
  57. printf("\nCards must be in between 1 to 10");
  58. scanf("%d",&P2[J]);
  59. }
  60. }
  61. //Sorting the Player 1 Cards from highest to lowest
  62. for(J=0; J<K; J++)
  63. {
  64. for(Q=J+1; Q<K; Q++){
  65. if(P1[J] < P1[Q]){
  66. bin = P1[J];
  67. P1[J] = P1[Q];
  68. P1[Q]=bin;
  69. }
  70. }     
  71. }
  72. //this is the sorting of the cards from higest to lowest
  73. for(J=0; J<A; J++)
  74. {
  75. for(Q=J+1; Q<A; Q++){
  76. if(P2[J] < P2[Q]){
  77. bin = P2[J];
  78. P2[J] = P2[Q];
  79. P2[Q]=bin;
  80. }
  81. }
  82.  
  83. }
  84. //Printing P1 sorted cards
  85. printf("\n\nP1 has %d cards: ",K);
  86. for(J=0; J<K; J++)
  87. {
  88. printf("%d ",P1[J]);
  89. }
  90. //Printing P2 Sorted Cards
  91. printf("\n\nP2 has %d cards: ",A);
  92. for(J=0; J<A; J++)
  93. {
  94. printf("%d ",P2[J]);
  95. }
  96. //Analyzing who has the highest value cards of the players
  97. if(K>A){
  98. B = K;
  99. }
  100. else if(A>K){
  101. B = A;
  102. }
  103. else{
  104. B = K;
  105. }
  106. for(J=MAX_SIZE; J>=K; J--){
  107. P1[J]=0;
  108. }
  109. for(J=MAX_SIZE; J>=A; J--){
  110. P2[J]=0;
  111. }
  112. // Deciding who the winner is
  113. for(J=0; J<B; J++)
  114. {
  115. if(P1[J] > P2[J])
  116. {
  117. decide = 1; //decide 1 P1 win             
  118. break;
  119. }
  120. else if(P1[J] < P2[J])
  121. {
  122. decide = 2; //decide 2 P1 win
  123. break;
  124. }
  125. }
  126. if(decide == 1){
  127. printf("\n*P1 WINS*");
  128. }
  129. else if(decide == 2){
  130. printf("\n*P2 WINS*");
  131. }
  132. else{
  133. printf("\n*DRAW*");
  134. }
  135. printf("\n****PLAY AGAIN****");
  136. }