<mosaic.cnfolio.com>
PROGRAM
  1. /* PSEUDOCODE
  2. START
  3.         Read cardsp1
  4.         Read cardsp2
  5.         Read x
  6.         Read p1array =[11}
  7.         Read p2array ={11}
  8.         Read i
  9.  
  10.         SCANF cardsp1
  11.             IF cardsp1<2
  12.                 PRINTF "Please enter value 2 or more"
  13.             ELSE IF cardsp1>10
  14.                 PRINTF "Please enter value less than 10"
  15.         FOR x=0 WHEN x<cardsp1 INCREMENT by 1
  16.             SCANF p1array[x]
  17.         END FOR
  18.         CALL BUBBLESORT (cardsp1, p1array)
  19.  
  20.  
  21.          SCANF cardsp2
  22.             IF cardsp2<2
  23.                 PRINTF "Please enter value 2 or more"
  24.             ELSE IF cardsp2>10
  25.                 PRINTF "Please enter value less than 10"
  26.         FOR x=0 WHEN x<cardsp2 INCREMENT by 1
  27.             SCANF p2array[x]
  28.         END FOR
  29.         CALL BUBBLESORT (cardsp2, p2array)
  30.  
  31.     Win Loop:
  32.         FOR i=0 WHEN i<10 INCREMENT by 1
  33.             IF arrayp1>arrayp2 THEN
  34.                 PRINTF "P1 WINS"
  35.             ELSE IF arrayp2>arrayp1 THEN
  36.                 PRINTF "P2 WINS"
  37.             ELSE IF i+1>cardsp2 AND cardsp1>i+1 THEN
  38.                 PRINTF "P1 WINS"
  39.             ELSE IF i+1>cardsp1 AND cardsp2>i+1 THEN
  40.                 PRINTF "P2 WINS"
  41.             ELSE IF cardsp1==cardsp2=TRUE OR (i+1>cardsp2 AND i+1>cardsp1)=TRUE THEN
  42.                 PRINTF "DRAW"
  43.             ELSE
  44.                 GO TO Win Loop
  45.  
  46.  
  47.  
  48.     FUNCTION BUBBLESORT
  49.         Read arraysize
  50.         Read array
  51.         Read tempswappingvalue
  52.         Read position1
  53.         Read position2
  54.  
  55.         FOR position1=0 WHEN position1<arraysize-1 INCREMENT by 1
  56.             FOR position2=0 WHEN position2<arraysize-position1-1 INCREMENT by 1
  57.                 IF array at position2<array at position2+1 THEN
  58.                     tempswappingvalue=value at position 2 in array
  59.                     value at position 2 = value at position 2+1
  60.                     value at position 2+1 = tempswappingvalue
  61.                 END IF
  62.             END FOR
  63.         END FOR
  64.     END FUNCTION
  65.  
  66. END
  67. */
  68.  
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71.  
  72. void bubblesort(int[], int);
  73. void errormessage1(int);
  74. void errormessage2(int[], int);
  75.  
  76.  
  77. int main()
  78. {
  79.   int x, numplayerone, numplayertwo;
  80.  
  81.   int playeroneCards[11]={0};
  82.   int playertwoCards[11]={0};
  83.  
  84.  
  85.   scanf("%2d", &numplayerone);
  86.   errormessage1(numplayerone);                /*goes to function which reads the input and returns an error string if necessary, if not carries on with code*/
  87.   for (x=0; x<numplayerone; x++)
  88.   {
  89.       scanf("%2d", &playeroneCards[x]);        /*this for loop populates the array by incrementing x to move along to next free space of array*/
  90.   }
  91.   errormessage2(playeroneCards, numplayerone);
  92.  
  93.  
  94.  
  95.   scanf("%2d", &numplayertwo);
  96.   errormessage1(numplayertwo);                  /*same as error message system for p1, but this time runs it for p2*/
  97.   for (x=0; x<numplayertwo; x++)
  98.   {
  99.     scanf("%2d", &playertwoCards[x]);           /*again, same as p1 but in another array*/
  100.   }
  101.  
  102.  
  103.   bubblesort(playeroneCards, numplayerone);     /*this calls the function bubblesort which sorts the array into descending order*/
  104.   bubblesort(playertwoCards, numplayertwo);
  105.  
  106.  
  107. whowins:
  108.     for (int i=0; i<10; i++)
  109.     {
  110.        if (playeroneCards[i]>playertwoCards[i])         /*if card value in array for p1 is larger than the value for p2, p1 wins*/
  111.        {
  112.            printf("Player 1 wins.");
  113.            return 0;
  114.        }
  115.        else if (playeroneCards[i]<playertwoCards[i])    /*if card value in array for p2 is larger than the value for p1, p2 wins*/
  116.        {
  117.            printf("Player 2 wins.");
  118.            return 0;
  119.        }
  120.        else if (i+1>=numplayertwo && i+1<numplayerone)  /*if p2 has less cards than p1, p1 wins*/
  121.        {
  122.            printf("Player 1 wins.");
  123.            return 0;
  124.        }
  125.        else if (i+1<numplayertwo && i+1>=numplayerone)  /*if p1 has less cards than p2, p2 wins*/
  126.        {
  127.            printf("Player 2 wins.");
  128.            return 0;
  129.        }
  130.        else if ((playeroneCards[i]==playertwoCards[i]) || (i+1>numplayertwo && i+1>numplayerone)) /*if p1 and p2 have the same number of cards OR p1's card values are the same as p2, then it is a draw*/
  131.        {
  132.            printf("Draw.");
  133.            return 0;
  134.        }
  135.  
  136.        else
  137.        {
  138.            goto whowins;
  139.        }
  140.     }
  141.   return 0;
  142. }
  143.  
  144. void bubblesort(int cards[], int cardvalue)
  145. {
  146.   int x, y, temp;
  147.  
  148.   for (x=0; x<(cardvalue-1); x++)
  149.   {
  150.     for (y=0; y<(cardvalue-x-1); y++)
  151.     {
  152.       if (cards[y] < cards[y+1])
  153.       {
  154.         temp=cards[y];
  155.         cards[y]=cards[y+1];
  156.         cards[y+1]=temp;
  157.       }
  158.     }
  159.   }
  160. }
  161.  
  162. void errormessage1(int numcards)
  163. {
  164.   if (numcards<2)
  165.   {
  166.       printf("Please enter a value more than 2 but less than 10.");
  167.       return 0;
  168.   }
  169.   else if (numcards>10)
  170.   {
  171.       printf("Please enter a value less than 10 but no less than 2.");
  172.       return 0;
  173.   }
  174. }
  175.  
  176. void errormessage2(int cardarray[], int arraylength)
  177. {
  178.  
  179.   for (int x=0; x<arraylength; x++)
  180.   {
  181.       if (cardarray[x]<1)
  182.       {
  183.           printf("Please enter card values between 1 and 10.");
  184.           return 0;
  185.       }
  186.       else if (cardarray[x]>10)
  187.       {
  188.           printf("Please enter card values between 1 and 10.");
  189.           return 0;
  190.       }
  191.   }
  192. }