<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2. /*  pseudocode
  3. *  START
  4. *     int I,p1count,p2count
  5. *     int p1card[10],p2card[10]  //zero is first index.
  6. *   START MAIN
  7. *     READ p1count
  8. *     I=0
  9. *     WHILE I < p1count
  10. *         READ p1card[I]   // oops remind myself that input maybe string and loop need to put it to array
  11. *         I=I+1
  12. *     END WHILE
  13. *     READ p2count
  14. *     I=0
  15. *     WHILE I < p2count
  16. *         READ p2card[I]
  17. *         I=I+1
  18. *     END WHILE
  19. *     bubble_sort (p1count, p1card)
  20. *     bubble_sort (p2count, p2card)
  21. *     WHILE I < p1count OR I < p2count
  22. *         IF p1card [I] > p2card [I]
  23. *            WRITE "p1wins game ends!"
  24. *         ELSE
  25. *           IF p2card [I] > p1card [I]
  26. *              WRITE "p2wins game ends!"
  27. *           ELSE
  28. *             IF p1card [I] == p2card [I]
  29. *                I=I+1
  30. *                IF I == p1count-1 AND p1count > p2count
  31. *                   WRITE "p1wins game ends!"   //p1 has more cards
  32. *                ELSE
  33. *                   IF I == p2count-1 AND p2count > p1count
  34. *                      WRITE "p2wins game ends!"  //p2 has more cards
  35. *                   ELSE
  36. *                      IF I == p1count AND p1count == p2count
  37. *                         WRITE "DRAW game ends!"
  38. *                      END IF
  39. *                   END IF
  40. *                END IF
  41. *             END IF
  42. *           END IF
  43. *         END IF
  44. *     END WHILE
  45. *    END MAIN
  46. *
  47. *
  48. *   START bubble_sort (count, array)
  49. *    char swap = 'FALSE'
  50. *    FOR i FROM 0 to count
  51. *       swap = 'FALSE'
  52. *       FOR j FROM 0 to count - 2
  53. *          if array[j] < array[j + 1]   //the highest card will be in position 0
  54. *              temp = array[j]
  55. *              array [j] = array[j + 1]
  56. *              array [j + 1] = temp
  57. *              swap = 'TRUE'
  58. *       END FOR
  59. *        IF swap = 'FALSE'
  60. *           BREAK   //exit loop when sorted
  61. *        END IF
  62. *    END FOR
  63. *       
  64. *   END bubble_sort
  65. *  END     
  66. */
  67. int main(void)
  68. {
  69. int count1,count2, i, j;
  70. char input1[30],input2[30];
  71. int temp;
  72. int p1[10] = {0,0,0,0,0,0,0,0,0,0,99}, p2[10] = {0,0,0,0,0,0,0,0,0,0,99}; // card value in the array.
  73.  
  74. //printf("number of cards p1");
  75. scanf("%d",&count1);
  76.  
  77. //printf("Enter cards held by p1");
  78. //for (i = 0; i==(count1-1); i++) { // **** was a loop to get input but doesn't work so using GEtMyInput sub.
  79. //scanf("%d",&p1[i]);
  80. //}
  81. GetMyInput(count1, p1);
  82.  
  83. //printf("number of cards p2");
  84. scanf("%d",&count2);
  85. //printf("Enter cards held by p2");
  86. //for (j = 0; j==count2-1; j++) { // **** was a loop to get input but doesn't work so using GEtMyInput sub.
  87. //scanf("%d",&p2[j]);
  88. //}
  89.  
  90. GetMyInput(count2, p2);
  91.  
  92. //checking values un array
  93. //printf("\n\n"); 
  94. //for (i=0; i<count1; ++i) printf("%d %d \n\n",count1,p1[i]);
  95. //for (j=0; j<count2; ++j) printf("%d %d \n\n",count2,p2[j]);
  96.  
  97.  
  98. bubble_sort(count1, p1);
  99. bubble_sort(count2, p2);
  100. i=0;j=0;
  101.  while (i < count1 || j < count2)
  102. {
  103.    if (p1[i] > p2[j])
  104.    {
  105.       printf ("\n player one wins!! game ends p1 bigger \n");
  106.       i = count1;
  107.       j = count2;
  108.       break;
  109.    } else {
  110.       if (p2 [j] > p1[i])
  111.       {
  112.          printf ("\n Player two wins!! game ends p2 bigger \n");
  113.          j = count2;
  114.          i = count1;
  115.          break;
  116.       } else {
  117.          if (p1[i++] == p2[j++])
  118.          {
  119.             //i++; j++;   //next turn
  120.                   
  121.             if (i == (count1-1) && count1 > count2) //if next turn is p1's last card
  122.             {
  123.                printf ("\n player one wins!! game ends p1 has more cards \n");
  124.                i = count1;
  125.                j = count2;
  126.                break;
  127.             } else {
  128.                   if (j == (count2-1) && count2 > count1)
  129.                   {
  130.                      printf ("\n Player two wins!! game ends p2 has more cards \n");
  131.                      j = count2;
  132.                      i = count1;
  133.                      break;
  134.  
  135.                   } else {
  136.                         if (i == count1 && count1 == count2)
  137.                         {
  138.                         printf ("\n Draw-- game over. \n");
  139.                         i = count1; j = count2;
  140.                         break;
  141.                         }
  142.                   }
  143.  
  144.             }
  145.          }
  146.       }
  147.    }
  148. } //end of while
  149.  
  150. } //end of main
  151.  
  152. void GetMyInput( int count, int *ar)
  153. {
  154. // needed to do this as the loop didn't put correct values into the array see **** above
  155. int array[10] = {0,0,0,0,0,0,0,0,0,0,99};
  156. switch (count)
  157. {
  158.    case 2: scanf("%d %d", &array[0], &array[1]);
  159.            break;
  160.    case 3: scanf("%d %d %d", &array[0], &array[1], &array[2]);
  161.            break;       
  162.    case 4: scanf("%d %d %d %d", &array[0], &array[1], &array[2], &array[3]);
  163.            break;     
  164.    case 5: scanf("%d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4]);
  165.            break;
  166.    case 6: scanf("%d %d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4], &array[5]);
  167.            break;
  168.    case 7: scanf("%d %d %d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4], &array[5], &array[6]);
  169.            break;
  170.    case 8: scanf("%d %d %d %d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4], &array[5], &array[6], &array[7]);
  171.            break;
  172.    case 9: scanf("%d %d %d %d %d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4], &array[5], &array[6], &array[7], &array[8]);
  173.            break;
  174.    case 10: scanf("%d %d %d %d %d %d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4], &array[5], &array[6], &array[7], &array[8], &array[9]);
  175.            break;
  176.            
  177. }
  178.            
  179. //for checking it's working
  180. /*    printf("\n %d", count);
  181.            for (int i = 0; i<10; i++)
  182.            {
  183.            ar[i] = array[i];
  184.      printf("ar is %d, ", ar[i]);     
  185.            
  186.            }
  187. */
  188.  
  189. } //end of getmyinput
  190.  
  191. void bubble_sort( int count, int *ar)
  192. {
  193. int array[10] = {0,0,0,0,0,0,0,0,0,0,99};
  194. int i = 0, j = 0, temp = 0;
  195. char swap = 'f'//set to false
  196. for (i = 0; i < count; i++)
  197.            {
  198.            array[i] = ar[i];   
  199.            }
  200. for (i = 0; i < count; i++)
  201. {
  202. swap = 'f';
  203.   for (j = 0; j <= (count - 2); j++)
  204.   {
  205.   if (array[j] < array[j + 1])
  206.   {
  207.    temp = array[j];
  208.    array[j] = array[j + 1];
  209.    array[j + 1] = temp;
  210.    swap = 't';   //set to true for a swap
  211.   }
  212.  
  213.   } // next j
  214.  
  215. if (swap == 'f') break//all sorted exit loop
  216. } //next i
  217.  
  218. //for checking it's working
  219. /*   printf("\n Now Sorted %d", count);
  220.            for (int i = 0; i < 10; i++)
  221.            {
  222.            ar[i] = array[i];
  223.      printf("ar is %d, ", ar[i]);     
  224.            
  225.            }
  226. */
  227. } //end of bubble_sort