<mosaic.cnfolio.com>
PROGRAM
  1. //##################################################
  2. //                   PSUEDO CODE
  3. //##################################################
  4.  
  5. /*
  6. Start
  7.    
  8.    There are two players in the game.
  9.    Players may have from 2 to 10 cards on their hands.
  10.    All cards may have a different quantities, from 1 to 10 AND should be sorts, starting the highest card first.
  11.    
  12.   WHILE card in hands.
  13.       
  14.       Take one highest card from each player.
  15.      
  16.       IF Player#1 has a bigger card then Player#2 has
  17.          
  18.         THEN 
  19.         Player#1 Win!
  20.          
  21.         ELSE IF Player#2 has a bigger card then Player#1 has
  22.             Player#2 Win!
  23.        
  24.         ELSE
  25.           Do WHILE again.
  26.       END IF
  27.          
  28.    END WHILE
  29.  
  30.    IF Player#1 has/ve card/s and Player#2 don't
  31.       THEN
  32.          Player#1 Win!
  33.      
  34.          ELSE IF Player#2 has/ve card/s and Player#1 don't
  35.              Player#2 Win!
  36.             
  37.          ELSE
  38.            Draw
  39.   END IF
  40.  
  41. END 
  42.  
  43. */
  44.  
  45. //##################################################
  46. //  COURSWORK 1 - A CARD GAME
  47. //##################################################
  48.  
  49. // Card Game::..
  50. // Library::..
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. // Logic::..
  55. // Main function of sorting action.
  56. //---------------------------------------------
  57. static int cmp(const void *p1, const void *p2){
  58.   return *(int*)p1 > *(int*)p2;
  59. }
  60. //---------------------------------------------
  61. int main (void){
  62.  
  63.   // DATA::..
  64.   int CardDeck01[10], CardDeck02[10], cardsINdeck1, cardsINdeck2, usit, c, o, i, j, n, m = 0;
  65.   int equal = 0;
  66.   char letter ;
  67.  
  68.   // Action::..
  69. //##################################################
  70. //                INTRODUCTION
  71. //##################################################
  72.  
  73.   printf("Welcome,\nThis is a card game with two players.\nYou will have to enter an input for each deck of cards for each player.\nAnd also, values of cards that those players will have.\n \n1) Enter two numbers of cards in decks from 2 to 10, for the first and second players.\n2) Also, please enter values for each card in deck for bouth players, from 1 to 10 and in this case, values cannot be repeated.\n\nExample:\n  2 3        (where 2 is cards in deck for first player and 3 is for second player)\n  6 7        (where 6 and 7 are values of those 2 cards in deck for first player)\n  6 7 8      (where 6, 7 and 8 are values of those 3 cards in deck for second player)\n\nNow, input your data, please.\n");
  74.  
  75.   scanf("%d %d", &cardsINdeck1, &cardsINdeck2);
  76.  
  77. //##################################################
  78. //  MOST IMPORTANT PART FOR TO INTERNALIZE ERRORS
  79. //##################################################
  80.  
  81.   if (2 > cardsINdeck1 ||cardsINdeck1 > 10 || cardsINdeck1 == letter || 2 > cardsINdeck2 ||cardsINdeck2 > 10 || cardsINdeck2 == letter ){
  82.     printf("\nError,\n  Reason can be:\n  1.No inputs\n  2.Inputs aren't the numbers\n  3.Numbers are to big/small.\n\nPlease try and run the programm again.");
  83.   }
  84.   else{
  85.     printf("\nYou have %d elements in deck for first player and also, %d elements in second deck for second player.\n\n", cardsINdeck1, cardsINdeck2);
  86.    
  87.     for (c = 0; c < cardsINdeck1; c++){
  88.       scanf("%d", &CardDeck01[c]);
  89.       if (CardDeck01[c] <= 0 || CardDeck01[c] > 10 || CardDeck01[c] == letter){
  90.         printf("Error,\n  Reason can be:\n  1.No inputs for the second part.\n  2.Inputs aren't the numbers.\n  3.Numbers are to big/small.\n\nPlease try and run the programm again.");
  91.         return 0;
  92.       }
  93.     }
  94.    
  95.     for (o = 0; o < cardsINdeck2; o++){
  96.       scanf("%d", &CardDeck02[o]);
  97.       if (CardDeck02[o] <= 0 || CardDeck02[o] > 10 || CardDeck02[o] == letter){
  98.         printf("Error,\n  Reason can be:\n  1.No inputs for the SECOND part.\n  2.Inputs aren't the numbers.\n  3.Numbers are to big/small.\n\nPlease try and run the programm again.");
  99.         return 0;
  100.       }
  101.     }
  102.    
  103. //##################################################
  104. //      SMALL, BUT IMPORTANT - SORTING ACTION
  105. //##################################################
  106.  
  107.     qsort(CardDeck01, cardsINdeck1, sizeof(int), cmp);
  108.     qsort(CardDeck02, cardsINdeck2, sizeof(int), cmp);
  109.    
  110. //##################################################
  111. //    DISTRIBUTION AND CALCULATION OF THE RESULTS
  112. //##################################################
  113.     printf("You have enter:");
  114.    
  115.     for( i=0; i<cardsINdeck1; ++i){
  116.       printf(" %d", CardDeck01[i]);
  117.     }
  118.    
  119.     printf(", for first deck of first player.\nAnd also:");
  120.    
  121.     for( j=0; j<cardsINdeck2; ++j){
  122.       printf(" %d", CardDeck02[j]);
  123.     }
  124.    
  125.     printf(", for second deck of second player.\n\nThe results: ");
  126.    
  127.     if (cardsINdeck2 > cardsINdeck1){
  128.       usit = cardsINdeck2;
  129.     }
  130.     else usit = cardsINdeck1;
  131.    
  132.     for (n = 0; n < usit; n++){
  133.       equal ++;
  134.       if (equal < usit){
  135.         if (CardDeck01[m] > CardDeck02[n]){
  136.           printf("the first player won!\n");
  137.           return 0;
  138.         }
  139.        
  140.         if (CardDeck01[m] < CardDeck02[n]){
  141.           printf("the second player won!\n");
  142.           return 0;
  143.         }
  144.       }
  145.       m++;
  146.       if ( m == usit){
  147.         if (cardsINdeck1 > cardsINdeck2){
  148.           printf("the first player won!\n");
  149.         }
  150.         else if (cardsINdeck2 > cardsINdeck1){
  151.           printf("the second player won!\n");
  152.         }
  153.         else printf("it's draw.");
  154.         return 0;
  155.       }
  156.     }
  157.   }
  158. }
  159.                                  
  160. //##################################################
  161. //                 ..::THE EDN::..
  162. //##################################################