<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*
  5. *
  6. * The pseudo code for this is fairly simple
  7. *
  8. * 1. Reserve memory for Player One array size
  9. * 2. Get Array Size for Player One
  10. * 3. Reserve memory for the array
  11. * 4. Get the array from the user
  12. * 5. Sort the array
  13. * 6. Repeat Steps 1 - 4 with Player Two
  14. * 7. Simulate game
  15. * 8. While playerOneIndex < playerOneSize and playerTwoIndex < playerTwoSize {
  16. *      if (playerOneHand[playerOneIndex] > playerTwoHand[playerTwoIndex]) {
  17. *          player one wins!
  18. *      }
  19. *      if (playerOneHand[playerOneIndex] < playerTwoHand[playerTwoIndex]) {
  20. *          player two wins!
  21. *      }
  22. *    }
  23. *    if (playerOneIndex == playerTwoIndex) {
  24. *      Tie Game 
  25. *    }
  26. *    if (playerTwoIndex == playerTwoSize) {
  27. *      Player 1 wins
  28. *    }
  29. *    Player 2 wins
  30. * 9. Display results
  31. */
  32.  
  33. void sort(int array[], int sizeOfArray) {
  34.     int val, j, i;
  35.     for (i = 1; i < sizeOfArray; i++) {
  36.         val = array[i]; // The val is the 2nd element of the array
  37.         j = i - 1; // J is the 1st element
  38.         while (j >= 0 && array[j] < val) { // If j greater than 0 and index j of array is less than val
  39.             array[j + 1] = array[j]; // swap indices
  40.             j = j - 1;
  41.         }
  42.         array[j + 1] = val; // Place val back into the array
  43.     }
  44. }
  45.  
  46. char simulateGame(int* playerOne, int playerOneSize, int* playerTwo, int playerTwoSize) {
  47.     int playerOneIndex = 0;
  48.     int playerTwoIndex = 0;
  49.     while (playerOneIndex < playerOneSize && playerTwoIndex < playerTwoSize) {
  50.         // Is the next card in Players One's hand greater than the next card in player two's hand
  51.         if (playerOne[playerOneIndex] > playerTwo[playerTwoIndex]) {
  52.             return '1';
  53.         }
  54.         // Is the next card in Players One's hand less than the next card in player two's hand
  55.         else if (playerOne[playerOneIndex] < playerTwo[playerTwoIndex]) {
  56.             return '2';
  57.         }
  58.         playerOneIndex++;
  59.         playerTwoIndex++;
  60.     }
  61.     // Have we gone through each card and been the same each time
  62.     if (playerTwoIndex == playerOneIndex) {
  63.         return 'T';
  64.     }
  65.     // Did we run out of cards in Player Two's hand
  66.     if (playerTwoIndex == playerTwoSize) {
  67.         return '1';
  68.     }
  69.     // We must have run out of cards in Player One's hand
  70.     return '2';
  71. }
  72.  
  73. int* getPlayerHand(int size) {
  74.     int* playerHand = (int *) malloc(sizeof(int) * size); // Reserve Memory for the hand
  75.     int i = 0;
  76.     while (i < size) {
  77.         scanf("%d", &playerHand[i]); // Get ints for 0 - size
  78.         i++;
  79.     }
  80.     return playerHand;
  81. }
  82.  
  83. int main()
  84. {
  85.     int* playerOneSize = (int *) malloc(sizeof(int)); // Memory for the input below
  86.     scanf("%d", playerOneSize);
  87.     int* playerOneHand = getPlayerHand(*playerOneSize); // Get Player One's Hand
  88.     sort(playerOneHand, *playerOneSize);
  89.     int* playerTwoSize = (int *) malloc(sizeof(int)); // Memory for the input below
  90.     scanf("%d", playerTwoSize);
  91.     int* playerTwoHand = getPlayerHand(*playerTwoSize); // Get Player Two's hand
  92.     sort(playerTwoHand, *playerTwoSize);
  93.     char winner = simulateGame(playerOneHand, *playerOneSize, playerTwoHand, *playerTwoSize);
  94.     if (winner == '1') {
  95.         printf("Player 1 wins!\n");
  96.     }
  97.     else if (winner == '2') {
  98.         printf("Player 2 wins!\n");
  99.     }
  100.     else {
  101.         printf("Tie game!\n");
  102.     }
  103.     // Free all memory allocated to avoid memory leaks
  104.     free(playerOneSize);
  105.     free(playerTwoSize);
  106.     free(playerOneHand);
  107.     free(playerTwoHand);
  108.     return 0;
  109. }