<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.    INPUT Number of cards player 1 has
  4.    INPUT Player 1 cards
  5.  
  6.    INPUT Number of cards player 2 has
  7.    INPUT Player 2 cards
  8.  
  9.    SORT Player 1 cards in descending order
  10.    SORT Player 2 cards in descending order
  11.  
  12.  
  13.    DRAW CARDS until we find a winner or someone runs out of cards
  14.       IF Player 1's card is bigger then Player 2's card
  15.          Player 1 is the winner 
  16.       ELSE IF Player 2's card is bigger then Player 1's card
  17.          Player 2 is the winner
  18.       END IF
  19.    END DRAW
  20.    
  21.    IF we have the winner
  22.       PRINT out the winner
  23.    ELSE
  24.       IF Player 1 has the same number of cards as Player 2
  25.          PRINT The game is a draw
  26.       ELSE
  27.          PRINT The player who has more card is the winner
  28.       END IF
  29.    END IF
  30. END
  31. */
  32.  
  33. int main( void )
  34. {
  35.    int player1CardNumber, player2CardNumber; //Storing the number of cards each player has
  36.    int player1Cards [10], player2Cards [10]; //Storing each player's card values
  37.    int winner=0; //Stores the number of the winner (0: no winner (yet), 1: player1 won, 2: player2 won
  38.    int maxRoundsToPlay; //Stores the number of maximum turns the game will have
  39.    int j, i, temp; //Loop indexes and assisting variables
  40.    
  41.    
  42.    //READING INPUTS
  43.    
  44.    /* Player1 card number */
  45.  
  46.    do{
  47.      printf("How many cards does Player1 have? (Number between 1 and 10)\n");
  48.      scanf( "%d", &player1CardNumber);
  49.       if(player1CardNumber < 1 || player1CardNumber > 10)
  50.       {
  51.            printf("Error: The input was not a number between 1 and 10\n");
  52.       }
  53.    }while(player1CardNumber < 1 || player1CardNumber > 10);
  54.    printf("Player1 has %d cards\n", player1CardNumber);
  55.    
  56.    /* Player1 cards */   
  57.      
  58.    for ( i = 0 ; i <  player1CardNumber; i++ )     
  59.    {
  60.       do
  61.       {
  62.          printf("What is Player1's %d. card? (Number between 1 and 10)\n", i+1);
  63.          scanf( "%d", &player1Cards[i]);
  64.          
  65.          if(player1Cards[i] < 1 || player1Cards[i] > 10)
  66.            {
  67.               printf("Error: The input (%d) was not a number between 1 and 10\n", player1Cards[i]);
  68.          }
  69.          
  70.       }while(player1Cards[i] < 1 || player1Cards[i] > 10);
  71.       printf("Player1's %d. card is: %d\n", i+1, player1Cards[i]);
  72.    }
  73.    
  74.    /* Player2 card number */
  75.    
  76.    do
  77.    {
  78.       printf("How many cards does Player2 have? (Number between 1 and 10)\n");
  79.     scanf( "%d", &player2CardNumber);
  80.       if(player2CardNumber < 1 || player2CardNumber > 10)
  81.       {
  82.            printf("Error: The input was not a number between 1 and 10\n");
  83.       }
  84.    }while(player2CardNumber < 1 || player2CardNumber > 10);
  85.    printf("Player2 has %d cards\n", player2CardNumber);
  86.    
  87.    
  88.    /* Player2 cards */ 
  89.    
  90.    for ( i = 0 ; i <  player2CardNumber; i++ )     
  91.    {
  92.       do{
  93.          printf("What is Player2's %d. card? (Number between 1 and 10)\n", i+1);
  94.          scanf( "%d", &player2Cards[i]);
  95.          if(player2Cards[i] < 1 || player2Cards[i] > 10)
  96.            {
  97.               printf("Error: The input (%d) was not a number between 1 and 10\n", player2Cards[i]);
  98.          }
  99.       }while(player2Cards[i] < 1 || player2Cards[i] > 10);
  100.       printf("Player2's %d. card is: %d\n", i+1, player2Cards[i]);
  101.    }   
  102.    
  103.    
  104.    // CARD SORTING - Bubble sort algorithm, descending order
  105.    
  106.    
  107.     //Player 1 sorting
  108.    
  109.    
  110.    for (i = 0; i < player1CardNumber; i++)
  111.    {
  112.       for (j = 0; j < (player1CardNumber - i - 1); j++)
  113.       {
  114.          if (player1Cards[j] < player1Cards[j + 1])
  115.          {
  116.             temp = player1Cards[j];
  117.             player1Cards[j] = player1Cards[j + 1];
  118.             player1Cards[j + 1] = temp;
  119.          }
  120.       }
  121.    }
  122.    printf("Player1's card sorted order is...\n");
  123.    for (i = 0; i < player1CardNumber; i++)
  124.    {
  125.       printf("%d ", player1Cards[i]);
  126.    }
  127.    printf("\n");
  128.    
  129.    
  130.     //Player 2 sorting
  131.    
  132.    for (i = 0; i < player2CardNumber; i++)
  133.    {
  134.       for (j = 0; j < (player2CardNumber - i - 1); j++)
  135.       {
  136.          if (player2Cards[j] < player2Cards[j + 1])
  137.          {
  138.             temp =player2Cards[j];
  139.             player2Cards[j] = player2Cards[j + 1];
  140.             player2Cards[j + 1] = temp;
  141.          }
  142.       }
  143.    }
  144.    printf("Player2's card sorted order is...\n");
  145.    for (i = 0; i < player2CardNumber; i++)
  146.    {
  147.       printf("%d ", player2Cards[i]);
  148.    }
  149.    printf("\n");
  150.    
  151.    
  152.     //FINDING THE WINNER       
  153.    
  154.     if(player1CardNumber < player2CardNumber)
  155.     {
  156.       maxRoundsToPlay = player1CardNumber;
  157.     }
  158.     else
  159.     {
  160.       maxRoundsToPlay = player2CardNumber;
  161.    }
  162.    
  163.    
  164.    printf("The game starts...\n");
  165.    
  166.    for(i=0; i<maxRoundsToPlay && winner==0; i++)
  167.    {
  168.       printf("Round %d. ",i+1);
  169.       if(player1Cards[i]>player2Cards[i])
  170.       {
  171.          winner = 1;
  172.          printf("Player1: %d > Player2: %d\n", player1Cards[i],player2Cards[i]);
  173.          
  174.       }
  175.       else if(player1Cards[i]<player2Cards[i])
  176.       {
  177.          winner = 2;
  178.          printf("Player1: %d < Player2: %d\n", player1Cards[i],player2Cards[i]);
  179.       }
  180.       else
  181.       {
  182.          printf("Player1: %d = Player2: %d\n", player1Cards[i],player2Cards[i]);
  183.       }
  184.     }
  185.    
  186.     if(winner == 1 || (winner == 0 && player1CardNumber > player2CardNumber))
  187.     {
  188.       if(winner == 0)
  189.       {
  190.          printf("Player2 ran out of cards\n");
  191.       }
  192.       printf("Player1 wins");
  193.     }
  194.     else if(winner == 2 || (winner == 0 && player1CardNumber < player2CardNumber))
  195.     {
  196.       if(winner == 0)
  197.       {
  198.          printf("Player1 ran out of cards\n");
  199.       }
  200.       printf("Player2 wins");
  201.     }
  202.     else
  203.     {
  204.       printf("It's a draw");
  205.     }
  206.    
  207.    
  208. }