Advertisement
Guest User

Untitled

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