<mosaic.cnfolio.com>
PROGRAM
  1. /* Pseudocode:
  2.              
  3.             START
  4.                  Assign player 1 with a quantity of cards between 2 and 10
  5.                  Assign player 2 with a quantity of cards between 2 and 10
  6.                  Assign player 1 with a list of cards between 1 and 10 based on the quantity of cards they selected
  7.                  Assign player 1 with a list of cards between 1 and 10 based on the quantity of cards they selected
  8.                
  9.                  Store the amount of cards that player 1 has
  10.                  Store the amount of cards that player 2 has
  11.              
  12.                  Sort cards of player 1 in descending order using a bubble sort algorithm
  13.                  Sort cards of player 2 in descending order using a bubble sort algorithm
  14.                
  15.                  Compare player 1's current card with player 2's current card
  16.             IF   Player 1's card > player 2's card
  17.               THEN
  18.                    Display message, "Player 1 wins"
  19.             ELSEIF Player 2's card > player 1's card
  20.               THEN
  21.                    Display message, "Player 2 wins"
  22.             ELSEIF Player 1's card = player 2's card
  23.               THEN
  24.                    Return and loop back up and compare the next player 1's and player 2's cards
  25.             ELSEIF Both player cards are empty
  26.               THEN
  27.                    Display message, "Draw"
  28.             END IF
  29.              
  30.           */
  31.                
  32.     #include <stdio.h>
  33.     #define MAX_CARD 10    // Maximum array size for the card game
  34.              
  35.     int main(void)
  36.     {
  37.      
  38.     /* Declaring all the variables that will be required at the beginning of the program */
  39.      
  40.              int player1card[MAX_CARD]; // The numbered cards player 1 will have
  41.              int player2card[MAX_CARD]; // The numbered cards player 2 will have
  42.                  
  43.              int p1size; // The quantity of cards that player 1 will have
  44.              int p2size; // The quantity of cards that player 2 will have
  45.                
  46.              int i;
  47.              int j;
  48.              int Y;
  49.              int temp;
  50.              int winner;
  51.          
  52.     printf("Welcome to this card game! ");
  53.      
  54.     /* Code for player 1 of the game */           
  55.     /* Lets the user input the size of the array for player 1 */
  56.      
  57.     scanf("%d", &p1size);
  58.              
  59.     /* Lets the user input the amount of elements in the array */
  60.              
  61.     for(i=0; i<p1size; i++)
  62.        {
  63.       scanf("%d", &player1card[i]);
  64.        }         
  65.      
  66.     for(i=0; i<p1size; i++)
  67.        {
  68.      
  69.     /* Place currently selected element array[i] to its correct place. */
  70.      
  71.     for(j=i+1; j<p1size; j++)
  72.          {
  73.     /* Swap if currently selected array element is not at its correct position. */
  74.      
  75.     if(player1card[i] < player1card[j])
  76.            {
  77.     temp = player1card[i];
  78.     player1card[i] = player1card[j];
  79.     player1card[j] = temp;
  80.        }
  81.          }
  82.            }
  83.              
  84.     /* Print the sorted array of cards for player 1 */
  85.      
  86.     printf("\n\nPlayer 1's cards in descending order: ");
  87.      
  88.     for(i=0; i<p1size; i++)
  89.        {
  90.     printf("%d\t", player1card[i]);
  91.        }
  92.                    
  93.      
  94.      
  95.      
  96.      
  97.     /* Code for player 2 of the game */   
  98.     /* Lets the user input the size of the array for player 2 */
  99.      
  100.     scanf("%d", &p2size);
  101.              
  102.     /* Lets the user input the amount of elements in the array */
  103.              
  104.     for(i=0; i<p2size; i++)
  105.        {
  106.     scanf("%d", &player2card[i]);
  107.        }
  108.      
  109.     for(i=0; i<p2size; i++)
  110.        {
  111.        
  112.     /* Place currently selected element array[i] to its correct place. */
  113.      
  114.     for(j=i+1; j<p2size; j++)
  115.           {
  116.      
  117.     /* Swap if currently selected array element is not at its correct position. */
  118.      
  119.     if(player2card[i] < player2card[j])
  120.             {
  121.     temp = player2card[i];
  122.     player2card[i] = player2card[j];
  123.     player2card[j] = temp;
  124.        }
  125.          }
  126.            }
  127.              
  128.     /* Print the sorted array of cards for player 2 */
  129.      
  130.     printf("\nPlayer 2's cards in descending order: ");
  131.     for(i=0; i<p2size; i++)
  132.       {
  133.     printf("%d\t", player2card[i]);
  134.       }
  135.              
  136.      
  137.      
  138.     /* Comparing player 1 and player 2's cards and calculating the result */
  139.      
  140.     if (p1size > p2size)
  141.       {
  142.     Y = p1size;  // If player 1's cards are higher, player 1 wins
  143.       }
  144.      
  145.     else if (p2size > p1size)
  146.       {
  147.     Y = p2size; // If player 2's cards are higher, player 2 wins
  148.       }
  149.      
  150.     else
  151.       {
  152.     Y = p1size;
  153.       }
  154.            
  155.     for(i=0; i<Y; i++)
  156.        {
  157.      
  158.     if(player1card[i] > player2card[i])
  159.       {   
  160.     winner = 1;
  161.          
  162.     break;
  163.       }
  164.     else if (player1card[i] < player2card[i])
  165.       {
  166.          
  167.     winner = 2;
  168.          
  169.     break;
  170.       }
  171.       }
  172.      
  173.      
  174.     /* Print the output of who has won the game or if its a draw */
  175.      
  176.     if(winner == 1)
  177.       {
  178.          
  179.     printf("\nPlayer 1 wins the game");
  180.       }
  181.     else if (winner == 2)
  182.       {
  183.     printf("\n\nPlayer 2 wins the game");
  184.       }
  185.      
  186.     else
  187.       {
  188.     printf("\n\nDraw ");
  189.       }   
  190.             }