<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2. //-----------------------------PSEUDOCODE------------------------------
  3. //INPUT Player1 card amount
  4. //IF number NOT >= 2 and <= 10
  5. //OUTPUT error message
  6. //ELSE Continue
  7. //INPUT numbers to P1numbers[] array
  8. //IF number NOT >= 1 and <= 10
  9. //OUTPUT error message
  10. //ELSE Continue
  11. //INPUT Player2 card amount
  12. //If number NOT >= 2 and <= 10
  13. //OUTPUT error message
  14. //ELSE Continue
  15. //INPUT numbers to P2numbers[] array
  16. //IF number NOT >= 1 and <= 10
  17. //OUTPUT error message
  18. //ELSE Continue
  19. //FOR counter less than P1Cards
  20. //IF P1list[counter] < P1list[counter + 1]
  21. //THEN temp = P1list[counter]
  22. //P1list[counter] = P1list[counter + 1]
  23. //P1list[counter + 1] = temp
  24. //REPEAT until all numbers sorted in descending order
  25. //FOR counter less than P2Cards
  26. //IF P2list[counter] < P2list[counter + 1]
  27. //THEN temp = P2list[counter]
  28. //P2list[counter] = P2list[counter + 1]
  29. //P2list[counter + 1] = temp
  30. //REPEAT until all numbers sorted in descending order
  31. //Initialise i to zero
  32. //Initialise j to zero
  33.  
  34. //FOR i <= P1cards AND j <= P2cards
  35. //IF P1list[i] > P2list[j]
  36. //THEN OUTPUT “Player 1 wins”
  37. //ELSE IF P1list[i] < P2list[j]
  38. //THEN OUTPUT “Player 2 wins”
  39. //ELSE IF i = 0 and j >1
  40. //THEN OUTPUT “Player 2 wins”
  41. //ELSE IF i > 1 and j = 0
  42. //THEN OUTPUT “Player 1 wins”
  43. //ELSE IF i = 0 and j = 0
  44. //THEN OUTPUT “Draw”
  45. //i = i + 1
  46. //j = j + 1
  47. //END
  48.  
  49. int main(void)
  50. {
  51.    int P1cards, P2cards;                               //declaring and giving variable meaningful name, as it will make it easier to keep track
  52.    int P1list[10], P2list[10];                         //declaring arrays for where the card numbers will be stored
  53.    int i, j, temp;                                     //declaring pointers
  54.  
  55.                                                   
  56.    for (i = 0; i < 10; i = i + 1)                      // Initialize both arrays to 0
  57.    {
  58.       P1list[i] = 0;
  59.       P2list[i] = 0;
  60.    }
  61.  
  62. //---------------Player 1---------------------
  63.    scanf("%d", &P1cards);                              //input being read
  64.  
  65.    if (P1cards >= 2 && P1cards <= 10)                  //This if function checks if the input is in within the range of 2-10
  66.    {                                                   //otherwise it will display an error message. Has to be a number and not an alphabet.   
  67.       printf("Player 1 has %d cards.\n", P1cards);
  68.    }
  69.    else
  70.    {
  71.       printf("Please enter a number between 2-10.\n"); //displaying error message because the input wasn't in the correct range.
  72.       return 0;                                        //exits the program because there was an error with the user input.
  73.    }
  74.    
  75.  
  76.    i = 0;                                              //initializing i back to zero so that the counter is reset
  77.    do {                                                //a loop to read the input numbers and stored inside an array.
  78.       scanf("%d", &P1list[i]);                         //reading input from user.
  79.       if (!(P1list[i] >= 1 && P1list[i] <= 10))        //checking that the input is a number between 1-10 as per the requirement.
  80.       {
  81.          printf("Please ensure you enter a number between 1 and 10."); //error message because of incorrect user input.
  82.  
  83.          return 0;                                     //exits the program because there was an error with the user input.
  84.       }
  85.       i = i + 1;                                       //incrementing by 1 so the next input can be stored in the next array element.
  86.    } while (i < (P1cards));                            //the do loop will repeat until the counter reaches the card ammount the player has.
  87.                                                        //the statement is "i <(P1cards)" instead of "i=<(P1cards)" because the array starts at 0 instead of 1.
  88.  
  89.    for (j = 0; j < P1cards - 1; j = j + 1)             //BUBBLESORT loop, a nested for loop that repeats until the counter is 1 less than P1cards
  90.    {
  91.       for (i = 0; i < P1cards - j - 1; i = i + 1)      //inner part of the loop that does 1 cycle of the sort and the outer loop that will repeat this cycle
  92.       {
  93.          if (P1list[i] < P1list[i + 1])                //compares the current array with the next array
  94.          {
  95.             temp = P1list[i];                          //puts the number in a temporary variable
  96.             P1list[i] = P1list[i + 1];                 //puts the number in the next array
  97.             P1list[i + 1] = temp;                      //the next array gets initialised with the temporary variaby
  98.          }
  99.       }
  100.    }
  101.  
  102.    printf("Player 1 cards (Highest first): ");         //displays the list of numbers in descending order
  103.    for (i = 0; i<P1cards; ++i)
  104.       printf("%d  ", P1list[i]);
  105.  
  106.  
  107.  
  108. //------------------------Player 2----------------------- (Same process as above)
  109.    scanf("%d", &P2cards);
  110.  
  111.    if (P2cards >= 2 && P2cards <= 10)                  //again checking the
  112.    {
  113.       printf("\n\nPlayer 2 has %d cards.\n", P2cards);
  114.    }
  115.    else
  116.    {
  117.       printf("Please enter a number between 2-10.\n");
  118.       return 0;
  119.    }
  120.    i = 0;
  121.    do {
  122.       scanf("%d", &P2list[i]);
  123.       if (!(P2list[i] >= 1 && P2list[i] <= 10))
  124.       {
  125.          printf("Cards may only be numbers between 1-10. Try again!");
  126.          return 0;
  127.       }
  128.       i = i + 1;
  129.    } while (i < (P2cards));
  130.  
  131.  
  132.    for (j = 0; j < P2cards - 1; j = j + 1)
  133.    {
  134.       for (i = 0, temp = 0; i < P2cards - j - 1; i = i + 1)
  135.       {
  136.          if (P2list[i] < P2list[i + 1])
  137.          {
  138.             temp = P2list[i];
  139.             P2list[i] = P2list[i + 1];
  140.             P2list[i + 1] = temp;
  141.          }
  142.       }
  143.    }
  144.  
  145.    printf("Player 2 cards (Highest first): ");
  146.    for (i = 0; i<P2cards; i = i + 1)
  147.       printf("%d  ", P2list[i]);
  148.  
  149.  
  150. //--------------------------Game outcome--------------------------------
  151.  
  152.    for (i = 0; i < 10; i=i+1)
  153.    {
  154.       if (P1list[i] > P2list[i])                       //if player 1's number in the array is bigger
  155.       {
  156.          printf("\n\nPlayer 1 wins.");                 //then player 1 wins
  157.          return 0;                                     //exits out of the loop
  158.       }
  159.       else if (P1list[i] < P2list[i])                  //if player 2's number in the array is bigger
  160.       {
  161.          printf("\n\nPlayer 2 wins.");                 //then player 2 wins
  162.          return 0;                                     //exits out of the loop
  163.       }
  164.    }
  165.  
  166.    printf("\n\nDraw.");                                //if both players cards runs out then its a draw
  167.    return 0;
  168. }