Advertisement
Ponnyou

War.c

Nov 11th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. /*
  2. Author: Bradley Felix, bfelix2019@my.fit.edu
  3. Course: CSE 1001, Section E3
  4. Filename: war.c
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11.  
  12. void shuffle(int *, int); //Used to shuffle the deck
  13. void deal_hand(int *, int *, int *);
  14. int check_hand(int, int);
  15. void card_declare(int *, int);
  16. void victory_royale(int *, int *, int , int);
  17. void loser_royale(int *, int *, int, int);
  18. void victory_royale_2(int *, int *, int, int);
  19. void shrink_growth(int *, int *, int, int, int, int);
  20.  
  21.  
  22. int main(void)
  23. {
  24.     int war_deck[52]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
  25.     int j = 26;
  26.     int p1_growth=0, p2_growth=0;
  27.     int p1_shrink=0, p2_shrink=0;
  28.     int p1_hand[104],p2_hand[104];
  29.     char p_e; // Play or exit
  30.     int p1_hand_length = sizeof(p1_hand) / sizeof(p1_hand[0]);
  31.     int p2_hand_length = sizeof(p2_hand) / sizeof(p2_hand[0]);
  32.  
  33.     printf("Welcome to War!\n");
  34.  
  35.     printf("Shuffling the deck...\n");
  36.     shuffle(war_deck, 52);
  37.     for (int i = 0; i < 52; i++){ // Printing out the war deck to make sure it shuffles correctly <Will remove after>
  38.         printf("%d ", war_deck[i]);
  39.     }
  40.  
  41.     printf("\nDealing out the cards...\n");
  42.     deal_hand(war_deck, p1_hand, p2_hand); // Printing out the delt cards <Will remove after>
  43.  
  44.     printf("\nWould you like to play(p) or exit(e)?: ");
  45.     scanf(" %c", &p_e);
  46.  
  47.     for(int i=0;i<52;i++){
  48.         printf("i incriment: %d\n", i);
  49.         printf("Player 1 hand: %d\n", p1_hand[i]);
  50.         printf("Player 2 hand: %d\n", p2_hand[j]); // Using j because p2_hand starts on the 26th value
  51.         if (p_e=='e' || p_e=='E'){
  52.             if(p1_hand>0 && p2_hand>0){
  53.                 printf("The game is a tie!\n");
  54.             }
  55.             printf("Goodbye! Thanks for playing War!");
  56.             break;
  57.         }
  58.         if (p_e=='p' || p_e=='P'){ // If the player chooses to play
  59.             printf("Player 1's Card: ");
  60.             card_declare(p1_hand, i);
  61.             printf("Player 2's Card: ");
  62.             card_declare(p2_hand, j);
  63.             if(p1_hand[i] > p2_hand[j]){ // If player one wins the battle
  64.                 printf("Player 1 wins the round!\n");
  65.                 printf("Awarding played cards to Player 1...\n");
  66.                 victory_royale(p1_hand, p2_hand, i, j);
  67.                 p1_growth++;
  68.                 p2_growth++;
  69.                 shrink_growth(p1_hand, p2_hand, i, j, p1_hand_length, p2_hand_length);
  70.                
  71.                 for (int i = 0; i < 26; i++){
  72.                     printf("%d ", p1_hand[i+p1_growth]); // Player one hand prints out their cards with the card they won
  73.                 }
  74.                
  75.                 printf("\n\n");
  76.                
  77.                 for (int i = 26; i < 52 ; i++){
  78.                     printf("%d ", p2_hand[i+p2_growth]); // Player two hand prints out their cards with the card they won
  79.                 }
  80.                 printf("\n");
  81.                
  82.             } else if(p2_hand[j] > p1_hand[p1_growth]){ // If player two wins the battle
  83.                 printf("Player 2 wins the round!\n");
  84.                 printf("Awarding played cards to Player 2...\n");
  85.                 victory_royale_2(p2_hand, p1_hand, i, j);
  86.                 p1_growth++;
  87.                 p2_growth++;
  88.                 shrink_growth(p2_hand, p1_hand, j, i, p2_hand_length, p1_hand_length);
  89.                 for (int i = 0; i < 26; i++){
  90.                     printf("%d ", p1_hand[i+p1_growth]);
  91.                 }
  92.                 printf("\n\n");
  93.                 for (int i = 26; i < 52 ; i++){
  94.                     printf("%d ", p2_hand[i+p2_growth]);
  95.                 }
  96.                 printf("\n");
  97.                
  98.             } else if(p1_hand[i] == p2_hand[j]){
  99.                 printf("The cards are equal! Delcaring WAR!\n");
  100.                 printf("Drawing the top 3 cards from both players decks for battle...\n");
  101.             }
  102.         }
  103.         printf("\nWould you like to play(p) or exit(e)?: ");
  104.         scanf(" %c", &p_e);
  105.         j++; // Using to incriment instead of i so p2_hand incriments at the same time with p1_hand
  106.     }
  107. }
  108.  
  109. void victory_royale(int *winner, int *loser, int increment, int increment_2){
  110.     winner[increment+26] = winner[increment]; // Hands the winner back their played card first
  111.     winner[increment+27] = loser[increment_2]; // Hands the winner the card they got from the loser
  112.     loser[increment_2+26] = loser[increment_2]; // Loser's card gets sent to the back of their deck <WIP>
  113.  
  114. }
  115.  
  116. void victory_royale_2(int *winner, int *loser, int increment, int increment_2 ){
  117.     winner[increment+52] = winner[increment]; // Hands the winner back their played card first
  118.     winner[increment+53] = loser[increment_2]; // Hands the winner the card they got from the loser
  119.     loser[increment] = loser[increment+26]; // Loser's card gets sent to the back of their deck <WIP>
  120.  
  121. }
  122.  
  123.  
  124. void shrink_growth(int *winner, int *loser, int increment, int increment_2, int len, int len2){ // <WIP> Deletes the last card they played by reducing the array size
  125.     loser[increment] = loser[increment-1];
  126. }
  127.  
  128. void card_declare(int *p_hand, int increment){
  129.   char suit[4]={'C','S','D','H'};
  130.   char c_value[4]={'J','Q','K','A'};
  131.   char card_suite;
  132.   char card_value_face;
  133.   int card_value_num;
  134.   // Declaring the card suite
  135.   if(p_hand[increment]<13)
  136.   {
  137.     card_suite=suit[1];
  138.   } else
  139.   if(p_hand[increment]>=13 && p_hand[increment]<26)
  140.   {
  141.     card_suite=suit[2];
  142.   } else
  143.   if(p_hand[increment]>=26 && p_hand[increment]<39)
  144.   {
  145.     card_suite=suit[3];
  146.   } else
  147.   if(p_hand[increment]>=39 && p_hand[increment]<52)
  148.   {
  149.     card_suite=suit[0];
  150.   }
  151.  
  152.   //  Declaring the card value
  153.   if(p_hand[increment]%13<=8)
  154.   {
  155.     card_value_num=(p_hand[increment]%13)+2;
  156.     printf("%d", card_value_num);
  157.   } else
  158.   if(p_hand[increment]%13==9)
  159.   {
  160.     card_value_face=c_value[0];
  161.     printf("%c", card_value_face);
  162.   } else
  163.   if(p_hand[increment]%13==10)
  164.   {
  165.     card_value_face=c_value[1];
  166.     printf("%c", card_value_face);
  167.   } else
  168.   if(p_hand[increment]%13==11)
  169.   {
  170.     card_value_face=c_value[2];
  171.     printf("%c", card_value_face);
  172.   } else
  173.   if(p_hand[increment]%13==12)
  174.   {
  175.     card_value_face=c_value[3];
  176.     printf("%c", card_value_face);
  177.   }
  178.     printf("%c",card_suite);
  179.   printf("\n");
  180. }
  181.  
  182.  
  183. void deal_hand(int *in_deck, int *hand1, int *hand2){
  184.   int dealt,dealt2;
  185.   // Splitting the deck into two seperate loops, the loops represent the hands
  186.   for (int i=0;i<26;i++)
  187.   {
  188.     dealt=in_deck[i];
  189.     hand1[i]=dealt;
  190.     printf("%d ", hand1[i]);
  191.   }
  192.   printf("\n\n");
  193.  
  194.   for (int i=26;i<52;i++)
  195.   {
  196.     dealt2=in_deck[i];
  197.     hand2[i]=dealt2;
  198.     printf("%d ", hand2[i]);
  199.   }
  200.  
  201. }
  202.  
  203.  
  204. void shuffle(int *in_deck, int n){
  205.     srand((unsigned) time(NULL));
  206.     if (n > 1) {
  207.         int i, k, t;
  208.         for (i = 0; i < (n - 1); i++) {
  209.             k = i + rand() / (RAND_MAX / (n - i) + 1);
  210.             t = in_deck[k];
  211.             in_deck[k] = in_deck[i];
  212.             in_deck[i] = t;
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement