<mosaic.cnfolio.com>
PROGRAM
  1. /*START
  2.    Declare integer called P1Cardsnum and P2Cardsnum
  3.    Declare arrays Player1[10] and Player2[10]
  4.    Declare integers integers i and j
  5.    Declare integers temp and counter
  6.    Declare and SET integer AllCards to ZERO
  7.    Declare integers CardsRemainedP1 and CardsRemainedP2
  8.    Get user input for P1Cardsnum
  9.    Set CardsRemainedP1 to P1Cardsnum
  10.    IF Player1 input is less than TWO or greater than TEN
  11.       Display Error Message
  12.       TERMINATE Program
  13.    END IF
  14.    SET i to ZERO and REPEAT as long as i is less than P1Cardsnum
  15.       Get input for array Player1[i]
  16.       IF user input is less than ONE or greater than TEN
  17.          Display Error Message
  18.          TERMINATE Program
  19.       END IF
  20.       increment i
  21.    END REPEAT
  22.    SET i to ZERO and REPEAT as long as i is less than P1Cardsnum
  23.       SET j to i and increment by ONE and REPEAT as long as j is less than P1Cardsnum
  24.          IF Player1[i] is less than Player1[j]
  25.             SET temp to Player1[i]
  26.             SET Player1[i] to Player1[j]
  27.             SET Player1[j] to temp
  28.          END IF
  29.          increment j
  30.        END REPEAT
  31.       increment i
  32.    END REPEAT
  33.    Get user input for P2Cardsnum
  34.    Set CardsRemainedP2 to P2Cardsnum
  35.    IF Player2 input less than TWO or greater than TEN
  36.       Display Error Message
  37.       TERMINATE Program
  38.    END IF
  39.    SET i to ZERO and REPEAT as long as i is less than P2Cardsnum
  40.       Get input for array Player2[i]
  41.       IF user input is less than ONE or greater than TEN
  42.          Display Error Message
  43.          TERMINATE Program
  44.       END IF
  45.       increment i
  46.    END REPEAT
  47.    SET i to ZERO and REPEAT as long as i is less than P2Cardsnum
  48.       SET j to i and increment by ONE and REPEAT as long as j is less than P2Cardsnum
  49.          IF Player2[i] is less than Player2[j]
  50.             SET temp to Player2[i]
  51.             SET Player2[i] to Player2[j]
  52.             SET Player2[j] to temp
  53.          END IF
  54.          increment j
  55.        END REPEAT
  56.       increment i
  57.    END REPEAT
  58.    REPEAT 10 times
  59.       IF Player1 and Player2 in a turn, have same cards
  60.          Decrement one from Player1 total of card numbers
  61.          Decrement one from Player 2 total of card numbers
  62.          IF Player1 cards is finished
  63.             Player1 is assumed to have card with value ZERO
  64.          END IF
  65.          IF Player2 cards is finished
  66.             Player2 is assumed to have card with value ZERO
  67.          END IF
  68.       IF Player1 card is greater than Player2 card in a turn
  69.          DISPLAY Player1 wins
  70.          TERMINATE Program
  71.       END IF
  72.       IF Player2 card is greater than Player1 card in a turn
  73.          DISPLAY Player2 wins
  74.          TERMINATE Program
  75.       END IF
  76.       IF Player1 card equals Player 2 card in a turn AND total of cards of two players are EQUAL
  77.          Increment AllCards
  78.          IF all cards in both players hands have been checked
  79.             DISPLAY DRAW
  80.             TERMINATE Program
  81.          END IF
  82.       END IF
  83.    END REPEAT
  84. END*/
  85. #include <stdio.h>
  86.                                                        
  87. int main()
  88. {
  89.    int P1Cardsnum, P2Cardsnum;               //the number of cards in each players hand
  90.    int Player1[10], Player2[10];     //the value of cards for each player
  91.    int i, j;                                 //loop variables
  92.    int temp, counter;                        //counter used for loop
  93.    int AllCards=0;                           //variable used to make sure in case of a draw, program has gone through all the cards
  94.    int CardsRemainedP1, CardsRemainedP2;     //variables used to determine which player won the game when one player runs out of cards
  95.  
  96.    
  97.    scanf("%d",&P1Cardsnum);                  //the number of cards in player 1 hand
  98.    CardsRemainedP1= P1Cardsnum;              //the number of cards left in player 1 hand initially is equal to the number of cards player 1 has
  99.    if (P1Cardsnum<2 || P1Cardsnum>10){   //displays errer when user input for the number of cards is less than 2 or greater than 10
  100.       printf("\nError! Player 1, you can have between 2 to 10 cards in your hand.\n");
  101.       return;                      //program terminates in case of error
  102.    }
  103.    for (i = 0 ; i < P1Cardsnum; i++){      //user input loop for the value of cards in player 1 hand
  104.     scanf("%d", &Player1[i]);
  105.       if (Player1[i]<1 || Player1[i]>10){  //displays error when the value of card entered is less than 1 and greater than 10
  106.          printf("\nError! Player1, you can have %d cards with values 1 to 10 in your hand",P1Cardsnum);
  107.          return;                    //terminates program in case of error
  108.          }
  109.    }
  110.    for(i=0; i<P1Cardsnum; i++){          //sorting cards in player 1 hand loop
  111.         for(j=i+1; j<P1Cardsnum; j++)
  112.         {
  113.             if(Player1[i] < Player1[j])
  114.             {
  115.                 temp     = Player1[i];
  116.                 Player1[i] = Player1[j];
  117.                 Player1[j] = temp;
  118.             }
  119.         }
  120.    }
  121.    scanf("%d",&P2Cardsnum);                  //the number of cards in player 2 hand
  122.    CardsRemainedP2= P2Cardsnum;      //the number of cards left in player 2 hand initially is equal to the number of cards player 2 has
  123.    if (P2Cardsnum<2 || P2Cardsnum>10){   //displays errer when user input for the number of cards is less than 2 or greater than 10
  124.       printf("\nError! Player 2, you can have between 2 to 10 cards in your hand.\n");
  125.       return;                      //program terminates in case of error
  126.       }
  127.    for (i = 0 ; i < P2Cardsnum; i++){
  128.     scanf("%d", &Player2[i]);               //user input loop for the value of cards in player 2 hand
  129.       if (Player2[i]<1 || Player2[i]>10){
  130.          printf("\nError! Player2, you can have %d cards with values 1 to 10 in your hand",P2Cardsnum);
  131.          return;                    //program terminates in case of error
  132.       }
  133.    }
  134.     for(i=0; i<P2Cardsnum; i++){     //sorting cards in player 2 hand loop
  135.         for(j=i+1; j<P2Cardsnum; j++)
  136.         {
  137.             if(Player2[i] < Player2[j])
  138.             {
  139.                 temp     = Player2[i];
  140.                 Player2[i] = Player2[j];
  141.                 Player2[j] = temp;
  142.             }
  143.         }
  144.    } 
  145.    for (counter=0 ; counter<10; counter ++){     //loop could continue to the max number of cards
  146.       if(Player1[counter]==Player2[counter]){            //if in a turn two cards are equal, that means each player will have one card less in the next turn
  147.          CardsRemainedP1= CardsRemainedP1-1;
  148.          CardsRemainedP2= CardsRemainedP2-1;
  149.          if(CardsRemainedP1==0)
  150.             Player1[counter]= 0;         //Player 1 cards finished
  151.          if(CardsRemainedP2==0)
  152.             Player2[counter]= 0;         //Player 2 Cards finished
  153.       }
  154.       if(Player1[counter]>Player2[counter]){     //if in a turn player 1 has a card of higher value than player 2, player 1 wins
  155.          printf("\nPlayer 1 wins.");
  156.          return;
  157.       }
  158.       if(Player2[counter]>Player1[counter]){     //if in a turn player 2 has a card of higher value than player 1, player 2 wins
  159.          printf("\nPlayer 2 wins.");
  160.          return;
  161.       }
  162.       if(Player2[counter]==Player1[counter] && P1Cardsnum==P2Cardsnum){ //condition makes sure both players have same amout of cards and that the value of each card is equal to the other player
  163.          AllCards++;             //this will help make sure the program does not display draw as soon as two cards are equal
  164.          if(P1Cardsnum==AllCards || P2Cardsnum==AllCards){         //display draw only if all the cards in each player hands are equal to each other
  165.             printf("\nDRAW!");
  166.             return;
  167.          }
  168.       }
  169.     }
  170. }