Advertisement
Guest User

War

a guest
Dec 6th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6. #define NUMBER_OF_SUITS 4
  7. #define NUMBER_OF_FACES 13
  8. #define SHUFFLING_TIMES 5
  9.  
  10. typedef enum {
  11.     hearts = 1, diamonds, clubs, spades
  12. }card_suit_t;
  13.  
  14. typedef enum {
  15.     two = 2,
  16.     three,
  17.     four,
  18.     five,
  19.     six,
  20.     seven,
  21.     eight,
  22.     nine,
  23.     ten,
  24.     Jack,
  25.     Queen,
  26.     King,
  27.     Ace
  28. }card_face_t;
  29.  
  30. const card_suit_t card_suits[] = { hearts,diamonds,clubs,spades };
  31.  
  32. const card_face_t card_faces[] =
  33. {
  34.     two,
  35.     three,
  36.     four,
  37.     five,
  38.     six,
  39.     seven,
  40.     eight,
  41.     nine,
  42.     ten,
  43.     Jack,
  44.     Queen,
  45.     King,
  46.     Ace
  47. };
  48.  
  49. typedef struct {
  50.     card_face_t face;
  51.     card_suit_t suit;
  52. }card_t;
  53.  
  54. int SIZE_OF_DECK = 20;
  55. void Size_and_variant(int* size_of_deck, int* no_faces, int* variant);
  56. card_t Deck_builder(card_t deck[], int* no_faces);
  57. void shuffle(card_t DeckToShuffle[]);
  58. void Deal(card_t DeckToDeal[], card_t player_1_hand[], card_t player_2_hand[], int* front_p1, int* rear_p1, int* front_p2, int* rear_p2, int* p1_cards, int* p2_cards);
  59. void Add_To_Hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards);
  60. card_t Take_From_hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards);
  61. void game(card_t player_1_hand[], card_t player_2_hand[], card_t table[], int* front_table, int* rear_table, int* front_p1, int* rear_p1, int* front_p2, int* rear_p2, int* variant, int* p1cards, int* p2cards);
  62. void war_case(card_t player_1_hand[], card_t table[], card_t player_2_hand[], int* front_table, int* rear_table, int* front_p1, int* rear_p1, int* front_p2, int* rear_p2, int* p1cards, int* p2cards, int* war_count, int* round);
  63. void Change_faces(card_t card);
  64. void Change_colors(card_t card);
  65.  
  66. int main()
  67. {
  68.     card_t* Start_deck, * player_1_hand, * player_2_hand, * table;
  69.     int front_p1 = -1, rear_p1 = -1, front_p2 = -1, rear_p2 = -1, front_t = -1, rear_t = -1, no_faces = 5, variant, p1cards = 0, p2cards = 0;
  70.     Size_and_variant(&SIZE_OF_DECK, &no_faces, &variant);
  71.     Start_deck = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
  72.     if (Start_deck == NULL)
  73.     {
  74.         cout << "Malloc failed";
  75.         exit(0);
  76.     }
  77.     player_1_hand = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
  78.     if (player_1_hand == NULL)
  79.     {
  80.         cout << "Malloc failed";
  81.         exit(0);
  82.     }
  83.     player_2_hand = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
  84.     if (player_2_hand == NULL)
  85.     {
  86.         cout << "Malloc failed";
  87.         exit(0);
  88.     }
  89.     table = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
  90.     if (table == NULL)
  91.     {
  92.         cout << "Malloc failed";
  93.         exit(0);
  94.     }
  95.  
  96.     srand(time(NULL));
  97.     Deck_builder(Start_deck, &no_faces);
  98.     shuffle(Start_deck);
  99.     Deal(Start_deck, player_1_hand, player_2_hand, &front_p1, &rear_p1, &front_p2, &rear_p2, &p1cards, &p2cards);
  100.     /*Add_To_Hand(Start_deck[11], &front_p1, &rear_p1, player_1_hand, &p1cards);  Add_To_Hand(Start_deck[17], &front_p2, &rear_p2, player_2_hand, &p2cards);
  101.     Add_To_Hand(Start_deck[0], &front_p1, &rear_p1, player_1_hand, &p1cards);   Add_To_Hand(Start_deck[6], &front_p2, &rear_p2, player_2_hand, &p2cards);
  102.     Add_To_Hand(Start_deck[4], &front_p1, &rear_p1, player_1_hand, &p1cards);   Add_To_Hand(Start_deck[12], &front_p2, &rear_p2, player_2_hand, &p2cards);
  103.     Add_To_Hand(Start_deck[1], &front_p1, &rear_p1, player_1_hand, &p1cards);   Add_To_Hand(Start_deck[2], &front_p2, &rear_p2, player_2_hand, &p2cards);
  104.     Add_To_Hand(Start_deck[3], &front_p1, &rear_p1, player_1_hand, &p1cards);   Add_To_Hand(Start_deck[16], &front_p2, &rear_p2, player_2_hand, &p2cards);
  105.     Add_To_Hand(Start_deck[13], &front_p1, &rear_p1, player_1_hand, &p1cards);  Add_To_Hand(Start_deck[10], &front_p2, &rear_p2, player_2_hand, &p2cards);
  106.     Add_To_Hand(Start_deck[9], &front_p1, &rear_p1, player_1_hand, &p1cards);   Add_To_Hand(Start_deck[5], &front_p2, &rear_p2, player_2_hand, &p2cards);
  107.     Add_To_Hand(Start_deck[15], &front_p1, &rear_p1, player_1_hand, &p1cards);  Add_To_Hand(Start_deck[18], &front_p2, &rear_p2, player_2_hand, &p2cards);
  108.     Add_To_Hand(Start_deck[7], &front_p1, &rear_p1, player_1_hand, &p1cards);   Add_To_Hand(Start_deck[8], &front_p2, &rear_p2, player_2_hand, &p2cards);
  109.     Add_To_Hand(Start_deck[19], &front_p1, &rear_p1, player_1_hand, &p1cards);  Add_To_Hand(Start_deck[14], &front_p2, &rear_p2, player_2_hand, &p2cards);*/
  110.     free(Start_deck);
  111.     for (int i = 0;i < SIZE_OF_DECK;i++)
  112.     {
  113.         Change_faces(player_1_hand[i]);cout << " vs ";Change_faces(player_2_hand[i]);
  114.         cout << endl;
  115.     }
  116.     game(player_1_hand, player_2_hand, table, &front_t, &rear_t, &front_p1, &rear_p1, &front_p2, &rear_p2, &variant, &p1cards, &p2cards);
  117.     free(player_1_hand);
  118.     free(player_2_hand);
  119.     free(table);
  120. }
  121.  
  122. void shuffle(card_t DeckToShuffle[])
  123. {
  124.     int randomizer;
  125.     card_t helper;
  126.     for (int t = 0; t <= SHUFFLING_TIMES; t++)
  127.     {
  128.         for (int i = 0; i < SIZE_OF_DECK; i++)
  129.         {
  130.             randomizer = rand() % SIZE_OF_DECK;
  131.             helper = DeckToShuffle[i];
  132.             DeckToShuffle[i] = DeckToShuffle[randomizer];
  133.             DeckToShuffle[randomizer] = helper;
  134.  
  135.         }
  136.     }
  137. }
  138. card_t Deck_builder(card_t deck[], int* no_faces)
  139. {
  140.     int up = 0;
  141.     for (int face = 12; face > (12 - *no_faces); face--)
  142.     {
  143.         for (int suit = 0; suit < NUMBER_OF_SUITS; suit++, up++)
  144.         {
  145.             deck[up].face = card_faces[face];
  146.             deck[up].suit = card_suits[suit];
  147.         }
  148.     }
  149.     return deck[SIZE_OF_DECK];
  150. }
  151. void Deal(card_t DeckToDeal[], card_t player_1_hand[], card_t player_2_hand[], int* front_p1, int* rear_p1, int* front_p2, int* rear_p2, int* p1_cards, int* p2_cards)
  152. {
  153.     *front_p1 = -1;
  154.     *front_p2 = -1;
  155.     *rear_p1 = -1;
  156.     *rear_p2 = -1;
  157.     card_t tmp;
  158.     for (int i = 0; i < SIZE_OF_DECK; i++)
  159.     {
  160.         if (i < SIZE_OF_DECK / 2)
  161.         {
  162.             tmp = DeckToDeal[i];
  163.             Add_To_Hand(tmp, front_p1, rear_p1, player_1_hand, p1_cards);
  164.         }
  165.         else
  166.         {
  167.             tmp = DeckToDeal[i];
  168.             Add_To_Hand(tmp, front_p2, rear_p2, player_2_hand, p2_cards);
  169.         }
  170.     }
  171. }
  172. void Add_To_Hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards)
  173. {
  174.     (*p_cards) += 1;
  175.     if (*front_of_player == -1)
  176.     {
  177.         *front_of_player = 0;
  178.     }
  179.     *rear_of_player = (*rear_of_player + 1) % SIZE_OF_DECK;
  180.     player_hand[*rear_of_player] = card;
  181. }
  182. card_t Take_From_hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards)
  183. {
  184.     (*p_cards) -= 1;
  185.     card = player_hand[*front_of_player];
  186.     *front_of_player = (*front_of_player + 1) % SIZE_OF_DECK;
  187.     return(card);
  188. }
  189. void Put_on_table(card_t card, int* front_table, int* rear_table, card_t table[])
  190. {
  191.     if (*front_table == -1)
  192.     {
  193.         *front_table = 0;
  194.     }
  195.     *rear_table = (*rear_table + 1) % SIZE_OF_DECK;
  196.     table[*rear_table] = card;
  197. }
  198.  
  199. card_t Take_from_table(card_t card, int* front_table, int* rear_table, card_t table[])
  200. {
  201.     if (*front_table == *rear_table)
  202.     {
  203.         *front_table = -1;
  204.         *rear_table = -1;
  205.     }
  206.     else
  207.     {
  208.         card = table[*front_table];
  209.         *front_table = (*front_table + 1) % SIZE_OF_DECK;
  210.         return(card);
  211.     }
  212. }
  213. int isEmpty(int* front)
  214. {
  215.     if (*front == -1) return 1;
  216.     return 0;
  217. }
  218. void game(card_t player_1_hand[], card_t player_2_hand[], card_t table[], int* front_table, int* rear_table, int* front_p1, int* rear_p1, int* front_p2, int* rear_p2, int* variant, int* p1cards, int* p2cards)
  219. {
  220.     int war_count;
  221.     int round = 0;
  222.     while (true)
  223.     {
  224.         if ((*p1cards >= SIZE_OF_DECK || *p2cards >= SIZE_OF_DECK) || (*p1cards <= 0 || *p2cards <= 0))
  225.             break;
  226.         else
  227.         {
  228.             war_count = 0;
  229.             cout << endl << endl << *p1cards << "    " << *p2cards << endl << "rounds :" << round << endl;//
  230.             Change_faces(player_1_hand[*front_p1]);
  231.             Change_colors(player_1_hand[*front_p1]);
  232.             cout << "    vs    ";
  233.             Change_faces(player_2_hand[*front_p2]);
  234.             Change_colors(player_2_hand[*front_p2]);
  235.             cout << endl;//
  236.  
  237.             if (player_1_hand[*front_p1].face > player_2_hand[*front_p2].face)
  238.             {
  239.                 Add_To_Hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards);
  240.                 Add_To_Hand(player_2_hand[*front_p2], front_p1, rear_p1, player_1_hand, p1cards);
  241.                 Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards);
  242.                 Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards);
  243.                 round++;
  244.             }
  245.             else if (player_1_hand[*front_p1].face < player_2_hand[*front_p2].face)
  246.             {
  247.                 Add_To_Hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards);
  248.                 Add_To_Hand(player_1_hand[*front_p1], front_p2, rear_p2, player_2_hand, p2cards);
  249.                 Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards);
  250.                 Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards);
  251.                 round++;
  252.             }
  253.             else if (player_1_hand[*front_p1].face == player_2_hand[*front_p2].face)
  254.             {
  255.                 cout << "==========war==========" << endl;
  256.                 war_count++;
  257.                 war_case(player_1_hand,table,player_2_hand,front_table,rear_table,front_p1,rear_p1,front_p2,rear_p2,p1cards,p2cards,&war_count,&round);
  258.                 cout << "==========end of war==========" << endl;
  259.             }
  260.         }
  261.     }
  262.     if (*p1cards <= 0)
  263.         cout << "p2 won game ! " << "rounds: " << round << endl;
  264.     else
  265.         cout << "p1 won game ! " << "rounds: " << round << endl;
  266. }
  267. void war_case(card_t player_1_hand[], card_t table[], card_t player_2_hand[], int* front_table, int* rear_table, int* front_p1, int* rear_p1, int* front_p2, int* rear_p2, int* p1cards, int* p2cards, int* war_count, int* round)
  268. {
  269.     if ((*p1cards >= 3) && (*p2cards >= 3))
  270.     {
  271.         Put_on_table(Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards), front_table, rear_table, table);//fighting card
  272.         Put_on_table(Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards), front_table, rear_table, table);//fighting card
  273.         Put_on_table(Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards), front_table, rear_table, table);//blank1
  274.         Put_on_table(Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards), front_table, rear_table, table);//blank2
  275.  
  276.         if (player_1_hand[*front_p1].face > player_2_hand[*front_p2].face)
  277.         {
  278.             Put_on_table(Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards), front_table, rear_table, table);
  279.             Put_on_table(Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards), front_table, rear_table, table);
  280.             while (isEmpty(front_table) != 1)
  281.             {
  282.                 cout << *front_table << endl;
  283.                 cout << "added: " << table[*front_table].face << endl;
  284.                 Add_To_Hand(Take_from_table(table[*front_table], front_table, rear_table, table), front_p1, rear_p1, player_1_hand, p1cards);
  285.  
  286.             }
  287.         }
  288.         else if (player_1_hand[*front_p1].face < player_2_hand[*front_p2].face)
  289.         {
  290.             Put_on_table(Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards), front_table, rear_table, table);
  291.             Put_on_table(Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards), front_table, rear_table, table);
  292.             while (isEmpty(front_table) != 1)
  293.             {
  294.                 cout << *front_table << endl;
  295.                 cout << "added: " << table[*front_table].face << endl;
  296.                 Add_To_Hand(Take_from_table(table[*front_table], front_table, rear_table, table), front_p2, rear_p2, player_2_hand, p2cards);
  297.             }
  298.         }
  299.         else if (player_1_hand[*front_p1].face == player_2_hand[*front_p2].face)
  300.         {
  301.             cout << "next war" << endl;
  302.             (*round) += 3;
  303.             war_case(player_1_hand, table, player_2_hand, front_table, rear_table, front_p1, rear_p1, front_p2, rear_p2, p1cards, p2cards, war_count, round);
  304.         }
  305.     }
  306.     else
  307.     {
  308.         if (*p1cards < 3)
  309.         {
  310.             (*round) += *p1cards;
  311.             (*p2cards) += (*p1cards);
  312.             (*p1cards) = 0;
  313.             return;
  314.         }
  315.         else if (*p2cards < 3)
  316.         {
  317.             (*round) += *p2cards;
  318.             (*p1cards) += (*p2cards);
  319.             (*p2cards) = 0;
  320.             return;
  321.         }
  322.     }
  323. }
  324. void Change_faces(card_t card)
  325. {
  326.     if (card.face == 2)
  327.         cout << "Two of";
  328.     else if (card.face == 3)
  329.         cout << "Three of";
  330.     else if (card.face == 4)
  331.         cout << "Four of";
  332.     else if (card.face == 5)
  333.         cout << "Five of";
  334.     else if (card.face == 6)
  335.         cout << "Six of";
  336.     else if (card.face == 7)
  337.         cout << "Seven of";
  338.     else if (card.face == 8)
  339.         cout << "Eight of";
  340.     else if (card.face == 9)
  341.         cout << "Nine of";
  342.     else if (card.face == 10)
  343.         cout << "Ten of";
  344.     else if (card.face == 11)
  345.         cout << "Jack of";
  346.     else if (card.face == 12)
  347.         cout << "Queen of";
  348.     else if (card.face == 13)
  349.         cout << "King of";
  350.     else if (card.face == 14)
  351.         cout << "Ace of";
  352. }
  353. void Change_colors(card_t card)
  354. {
  355.     if (card.suit == 1)
  356.         cout << " hearts";
  357.     else if (card.suit == 2)
  358.         cout << " diamonds";
  359.     else if (card.suit == 3)
  360.         cout << " clubs";
  361.     else if (card.suit == 4)
  362.         cout << " spades";
  363. }
  364. void Size_and_variant(int* size_of_deck, int* no_faces, int* variant)
  365. {
  366.     char size, var = 1;
  367.     cout << "Choose size of deck" << endl;
  368.     cout << "1.A-10" << endl
  369.         << "2.A-9" << endl
  370.         << "3.A-8" << endl
  371.         << "4.A-7" << endl
  372.         << "5.A-6" << endl
  373.         << "6.A-5" << endl
  374.         << "7.A-4" << endl
  375.         << "8.A-3" << endl
  376.         << "9.A-2" << endl;
  377.     cin >> size;
  378.     cout << "=================================" << endl;
  379.     cout << "choose variant of the game" << endl;
  380.     cout << "1.A" << endl
  381.         << "2.B" << endl;
  382.     cin >> var;
  383.     switch (var)
  384.     {
  385.     case '1':
  386.         *variant = 1;
  387.         break;
  388.     case '2':
  389.         *variant = 2;
  390.         break;
  391.     }
  392.     switch (size)
  393.     {
  394.     case '1':
  395.     {
  396.         *size_of_deck = 20;
  397.         *no_faces = 5;
  398.     }break;
  399.     case '2':
  400.     {
  401.         *size_of_deck = 24;
  402.         *no_faces = 6;
  403.     }break;
  404.     case '3':
  405.     {
  406.         *size_of_deck = 28;
  407.         *no_faces = 7;
  408.     }break;
  409.     case '4':
  410.     {
  411.         *size_of_deck = 32;
  412.         *no_faces = 8;
  413.     }break;
  414.     case '5':
  415.     {
  416.         *size_of_deck = 36;
  417.         *no_faces = 9;
  418.     }break;
  419.     case '6':
  420.     {
  421.         *size_of_deck = 40;
  422.         *no_faces = 10;
  423.     }break;
  424.     case '7':
  425.     {
  426.         *size_of_deck = 44;
  427.         *no_faces = 11;
  428.     }break;
  429.     case '8':
  430.     {
  431.         *size_of_deck = 48;
  432.         *no_faces = 12;
  433.     }break;
  434.     case '9':
  435.     {
  436.         *size_of_deck = 52;
  437.         *no_faces = 13;
  438.     }break;
  439.     }
  440. }
  441. void Show_blank_card(card_t card, card_t card2)
  442. {
  443.     cout << "First Player`s blank card: ";Change_faces(card);Change_colors(card);
  444.     cout << endl;
  445.     cout << "Second Player`s blank card : ";Change_faces(card2);Change_colors(card2);
  446.     cout << endl;
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement