<mosaic.cnfolio.com>
PROGRAM
  1. /*ENG421 COURSEWORK*/
  2.  
  3. /* PSEUDOCODE
  4.  
  5. INPUT number of cards in player 1's hand
  6. INPUT player 1's cards
  7. INPUT number of cards in player 2's hand
  8. INPUT player 2's cards
  9.  
  10. IF number of player 1 cards is less than 2 or greater than 10
  11.    THEN
  12.       RETURN error message
  13.       STOP GAME
  14.    ELSE IF number of player 2 cards is less than 2 or greater than 10
  15.       THEN
  16.          RETURN error message
  17.          STOP GAME
  18. END IF
  19.  
  20. WHILE player 1 has unchecked cards
  21.    IF card is less than one or greater than 10
  22.       THEN
  23.          RETURN error message
  24.          STOP GAME
  25.    END IF
  26. END WHILE
  27.  
  28. WHILE player 2 has unchecked cards
  29.    IF card is less than one or greater than 10
  30.       THEN
  31.          RETURN error message
  32.          STOP GAME
  33.    END IF
  34. END WHILE
  35.  
  36. SORT player 1's cards highest to lowest
  37. SORT player 2's cards highest to lowest
  38.  
  39. IF number of cards in player 1's hand is greater than number in player 2's hand
  40.    THEN
  41.       number of turns limit is number of cards in player 2's hand
  42.    ELSE
  43.       number of turns limit is number of cards in player 1's hand
  44. END IF
  45.  
  46. SET turn counter to 0
  47.  
  48. WHILE turn counter does not equal the turns limit
  49.    GET highest number player 1 card
  50.    GET highest number player 2 card
  51.    Increment the turn counter
  52.    IF player 1 card is greater than player 2 card
  53.       THEN 
  54.          player 1 wins
  55.          STOP GAME
  56.       ELSE IF player 2 card is greater than player 1 card
  57.          THEN
  58.             player 2 wins
  59.             STOP GAME
  60.       ELSE
  61.          They have the same card and the turn is a draw
  62.          Remove the most recently played cards from each player's hand
  63.          Increment the turn counter
  64.    END IF
  65. END WHILE
  66.  
  67. IF both players are out of cards
  68.    THEN
  69.       The game ends in a draw
  70.    ELSE IF player 1 is out of cards
  71.       THEN
  72.          Player 2 is the winner
  73.    ELSE IF player 2 is out of cards
  74.       THEN
  75.          Player 1 is the winner
  76. END IF
  77. */
  78.  
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81.  
  82. int main()
  83. {
  84.    printf("Welcome!\n");
  85.    int numPlayer1;
  86.    int numPlayer2;
  87.    int i=0;
  88.    int temp;
  89.    
  90.    scanf("%d",&numPlayer1); /*Get number of player 1 cards*/
  91.    int player1Cards[numPlayer1]; /*Creates an array ready for player 1's cards*/
  92.    while (i<numPlayer1) /*Gets the individual cards of player 1*/
  93.    {
  94.       scanf("%d",&temp);
  95.       if (temp<1 || temp>10) /*Checks the card is valid*/
  96.       {
  97.          printf("Error: At least one of player 1's cards are invalid");
  98.          exit(0);
  99.       }
  100.       player1Cards[i]=temp; /*Saves the input card into the array*/
  101.       i++;
  102.    }
  103.    
  104.    scanf("%d",&numPlayer2); /*Get number of player 2 cards*/
  105.    int player2Cards[numPlayer2];
  106.    
  107.    i=0;
  108.    
  109.    while (i<numPlayer2) /*Gets the individual player 2 cards*/
  110.    {
  111.       scanf("%d",&temp);
  112.       if (temp<1 || temp>10) /*Checks the card is valid*/
  113.       {
  114.          printf("Error: At least one of player 2's cards are invalid");
  115.          exit(0);
  116.       }
  117.       player2Cards[i]=temp; /*Saves the input card into the array*/
  118.       i++;
  119.    }
  120.    
  121.    if (numPlayer1<2 || numPlayer1>10) /*Checks number of cards for player 1*/
  122.    {
  123.       printf("Error: Player 1 does not have a correct number of cards");
  124.       exit(0);
  125.    }
  126.    else if (numPlayer2<2 || numPlayer2>10) /*Checks number of cards for player 2*/
  127.    {
  128.       printf("Error: Player 2 does not have a correct number of cards");
  129.       exit(0);
  130.    }
  131.    
  132.    /*The following is an implementation of bubble sort to sort the list of cards for each player.
  133.    A 'for' loop is used to re-run the algorithm until the list is in order*/
  134.    
  135.    int c=0;
  136.    int d=0;
  137.    int n=numPlayer1;
  138.    int swap=0;
  139.  
  140.    for (c = 0 ; c < ( n - 1 ); c++) /*Sorts player 1 cards*/
  141.     {
  142.       for (d = 0 ; d < n - c - 1; d++)
  143.       {
  144.         if (player1Cards[d] < player1Cards[d+1])
  145.         {
  146.          swap = player1Cards[d];
  147.          player1Cards[d]   = player1Cards[d+1];
  148.          player1Cards[d+1] = swap;
  149.         }
  150.       }
  151.     }
  152.    
  153.    c=0;
  154.    d=0;
  155.    n=numPlayer2;
  156.    swap=0;
  157.    
  158.    for (c = 0 ; c < ( n - 1 ); c++) /*Sorts player 2 cards*/
  159.     {
  160.       for (d = 0 ; d < n - c - 1; d++)
  161.       {
  162.         if (player2Cards[d] < player2Cards[d+1])
  163.         {
  164.          swap = player2Cards[d];
  165.          player2Cards[d]   = player2Cards[d+1];
  166.          player2Cards[d+1] = swap;
  167.         }
  168.       }
  169.     }
  170.    
  171.    /*End of sorting*/
  172.    
  173.    int turnLimit;
  174.    
  175.    if (numPlayer1>numPlayer2) /*Determines the max number of turns possible until at least 1 player is out of cards*/
  176.    {
  177.       turnLimit=numPlayer2; /*If player 1 has a greater number of cards then the game is limited by the number of cards of player 2*/
  178.    }
  179.    else
  180.    {
  181.       turnLimit=numPlayer1; /*If player 2 has a greater number of cards, they have the same number each,
  182.                               then the game is limited by the number of cards of player 1*/
  183.    }
  184.    
  185.    int turnCounter=0;
  186.    int current1;
  187.    int current2;
  188.    
  189.    while (turnCounter<turnLimit) /*While loop to repeat until the previously determined turn limit is reached*/
  190.    {
  191.       current1=player1Cards[turnCounter];
  192.       current2=player2Cards[turnCounter];
  193.       if (current1>current2)
  194.       {
  195.          printf("Player 1 is the winner!"); /*Winner is selected during the while loop*/
  196.          exit(0);
  197.       }
  198.       else if (current2>current1)
  199.       {
  200.          printf("Player 2 is the winner!");
  201.          exit(0);
  202.       }
  203.       else
  204.       {
  205.          turnCounter++;
  206.       }
  207.    }
  208.    
  209.    printf("The game is a draw");
  210.    
  211.    return 0;
  212. }