<mosaic.cnfolio.com>
PROGRAM
1. /*&Create needed variables.
2. Scan the first input and save it
3. Create an array for player 1, which is equal to the size of the first input
4. Create a loop that saves the numbers of the cards to the array until the number of cards is reached
5. Scan for the next input (number of cards in players 2s hand) and save it
6. Create an array for player 2, which is equal to the size of their number of cards
7. Create another loop that saves the numbers of the cards to the array until the number of cards is reached
8. Check that the number of cards isn’t too high/low
9. Check that the cards entered aren’t too high/low
10. Create a loop for player 1 that sorts the cards into descending order
11. Check each number one by one using a bubble sort, moving the smaller number to the right
12. Create a loop for player 2 that sorts the cards into descending order
13. Again checking each number one by one using a bubble sort, moving the smaller number to the right
14. Create a criteria where the player with more cards wins
15. If both players have the same number of cards, it’s a draw
16. Save this as the result
17. Then compare each card, and if one card is higher than the other, the owner of that card wins, overwrite result with this
18. Print the result*/
19.
20. #include <stdio.h>
21.
22. int main( void )
23. {
24.    int number1, number2, i, j, temp, c;   /*Creating all integer variables*/
25.    char *result;                          /*Creating a string for the result*/
26.    scanf( "%d", &number1 );           /*Scanning line 1 in the input for a double(number of cards in player 1's hand) to store*/
27.                                           /*as number1*/
28.    int player1[number1];          /*Creating an integer array called "player1" of length number1 to store player 1s hand*/
29.
30.    for (i=0; i<number1; i++)      /*A loop that itterates from i=0 to i=number1-1, with i being the number of cards stored*/
31.    {              /*(checked)*/
32.       scanf( "%d", &player1[i] );     /*For each i, it reads the next unstored number and stores it in position i of array player1*/
33.    }
34.
35.    scanf( "%d", &number2 );         /*Scanning line 3 in the input for a double(number of cards in player 2's hand) to store*/
36.                                           /*as number2*/
37.    int player2[number2];                  /*Creating an integer array called "player2" of length number2 to store player 2s hand*/
38.
39.    for (i=0; i<number2; i++)      /*A loop that itterates from i=0 to i=number2-1, with i being the number of cards stored*/
40.    {              /*(checked)*/
41.       scanf( "%d", &player2[i] );     /*For each i, it reads the next unstored number and stores it in position i of array player2*/
42.    }
43.
44.    if (number1>10 | number2>10)        /*In the case that player 1 or 2s hand has more than 10 cards, give an error message and*/
45.    {              /*close the program*/
46.       printf("Error: No more than 10 cards allowed");
47.       return
48.    }
49.
50.    if (number1<2 | number2<2)     /*In the case that player 1 or 2s hand has less than 2 cards, give an error message and*/
51.    {                          /*close the program*/
52.       printf("Error: You must have atleast 2 cards");
53.       return;
54.    }
55.
56.    for (i=0; i < number1; i++)         /*This checks all cards in player 1s hand*/
57.    {
58.       if (player1[i] > 10)      /*If the current card number is more than 10, give an error and close the program*/
59.       {
60.          printf("Error: Player 1s card number %d is higher than 10, please enter a number 10 or lower", i+1);
61.          return;
62.       }
63.
64.       if (player1[i] < 1)                  /*If the current card number is less than 1, give an error and close the program*/
65.       {
66.          printf("Error: Player 1s card number %d is lower than 1, please enter a number 1 or higher", i+1);
67.          return;
68.       }
69.    }
70.
71.    for (i=0; i < number2; i++)            /*This checks all cards in player 2s hand*/
72.    {
73.       if (player2[i] > 10)      /*If the current card number 2s more than 10, give an error and close the program*/
74.       {
75.          printf("Error: Player 2s card number %d is higher than 10, please enter a number 10 or lower", i+1);
76.          return;
77.       }
78.       if (player2[i] < 1)            /*If the current card number 2s less than 1, give an error and close the program*/
79.       {
80.          printf("Error: Player 2s card number %d is lower than 1, please enter a number 1 or higher", i+1);
81.          return;
82.       }
83.    }
84.
85.    for (i=0; i < (number1-1); i++)        /*i is the amount of cards that have been sorted. Once all but one of the cards have been*/
86.    {                          /*sorted, end the loop (as a list of 1 is sorted)*/
87.       for (j=0; j < (number1-1-i); j++)   /*j is the number of cards that have been checked so far. This runs until all unsorted*/
88.       {                                   /*cards have been looked at.*/
89.         if (player1[j] < player1[j+1])   /*Tests if the current card is smaller than the next card, and switches them (below) if it*/
90.          {                                /*does*/
91.             temp = player1[j+1];          /*Stores the next card in the temporary variable*/
92.             player1[j+1] = player1[j];    /*Overides the next cards position with the current card*/
93.             player1[j] = temp;            /*Overides the current cards position with the temporary variable (next card)*/
94.          }
95.       }
96.    }
97.
98.    for (i=0; i < (number2-1); i++)        /*i is the amount of cards that have been sorted. Once all but one of the cards have been*/
99.    {                                      /*sorted, end the loop (as a list of 1 is sorted)*/
100.       for (j=0; j < (number2-1-i); j++)   /*j is the number of cards that have been checked so far. This runs until all unsorted*/
101.       {                                   /*cards have been looked at.*/
102.         if (player2[j] < player2[j+1])   /*Tests if the current card is smaller than the next card, and switches them (below) if it*/
103.          {                                /*does*/
104.             temp = player2[j+1];          /*Stores the next card in the temporary variable*/
105.             player2[j+1] = player2[j];    /*Overides the next cards position with the current card*/
106.             player2[j] = temp;            /*Overides the current cards position with the temporary variable (next card)*/
107.          }
108.       }
109.    }
110.
111.    if (number1>number2)                   /*This is to check to make sure that the number of times checked isn't higher than the*/
112.    {                                      /*minimum number of cards*/
113.       c=number2;                          /*This sets c as number2, as it represents the minimum hand size*/
114.       result = "Player 1 wins";           /*This is the result in the case that all cards checked are equal if player 1 has more cards*/
115.    }
116.
117.    if (number2>number1)
118.    {
119.       c=number1;                          /*This sets c as number1, as it represents the minimum hand size*/
120.       result = "Player 2 wins";           /*This is the result in the case that all cards checked are equal if player 2 has more cards*/
121.    }
122.
123.    if (number2==number1)
124.    {
125.       c=number1;                          /*This sets c as number1, as it represents the minimum hand size (could be number2)*/
126.       result = "Draw";                    /*This is the result in the case that all cards checked are equal if both players have the*/
127.    }                                      /*same number of cards.*/
128.
129.    for (i=0; i<c; i++)                    /*Check each hand until the minimum hand size has been reached*/
130.    {
131.       if (player1[i]>player2[i])          /*In the scenario that player 1s card is higher than player 2s*/
132.       {
133.          result = "Player 1 wins";        /*Saves the result as 'Player 1 wins'*/
134.          break;                           /*Exits loop*/
135.       }
136.
137.       if (player2[i]>player1[i])          /*In the scenario that player 2s card is higher than player 2s*/
138.       {
139.          result = "Player 2 wins";        /*Saves the result as 'Player 2 wins'*/
140.          break;                           /*Exits loop*/
141.       }
142.    }
143.    printf( "%s", result);                  /*This prints the result of the card game*/
144. }