<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.    
  4.     Give player one a number of cards between 2 and 10
  5.     Give player two a number of cards between 2 and 10
  6.     Arrange player one's cards in descending order, highest to lowest
  7.     Arrange player two's cards in descending order, highest to lowest
  8.     IF
  9.     Player one's number of cards is less than 2 or greater than 10
  10.     THEN
  11.     display error message
  12.     END IF
  13.    
  14.     IF
  15.     player two's number of cards is less than 2 or greater than 10
  16.     THEN
  17.     display error message
  18.     END IF
  19.  
  20.     player one shows their highest card
  21.     player two shows their highest card
  22.     IF
  23.     player one's card is greater than player two's card
  24.     THEN
  25.     display player one wins message
  26.     END IF
  27.    
  28.    
  29.     ELSE IF
  30.     player one's card is less than player two's card
  31.     THEN
  32.     display player two wins message
  33.     END ELSE IF
  34.    
  35.     ELSE IF
  36.     player one's card and player two's card are the same display draw
  37.     THEN
  38.     continue to display cards until either player draws a higher value card or they run out of cards
  39.     END ELSE IF
  40.    
  41.     IF both players run out of cards
  42.     THEN
  43.     display game over it's a draw message
  44.     END IF
  45. */
  46.  
  47. #include <stdio.h>
  48. int main()
  49. {
  50.  
  51.  
  52.  
  53. int player_one_card_quantity,player_two_card_quantity;
  54.    
  55. int p1_cards [10] = {0};
  56.    
  57. int p2_cards [10] = {0};
  58.  
  59. int game_number = 1; //set game number
  60.    
  61. //Start
  62.    
  63. printf("Game: %d\n",game_number);
  64.  
  65.  
  66. //Player one's turn
  67.    
  68. printf("Player one\n");
  69. printf("Enter a number of cards to draw (between 2-10) 0 to exit\n"); // Give P1 x amount of cards
  70. scanf("%d", &player_one_card_quantity);
  71. printf("P1 drew %d cards\n",player_one_card_quantity);
  72.    
  73.    
  74. //Error! exit if player one enters 0
  75. if(player_one_card_quantity == 0){
  76. printf("\nExiting\n");
  77. exit(-1);
  78. }
  79.    
  80.    
  81. //Display this error if card quantity is < 2
  82. if(player_one_card_quantity <2) {
  83. printf("Error! Too low, Enter a valid number between 2 and 10\n");
  84. exit(-1);
  85. }
  86.  
  87. //Display this error if card quantity is > 10
  88. if(player_one_card_quantity > 10) {
  89. printf("Error! Too large, Enter a valid number between 2 and 10\n");
  90. exit(-1);
  91. }
  92.    
  93.  
  94. // Determine's which cards have been drawn             //Increment the variable x by 1
  95. for (int x=0; x <player_one_card_quantity; x++){ //for as long as it's less than card quantity
  96.  scanf("%d",&p1_cards [x]);
  97. }
  98.                                                
  99. //Display this error if value of cards drawn for p1 is less than 1
  100. if(p1_cards[0] < 1) {
  101. printf("\nError! There is no lower card than one, the range is between 1 and 10");
  102. return (-1);
  103. }
  104.  
  105. //Display this error if value of cards drawn for p1 is greater than 10
  106. if(p1_cards[0] > 10) {
  107. printf("\nError! There is no higher card than ten, the range is between 1 and 10");
  108. return (-1);
  109. }
  110.  
  111.    
  112. //Bubble sort algorithm, this sorts whatever's been entered in desending order (highest to lowest card)
  113. int temp;     
  114. for (int i = 0; i < player_one_card_quantity - 1; i++){
  115.    
  116. for (int x = 0; x < player_one_card_quantity -1; x++){
  117.      
  118. if  (p1_cards[x] < p1_cards [x+1]){
  119. temp = p1_cards[x];
  120.    
  121. p1_cards[x] = p1_cards[x+1];
  122. p1_cards[x+1] = temp;
  123. }
  124. }
  125. }
  126. printf("Player one's cards: ");
  127. for (int x = 0; x < player_one_card_quantity; x++){
  128. printf("%d ",p1_cards[x]);
  129. }
  130.    
  131. //Player two's turn
  132.    
  133. printf("\n\nPlayer two\n");
  134. printf("Enter a number of cards to draw (between 2-10) 0 to exit\n");
  135. scanf("%d", &player_two_card_quantity);
  136. printf("P2 drew %d cards\n",player_two_card_quantity);
  137.    
  138. //Error 1 exit if player two enters 0
  139.  
  140. if(player_two_card_quantity == 0) {
  141. printf("\nExiting\n");
  142. exit(-1);
  143. }
  144.    
  145. //Error! if player one's input is invalid exit
  146. if(player_two_card_quantity <2 || player_two_card_quantity >10) {
  147. printf("Error! Enter a valid number between 2 and 10\n");
  148. exit(-1);
  149. }
  150.    
  151. //Determine which cards have been drawn for player two
  152. for (int x=0; x <player_two_card_quantity; x++){
  153. scanf("%d",&p2_cards [x]);
  154. }
  155.  
  156.  
  157. //Display this error if value of cards drawn for p2 is less than 1
  158. if(p2_cards[0] < 1) {
  159. printf("\nError! There is no lower card than one, the range is between 1 and 10");
  160. return (-1);
  161. }
  162.  
  163.  
  164. //Display this error if value of cards drawn for p2 is greater than 10
  165. if(p2_cards[0] > 10) {
  166. printf("\nError! There is no higher card than ten, the range is between 1 and 10");
  167. return (-1);
  168. }
  169.  
  170.    
  171. //Sorting algorith for player two, functiones the same way as mentiioned earlier
  172. for (int i = 0; i < player_two_card_quantity - 1; i++){
  173.    
  174. for (int x = 0; x < player_two_card_quantity -1; x++){
  175.      
  176. if  (p2_cards[x] < p2_cards [x+1]){
  177. temp = p2_cards[x];
  178.    
  179. p2_cards[x] = p2_cards[x+1];
  180. p2_cards[x+1] = temp;
  181. }
  182. }
  183. }
  184. printf("Player two's cards: ");
  185. for (int x = 0; x < player_two_card_quantity; x++){
  186. printf("%d ",p2_cards[x]);
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193. //Code for declaring the winner
  194.  
  195. {for(int i =0; i< p1_cards; i++){
  196. if (p1_cards[i] > p2_cards[i]){
  197. printf("\nGame over, player one wins\n");
  198. return 0;
  199. }
  200. else if (p1_cards[i] < p2_cards[i]){
  201. printf("\nGame over, Player two wins");
  202. return 0;
  203. }
  204. else if (p1_cards[i] == p2_cards[i]){
  205. printf("\nDraw");
  206. }
  207. }
  208. }
  209. }