<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3. Retrieve Player1 total
  4. if Player 1 total lessthan 2 or greaterthan 10
  5.    then display: please enter a number between 2 and 10
  6. end if
  7. Retrieve Player1 hand equal to total number input
  8. Order Player1 hand in descending order
  9.  
  10. Retrieve Player2 total
  11. if Player 2 total lessthan 2 or greaterthan 10
  12.    then display: please enter a number between 2 and 10
  13. end if
  14. Retrieve Player2 hand equal to total number input
  15. Order Player2 hand in descending order
  16.  
  17. If Player1's largest is greater than Player2's largest
  18.    then display: Player1 Wins
  19. End if
  20. Else if Player2's largest is greater than Player1's largest
  21.    then display: Player1 wins
  22. End if
  23. Else if Player1's largest is equal to Player2's largest
  24.    then draw next card
  25. End if
  26.  
  27. if Player1 runs out of cards
  28.    then display: Player 2 wins, Player one has no cards left
  29. End if
  30. if Player2 runs out of cards
  31.    then display: Player 1 wins, Player two has no cards left
  32. End if
  33. if both player1 and player2 runs out of cards
  34.    then display: its a draw, both players out of cards
  35. End if
  36.  
  37.  
  38.  
  39.  
  40. */
  41.  
  42. #include <stdio.h>
  43. #include <ctype.h>
  44. int main( void )
  45. {
  46. int P1Handsize;              /*variable to hold player 1's handsize*/
  47. int P2Handsize;               /*variable to hold player 2's handsize*/
  48. int P1card[10] = {0};        /*array to hold player 1's integer cards*/
  49. int P2card[10] = {0};         /*array to hold player 2's integer cards*/
  50. char P1c[10];            /*array to hold player 1's char inputs*/ 
  51. char P2c[10];              /*array to hold player 2's char inputs*/
  52. scanf("%d",&P1Handsize);            /*reads player 1's hand and stores it as an int*/
  53. if (P1Handsize > 10)       /*checks if P1's hand is too big*/
  54. {
  55. printf("Player 1 handsize too large, maximum : 10");
  56. return 0;
  57. }
  58. else if (P1Handsize < 2)               /*Checks if P1's hand is too small*/
  59. {
  60. printf("Player 1 handsize too small, minimum : 2");
  61. return 0;
  62. }
  63.  
  64.  
  65. for (int i = 0; i < P1Handsize; i++)         /*scans each input up until player one's handsize is reached*/
  66. {
  67. scanf("%s", &P1c[i]);                        /*Stores as characters in char array*/
  68. }
  69. for (int i = 0; i < P1Handsize; i++)
  70. {
  71. if (isalpha(P1c[i]))         /*checks if any alphabetical characters were entered*/
  72. {
  73. printf("Error, Player 1, character entered. Please enter a number");
  74. return 0;
  75. }
  76. }
  77. for (int i = 0; i < P1Handsize; i++)   /*places each slot of the array in the corresponding slot of the int array*/
  78. {
  79. P1card[i] = P1c[i] -48 ;                     /*-48 as 0 is number 48 in ascii*/
  80. /*printf("%d ", P1card[i]);*/
  81. }
  82. for (int i = 0; i < P1Handsize; i++)         /*checks if the numbers entered into the array were too big/small*/
  83. {
  84. if (P1card[i] < 1)
  85. {
  86. printf("Error, Player 1, please enter numbers greater than 0");
  87. return 0;
  88. }
  89. else if (P1card[i] >10)
  90. {
  91. printf("Error, Player 1, lease enter numbers less than 11");
  92. return 0;
  93. }
  94. }
  95. order(P1Handsize,P1card);          /*Calls order function to order cards in ascending */
  96.  
  97. printf("\n");
  98. for (int i = 0; i < P1Handsize; i++)   /*Prints cards in descending order*/
  99. {
  100. printf("%d ", P1card[i]);
  101. }
  102. printf("\n");
  103. scanf("%d",&P2Handsize);               /*Scans P2 handsize */
  104. if (P2Handsize > 10)       /*Checks if handsize is too big*/
  105. {
  106. printf("Player 2 handsize too large, maximum : 10");
  107. return 0;
  108. }
  109. else if (P2Handsize < 2)               /*checks if handsize is too small*/
  110. {
  111. printf("Player 2 handsize too small, minimum : 2");
  112. return 0;
  113. }
  114.  
  115. for (int i = 0; i < P2Handsize; i++)      /*Scans in characters and stores in p2's char  array*/
  116. {
  117. scanf("%s", &P2c[i]);
  118. }
  119. for (int i = 0; i < P2Handsize; i++)      /*Checks for any alphabetical characters in P2's array*/
  120. {
  121. if (isalpha(P2c[i]))
  122. {
  123. printf("Error, Player 2, character entered. Please enter a number");
  124. return 0;
  125. }
  126. }
  127. for (int i = 0; i < P2Handsize; i++)      /*Converts Char array into int array*/
  128. {
  129. P2card[i] = P2c[i] -48 ;
  130. /*printf("%d ", P2card[i]);*/
  131. }
  132.  
  133. for (int i = 0; i < P2Handsize; i++)      /*Checks if card values are within the ranges*/
  134. {
  135. if (P2card[i] < 1)
  136. {
  137. printf("Error, Player 2, Please enter numbers greater than 0");
  138. return 0;
  139. }
  140. else if (P2card[i] >10)
  141. {
  142. printf("Error, Player 2, Please enter numbers less than 11");
  143. return 0;
  144. }
  145. }
  146. order(P2Handsize,P2card);            /*Orders P2's array in descending*/
  147. for (int i = 0; i < P2Handsize; i++)
  148. {
  149. printf("%d ", P2card[i]);             /*Prints p2's ordered array*/
  150. }
  151. Winner(P1card,P2card,P1Handsize,P2Handsize );   /*Calls winner function*/
  152.  
  153.  
  154. }
  155.  
  156.  
  157.  
  158. int order(int Handsize,int card[])           /*Ordering using bubblesort */
  159. {
  160. for(int j = 0; j < Handsize -1; j++)
  161. {
  162. for(int i = 0; i < Handsize -1; i++)
  163. {
  164. if (card[i] < card[i+1])            /*if array variable is larger then next value in the array then swap*/
  165. {
  166. int temp = card[i] ;
  167. card[i] = card[i+1];
  168. card[i+1] = temp;
  169. }
  170. }
  171. }
  172. return card;
  173. }
  174.  
  175. int Winner(int P1card[],int P2card[],int P1Handsize, int P2Handsize )
  176. {
  177. printf("\n");
  178. int winner = 0;
  179. if(P1card[0] > P2card[0])      /*if p1's card is bigger p1 wins*/
  180. {
  181. printf("Player 1 Wins");
  182. winner = 1;
  183. return winner;
  184. }
  185. if(P1card[0] < P2card[0])      /*if p2's card is bigger p2 wins*/
  186. {
  187. printf("Player 2 Wins");
  188. winner =1;
  189. return winner;
  190. }
  191. if(P1card[0] = P2card[0])      /*if p1's card is equal to p2's call draw function*/
  192. {
  193. /*printf("Draw");
  194. printf("\n");*/
  195. Draw(P1card,P2card,P1Handsize,P2Handsize);
  196. winner = 0;
  197. }
  198. return winner;
  199. }
  200.  
  201. int Draw(int P1card[],int P2card[], int P1Handsize, int P2Handsize)
  202. {
  203. int winner =0;
  204. P1Handsize --;    /*reduce both handsizes by 1 */
  205. P2Handsize --;
  206. if (P1Handsize ==0 && P2Handsize !=0)    /*if p1's hand is empty, p2 wins */
  207. {
  208. printf("Player 2 Wins");
  209. winner = 1;
  210. return winner;
  211. }
  212. if (P2Handsize ==0 && P1Handsize !=0)    /*if p2's hand is empty, p1 wins */
  213. {
  214. printf("Player 1 Wins");
  215. winner = 1;
  216. return winner;
  217. }
  218. if (P2Handsize ==0 && P1Handsize ==0)      /*if both players hands are empty its a draw */
  219. {
  220. printf("No cards for either player, draw");
  221. winner = 1;
  222. return winner;
  223. }
  224. for(int i = 0; i < P1Handsize ; i++)      /* when card is deleted from hand then array[0] replaced with array[1]*/
  225. {
  226. P1card[i] = P1card[i+1];
  227. }
  228. for(int i = 0; i < P2Handsize ; i++)
  229. {
  230. P2card[i] = P2card[i+1];
  231. }
  232. Winner(P1card,P2card,P1Handsize,P2Handsize);
  233. }