<mosaic.cnfolio.com>
PROGRAM
  1. //pseudo code//
  2. // start //
  3. // input player 1 hand size //
  4. // check hand size is between 2 and 10  using if statemtets if not in size print error message //
  5. // input player 1s cards //
  6. // check cards are between 1 and 10 using if satements if not in size print error message
  7. // input player 2 hand size //
  8. // check hand size is between 2 and 10 using if statemnts if not in size print error message  //
  9. // input player 2's cards //
  10. // check cards are between 1 and 10 unsing if statments if not in size print error message //
  11. // sort player 1's hand in decending order using quick sort //
  12. // sort player 2's hand in decending order using quick sort //
  13. // compare cards in hand and print win or draw message //
  14.  
  15.  
  16. #include <stdio.h>
  17.  // this subroutine is the quick sort//
  18.    void quicksort(int number[11],int first,int last)
  19.    {
  20.    int i, j, pivot, temp;
  21.  
  22.    if(first<last)
  23.     {
  24.       pivot=first;
  25.       i=first;
  26.       j=last;
  27.  
  28.       while(i<j){
  29.          while(number[i]<=number[pivot]&&i<last)
  30.             i++;
  31.          while(number[j]>number[pivot])
  32.             j--;
  33.          if(i<j){
  34.             temp=number[i];
  35.             number[i]=number[j];
  36.             number[j]=temp;
  37.          }
  38.       }
  39.  
  40.       temp=number[pivot];
  41.       number[pivot]=number[j];
  42.       number[j]=temp;
  43.       quicksort(number,first,j-1);
  44.       quicksort(number,j+1,last);
  45.  
  46.    }
  47. }
  48.  
  49. int main()
  50. {
  51.  int plr1crdnm,plr2crdnm,i,j,k,l,m,n,Temp,temp,outside, inside, lowest, tmpSwapValue;
  52.     int PLR1CRDS [11]={0};
  53.     int PLR2CRDS [11]={0};
  54.      // these two lines are used to ask for and recieve the number of cards in player 1's hand//
  55.     scanf("%d",&plr1crdnm);
  56.  
  57.     // this line is used to check if the hand is bigger than 1 but smaller then 11//
  58.     if (plr1crdnm<=10 && plr1crdnm>=2)
  59.     {
  60.         // these three lines of code recieve player 1's set of cards and stores them in the array PLR1CRDS//
  61.  
  62.     for(i=0;i<plr1crdnm;i++)
  63.       scanf("%d",&PLR1CRDS[i]);
  64.      // this for loop is to check that all the cards in the hand fall between 1 and 10 if they don't the error message is displayed//
  65.     for(i=0;i<plr1crdnm;i++)
  66.         {
  67.  
  68.  
  69.      
  70.       if (PLR1CRDS[i]<1 || PLR1CRDS[i]>10)
  71.         {
  72.             printf("\ncards in players hands must be between 1 and 10 only, please try again\n");
  73.             return(0);
  74.         }
  75.         else
  76.         k=plr1crdnm; // k is used in the checking of who wins as it is easier to use//
  77.         }
  78.  
  79.  
  80.     scanf("%d",&plr2crdnm);
  81.  
  82.      // this line of code checks if the hand is between 2 and 10 in size//
  83.     if (plr2crdnm<=10 && plr2crdnm>=2)
  84.     {
  85.     // this set of code asks for the cards and put them in the array PLR2CRDS//
  86.     for(j=0;j<plr2crdnm;j++)
  87.       scanf("%d",&PLR2CRDS[j]);
  88.  
  89.      // this block of code checks that all the cards in the hand are between 1 and 10 and if they aren't an error message is displayed //
  90.     for(j=0;j<plr2crdnm;j++)
  91.     {
  92.       if (PLR2CRDS[j]<1 || PLR2CRDS[j]>10)
  93.         {
  94.             printf("\ncards in players hands must be between 1 and 10 only, please try again\n");
  95.             return(0);
  96.         }
  97.         else
  98.         l=plr2crdnm;
  99.     }
  100.     //this calls the quick sort sub routine//
  101.     quicksort(PLR1CRDS,0,plr1crdnm-1);
  102.     // this set of code flips the array after it comes back form the quick sort to make it highest to lowest rather than lowest to highest//
  103.    for (i = 0; i < plr1crdnm+1; i++)
  104.  
  105.  
  106.    j = i - 1;
  107.    i = 0;
  108.  
  109.    while (i < j)
  110.    {
  111.       Temp = PLR1CRDS[i];   // the resorting code swaps the numbers in the array working from outside to in//
  112.       PLR1CRDS[i] = PLR1CRDS[j]; // it does this until only a middle number is left or all numbers are swapped//
  113.       PLR1CRDS[j] = Temp;
  114.       i++;
  115.       j--;
  116.    }
  117.    // this set of code uses a for loop to display player 1's sorted hand from the array PLR1CRDS//
  118.    printf("\nplayer 1's sorted hand: \n");
  119.    for (i = 0; i < plr1crdnm; i++)
  120.    {
  121.       printf("%d \t", PLR1CRDS[i]);
  122.    }
  123.  
  124.        printf(" \n"); // this is used to put the output on a new line so the output looks neater//
  125.  
  126.      // this line calls the subroutine quick sort and places the sorted list into the array PLR2CRDS//
  127.     quicksort(PLR2CRDS,0,plr2crdnm-1);
  128.  
  129.    // this set of code reverses the array from lowest to highest to being highest to lowest//
  130.    for (i = 0; i < plr2crdnm+1; i++)
  131.  
  132.  
  133.    j = i - 1;
  134.    i = 0;
  135.  
  136.    while (i < j)
  137.    {
  138.       Temp = PLR2CRDS[i];//the resorting code swaps the numbers in the array working from outside to in//
  139.       PLR2CRDS[i] = PLR2CRDS[j];//it does this until only a middle number is left or all numbers are swapped//
  140.       PLR2CRDS[j] = Temp;
  141.       i++;
  142.       j--;
  143.    }
  144.    // prints player twos sorted hand from the array PLR2CRDS//
  145.    printf("\nplayer 2's sorted hand: \n");
  146.    for (i = 0; i < plr2crdnm; i++)
  147.    {
  148.       printf("%d \t", PLR2CRDS[i]);
  149.    }
  150.  
  151.  
  152.  
  153.        printf(" \n");// this is for output neatness//
  154.       m=0;
  155.       n=0;
  156.  
  157. label: // this is used to loop through the pairs of cards that need to be checked//
  158.      if (PLR1CRDS[m]>PLR2CRDS[n])
  159.      {
  160.          printf("\nplayer 1 wins  \n");
  161.          return (0);
  162.  
  163.      }
  164.  
  165.         else if (PLR1CRDS[m]<PLR2CRDS[n])
  166.         {
  167.             printf("\nplayer 2 wins  \n");
  168.             return (0);
  169.         }
  170.        else if(PLR1CRDS[m]==PLR2CRDS[n])
  171.        {
  172. // this if statement checks if both players have used all there cards and they have all been equal and if they draw is shown//
  173.            if (m==l && n==k)
  174.            {
  175.                printf ("\ndraw \n");
  176.                return (0);
  177.            }
  178.            else
  179. //if none of the if statements so far have been met m and n are incrimented and the new numbers are checked by the if statements//
  180.             m++;
  181.             n++;
  182.         goto label; //this is the jump statement//
  183.        }
  184.  
  185.       return (0);
  186.     }
  187.     else if (plr2crdnm>10)  // this if statement is used if player 2's hand is above 10//
  188.     {
  189.         printf("\nplayer 2's hand is too big to play, please try again \n");
  190.         return (0);
  191.     }
  192.     else if (plr2crdnm<2) // this if statement is used if player 2's hand is below 2//
  193.     {
  194.         printf("\nplayer 2's hand is too small to play, please try again \n");
  195.         return (0);
  196.     }
  197.     }
  198.      else if (plr1crdnm>10) // this if statement is used if player 1's hand is above 10//
  199.      {
  200.          printf("\nplayer 1's hand is too big to play, please try again \n");
  201.          return (0);
  202.      }
  203.      else if (plr1crdnm<2) // this is statement is used if player 1's hand is below 2//
  204.      {
  205.          printf("\nplayer 1's hand is too small to play, please try again \n");
  206.      }
  207. }