<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2.  
  3. /*
  4. START
  5. initialize variables
  6.  
  7. read input for number of cards that player 1 has
  8. IF input is smaller than 2 OR larger than 10
  9.    display error message
  10.    stop program
  11. END IF
  12.  
  13. create array with number of elements equal to number of cards that player 1 has
  14.  
  15. read input for which cards player 1 has
  16. IF input is smaller than 1 OR larger than 10
  17.    display error message
  18.    stop program
  19. END IF
  20. store values in player 1 cards array
  21.  
  22. read input for number of cards that player 2 has
  23. IF input is smaller than 2 OR larger than 10
  24.    display error message
  25.    stop program
  26. END IF
  27.  
  28. create array with number of elements equal to number of cards that player 2 has
  29.  
  30. read input for which cards player 2 has
  31. IF input is smaller than 1 OR larger than 10
  32.    display error message
  33.    stop program
  34. END IF
  35. store values in player 2 cards array
  36.  
  37. sort player 1 array into highest to lowest values
  38. sort player 2 array into highest to lowest values
  39.  
  40. take first value in each player's array
  41. IF p1 has higher value than p2
  42.    print message saying p1 has won
  43.    end game
  44.    
  45.    ELSE IF p2 has higher value than p1
  46.       print message saying p2 has won
  47.       end game
  48.      
  49.       ELSE IF both players have equal cards
  50.          go through each set until a set that does not contain equal cards is found
  51.          
  52.          IF p1 has higher value than p2
  53.          print message saying p1 has won
  54.          end game
  55.    
  56.             ELSE IF p2 has higher value than p1
  57.                print message saying p2 has won
  58.                end game
  59.          END IF
  60.          
  61. END IF
  62.  
  63. IF both players arrays are empty
  64.    display message saying that the game has ended in a draw
  65. END IF
  66.  
  67. END
  68. */
  69.  
  70.  
  71.  
  72. int main( void ){
  73.  
  74.    /* Initializes variables */
  75.    int p1cardnum;
  76.    int p2cardnum;
  77.    int a;
  78.    int b;
  79.    int swap;
  80.    
  81.    /* Scans input for number of cards that p1 has */
  82.    scanf("%d", &p1cardnum);
  83.    
  84.    /* Checks to make sure number of cards is within limits and gives error message and stops program if not */
  85.    if(p1cardnum < 2 || p1cardnum > 10){
  86.       printf("ERROR: Please enter a number of cards for Player 1 between 2 and 10.");
  87.       return;
  88.       }
  89.  
  90.    /* Creates array with number of elements that p1 has cards */
  91.    int p1cards[p1cardnum];
  92.    
  93.    /* Scans input number of times that p1 has cards and stores these inputs in the array */
  94.    for(a=0; a < p1cardnum; a++){
  95.       scanf("%d", &p1cards[a]);
  96.       /* Checks that each input is valid, prints error message and stops program if not */
  97.       if(p1cards[a] < 1 || p1cards[a] > 10){
  98.          printf("ERROR: Please make sure all cards for Player 1 are whole numbers between 1 and 10.");
  99.          return;
  100.          }
  101.       }
  102.    
  103.    /* Bubble sorting algorithm to arrange array from highest to lowest value */
  104.    /* Loops enough times until the array is sorted */
  105.    for(a=0; a < (p1cardnum - 1); a++){
  106.       /* Passes through the array so that the if statement can compare and rearrange the cards */
  107.       for(b=0; b < (p1cardnum - a - 1); b++){
  108.          /* Swaps the 2 numbers next to each other if the 1st is lower than the 2nd */
  109.          if(p1cards[b] < p1cards[b+1]){
  110.             swap = p1cards[b];
  111.             p1cards[b] = p1cards[b+1];
  112.             p1cards[b+1] = swap;
  113.             }
  114.          }
  115.       }
  116.    
  117.    /* Scans input for number of cards that p2 has */
  118.    scanf("%d", &p2cardnum);
  119.    
  120.    /* Checks to make sure number of cards is within limits and gives error message and stops program if not */
  121.    if(p2cardnum < 2 || p2cardnum > 10){
  122.       printf("ERROR: Please enter a number of cards for Player 2 between 2 and 10.");
  123.       return;
  124.       }
  125.    
  126.    /* Creates array with number of elements that p1 has cards */
  127.    int p2cards[p2cardnum];
  128.    
  129.    /* Scans input number of times that p2 has cards and stores these inputs in the array */
  130.    for(a=0; a < p2cardnum; a++){
  131.       scanf("%d", &p2cards[a]);
  132.       /* Checks that each input is valid, prints error message and stops program if not */
  133.       if(p2cards[a] < 1 || p2cards[a] > 10){
  134.          printf("ERROR: Please make sure all cards for Player 2 are whole numbers between 1 and 10.");
  135.          return;
  136.          }
  137.      
  138.       }
  139.    
  140.    /* Bubble sorting algorithm to arrange array from highest to lowest value */
  141.    /* Loops enough times until the array is sorted */
  142.    for(a=0; a < (p2cardnum - 1); a++){
  143.       /* Passes through the array so that the if statement can compare and rearrange the cards */
  144.       for(b=0; b < (p2cardnum - a - 1); b++){
  145.          /* Swaps the 2 numbers next to each other if the 1st is lower than the 2nd */
  146.          if(p2cards[b] < p2cards[b+1]){
  147.             swap = p2cards[b];
  148.             p2cards[b] = p2cards[b+1];
  149.             p2cards[b+1] = swap;
  150.             }
  151.          }
  152.       }
  153.    
  154.    /* Compares cards to determine winner of game */
  155.    /* Prints that p1 has won if they have a higher card that p2 and stops program*/
  156.    if(p1cards[0] > p2cards[0]){
  157.       printf("Player 1 is the winner!");
  158.       return;
  159.       }
  160.       /* Prints that p2 has won if they have a higher card than p1 and stops program */
  161.       else if(p1cards[0] < p2cards[0]){
  162.          printf("Player 2 is the winner!");
  163.          return;
  164.          }
  165.          
  166.          /* Determines winner when 1st set of cards or more are equal */
  167.          else {
  168.             /* Goes through cards until it gets to a set where p1 and p2 cards aren't equal */
  169.             for(a=1; p1cards[a] == p2cards[a]; a++){
  170.                printf("Round %d is a draw. Choosing next highest cards for next round...\n", a);
  171.                /* Prints message and ends game when both players run out of cards */
  172.                if((p1cards[a] && p2cards[a]) == 0){
  173.                   printf("Both players have ran out of cards. The game has ended in a draw.");
  174.                   return;
  175.                   }
  176.                }
  177.               /* Prints that p1 has won if they have a higher card that p2 and stops program */
  178.               if(p1cards[a] > p2cards[a]){
  179.                  printf("Player 1 is the winner!");
  180.                  return;
  181.                  }
  182.                  /* Prints that p2 has won if they have a higher card than p1 and stops program*/
  183.                  else if(p1cards[a] < p2cards[a]){
  184.                     printf("Player 2 is the winner!");
  185.                     return;
  186.                     }
  187.            
  188.          }
  189. }