Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- #define NUMBER_OF_SUITS 4
- #define NUMBER_OF_FACES 13
- #define SHUFFLING_TIMES 5
- #define MOD(a,b) ((((a)%(b))+(b))%(b))
- typedef enum {
- hearts = 1, diamonds, clubs, spades
- }card_suit_t;
- typedef enum {
- two = 2,
- three,
- four,
- five,
- six,
- seven,
- eight,
- nine,
- ten,
- Jack,
- Queen,
- King,
- Ace
- }card_face_t;
- const card_suit_t card_suits[] = { hearts,diamonds,clubs,spades };
- const card_face_t card_faces[] =
- {
- two,
- three,
- four,
- five,
- six,
- seven,
- eight,
- nine,
- ten,
- Jack,
- Queen,
- King,
- Ace
- };
- typedef struct {
- card_face_t face;
- card_suit_t suit;
- }card_t;
- int SIZE_OF_DECK =20;
- void Size_and_variant(int* size_of_deck, int* no_faces, int* variant);
- card_t Deck_builder(card_t deck[], int* no_faces);
- void shuffle(card_t DeckToShuffle[]);
- 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);
- void Add_To_Hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards);
- card_t Take_From_hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards);
- 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);
- 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);
- void Change_faces(card_t card);
- void Change_colors(card_t card);
- int main()
- {
- card_t* Start_deck, * player_1_hand, * player_2_hand, * table;
- 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;
- //Size_and_variant(&SIZE_OF_DECK, &no_faces, &variant);
- Start_deck = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
- if (Start_deck == NULL)
- {
- cout << "Malloc failed";
- exit(0);
- }
- player_1_hand = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
- if (player_1_hand == NULL)
- {
- cout << "Malloc failed";
- exit(0);
- }
- player_2_hand = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
- if (player_2_hand == NULL)
- {
- cout << "Malloc failed";
- exit(0);
- }
- table = (card_t*)malloc(SIZE_OF_DECK * sizeof(card_t));
- if (table == NULL)
- {
- cout << "Malloc failed";
- exit(0);
- }
- srand(time(NULL));
- Deck_builder(Start_deck, &no_faces);
- //shuffle(Start_deck);
- //Deal(Start_deck, player_1_hand, player_2_hand, &front_p1, &rear_p1, &front_p2, &rear_p2, &p1cards, &p2cards);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- free(Start_deck);
- for (int i = 0;i < SIZE_OF_DECK;i++)
- {
- Change_faces(player_1_hand[i]);cout << " vs ";Change_faces(player_2_hand[i]);
- cout << endl;
- }
- game(player_1_hand, player_2_hand,table,&front_t,&rear_t, &front_p1, &rear_p1, &front_p2, &rear_p2, &variant, &p1cards, &p2cards);
- free(player_1_hand);
- free(player_2_hand);
- free(table);
- }
- void shuffle(card_t DeckToShuffle[])
- {
- int randomizer;
- card_t helper;
- for (int t = 0; t <= SHUFFLING_TIMES; t++)
- {
- for (int i = 0; i < SIZE_OF_DECK; i++)
- {
- randomizer = rand() % SIZE_OF_DECK;
- helper = DeckToShuffle[i];
- DeckToShuffle[i] = DeckToShuffle[randomizer];
- DeckToShuffle[randomizer] = helper;
- }
- }
- }
- card_t Deck_builder(card_t deck[], int* no_faces)
- {
- int up = 0;
- for (int face = 12; face > (12 - *no_faces); face--)
- {
- for (int suit = 0; suit < NUMBER_OF_SUITS; suit++, up++)
- {
- deck[up].face = card_faces[face];
- deck[up].suit = card_suits[suit];
- }
- }
- return deck[SIZE_OF_DECK];
- }
- 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)
- {
- *front_p1 = -1;
- *front_p2 = -1;
- *rear_p1 = -1;
- *rear_p2 = -1;
- card_t tmp;
- for (int i = 0; i < SIZE_OF_DECK; i++)
- {
- if (i < SIZE_OF_DECK / 2)
- {
- tmp = DeckToDeal[i];
- Add_To_Hand(tmp, front_p1, rear_p1, player_1_hand, p1_cards);
- }
- else
- {
- tmp = DeckToDeal[i];
- Add_To_Hand(tmp, front_p2, rear_p2, player_2_hand, p2_cards);
- }
- }
- }
- void Add_To_Hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards)
- {
- (*p_cards) += 1;
- if (*front_of_player == -1)
- {
- *front_of_player = 0;
- }
- *rear_of_player = (*rear_of_player + 1) % SIZE_OF_DECK;
- player_hand[*rear_of_player] = card;
- }
- card_t Take_From_hand(card_t card, int* front_of_player, int* rear_of_player, card_t player_hand[], int* p_cards)
- {
- (*p_cards) -= 1;
- card = player_hand[*front_of_player];
- *front_of_player = (*front_of_player + 1) % SIZE_OF_DECK;
- return(card);
- }
- void Put_on_table(card_t card, int* front_table, int* rear_table, card_t table[])
- {
- if (*front_table == -1)
- {
- *front_table = 0;
- }
- *rear_table = (*rear_table + 1) % SIZE_OF_DECK;
- table[*rear_table] = card;
- }
- card_t Take_from_table(card_t card, int* front_table, int* rear_table, card_t table[])
- {
- if (*front_table == *rear_table)
- {
- *front_table = -1;
- *rear_table = -1;
- }
- else
- {
- card = table[*front_table];
- *front_table = (*front_table + 1) % SIZE_OF_DECK;
- return(card);
- }
- }
- int isEmpty(int*front)
- {
- if (*front == -1) return 1;
- return 0;
- }
- 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)
- {
- int war_count;
- int round = 0;
- while (true)
- {
- if ((*p1cards >= SIZE_OF_DECK || *p2cards >= SIZE_OF_DECK) || (*p1cards <= 0 || *p2cards <= 0))
- break;
- else
- {
- war_count = 0;
- cout << endl << endl << *p1cards << " " << *p2cards << endl <<"rounds :"<< round << endl;//
- Change_faces(player_1_hand[*front_p1]);
- Change_colors(player_1_hand[*front_p1]);
- cout << " vs ";
- Change_faces(player_2_hand[*front_p2]);
- Change_colors(player_2_hand[*front_p2]);
- cout << endl;//
- if (player_1_hand[*front_p1].face > player_2_hand[*front_p2].face)
- {
- Add_To_Hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards);
- Add_To_Hand(player_2_hand[*front_p2], front_p1, rear_p1, player_1_hand, p1cards);
- Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards);
- Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards);
- round++;
- }
- else if (player_1_hand[*front_p1].face < player_2_hand[*front_p2].face)
- {
- Add_To_Hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards);
- Add_To_Hand(player_1_hand[*front_p1], front_p2, rear_p2, player_2_hand, p2cards);
- Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards);
- Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards);
- round++;
- }
- else if(player_1_hand[*front_p1].face == player_2_hand[*front_p2].face)
- {
- cout << "==========war==========" << endl;
- war_count++;
- 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);
- cout << "==========end of war==========" << endl;
- }
- }
- }
- if (*p1cards <= 0)
- cout << "p2 won game ! " << "rounds: "<< round << endl;
- else
- cout << "p1 won game ! " << "rounds: " << round << endl;
- }
- 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)
- {
- if ((*p1cards >= 3) && (*p2cards >= 3))
- {
- 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
- 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
- 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
- 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
- if (player_1_hand[*front_p1].face > player_2_hand[*front_p2].face)
- {
- cout << "p1 wins"<<endl;
- Put_on_table(Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards), front_table, rear_table, table);
- Put_on_table(Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards), front_table, rear_table, table);
- while (isEmpty(front_table) != 1)
- {
- cout << "added: " << table[*front_table].face << endl;
- Add_To_Hand(Take_from_table(table[*front_table],front_table,rear_table,table), front_p1, rear_p1, player_1_hand, p1cards);
- }
- }
- else if (player_1_hand[*front_p1].face < player_2_hand[*front_p2].face)
- {
- cout << "p2 wins" << endl;
- Put_on_table(Take_From_hand(player_1_hand[*front_p1], front_p1, rear_p1, player_1_hand, p1cards), front_table, rear_table, table);
- Put_on_table(Take_From_hand(player_2_hand[*front_p2], front_p2, rear_p2, player_2_hand, p2cards), front_table, rear_table, table);
- while (isEmpty(front_table) != 1)
- {
- cout << "added: " << table[*front_table].face << endl;
- Add_To_Hand(Take_from_table(table[*front_table], front_table, rear_table, table), front_p2, rear_p2, player_2_hand, p2cards);
- }
- }
- else if (player_1_hand[*front_p1].face == player_2_hand[*front_p2].face)
- {
- cout << "next war" << endl;
- (*round) += 3;
- 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);
- }
- }
- else
- {
- if (*p1cards < 3 )
- {
- (*round) += *p1cards;
- (*p2cards) += (*p1cards);
- (*p1cards) = 0;
- return;
- }
- else if (*p2cards < 3 )
- {
- (*round) += *p2cards;
- (*p1cards) += (*p2cards);
- (*p2cards) = 0;
- return;
- }
- }
- }
- void Change_faces(card_t card)
- {
- if (card.face == 2)
- cout << "Two of";
- else if (card.face == 3)
- cout << "Three of";
- else if (card.face == 4)
- cout << "Four of";
- else if (card.face == 5)
- cout << "Five of";
- else if (card.face == 6)
- cout << "Six of";
- else if (card.face == 7)
- cout << "Seven of";
- else if (card.face == 8)
- cout << "Eight of";
- else if (card.face == 9)
- cout << "Nine of";
- else if (card.face == 10)
- cout << "Ten of";
- else if (card.face == 11)
- cout << "Jack of";
- else if (card.face == 12)
- cout << "Queen of";
- else if (card.face == 13)
- cout << "King of";
- else if (card.face == 14)
- cout << "Ace of";
- }
- void Change_colors(card_t card)
- {
- if (card.suit == 1)
- cout << " hearts";
- else if (card.suit == 2)
- cout << " diamonds";
- else if (card.suit == 3)
- cout << " clubs";
- else if (card.suit == 4)
- cout << " spades";
- }
- void Size_and_variant(int* size_of_deck, int* no_faces, int* variant)
- {
- char size, var = 1;
- cout << "Choose size of deck" << endl;
- cout << "1.A-10" << endl
- << "2.A-9" << endl
- << "3.A-8" << endl
- << "4.A-7" << endl
- << "5.A-6" << endl
- << "6.A-5" << endl
- << "7.A-4" << endl
- << "8.A-3" << endl
- << "9.A-2" << endl;
- cin >> size;
- cout << "=================================" << endl;
- cout << "choose variant of the game" << endl;
- cout << "1.A" << endl
- << "2.B" << endl;
- cin >> var;
- switch (var)
- {
- case '1':
- *variant = 1;
- break;
- case '2':
- *variant = 2;
- break;
- }
- switch (size)
- {
- case '1':
- {
- *size_of_deck = 20;
- *no_faces = 5;
- }break;
- case '2':
- {
- *size_of_deck = 24;
- *no_faces = 6;
- }break;
- case '3':
- {
- *size_of_deck = 28;
- *no_faces = 7;
- }break;
- case '4':
- {
- *size_of_deck = 32;
- *no_faces = 8;
- }break;
- case '5':
- {
- *size_of_deck = 36;
- *no_faces = 9;
- }break;
- case '6':
- {
- *size_of_deck = 40;
- *no_faces = 10;
- }break;
- case '7':
- {
- *size_of_deck = 44;
- *no_faces = 11;
- }break;
- case '8':
- {
- *size_of_deck = 48;
- *no_faces = 12;
- }break;
- case '9':
- {
- *size_of_deck = 52;
- *no_faces = 13;
- }break;
- }
- }
- void Show_blank_card(card_t card, card_t card2)
- {
- cout << "First Player`s blank card: ";Change_faces(card);Change_colors(card);
- cout << endl;
- cout << "Second Player`s blank card : ";Change_faces(card2);Change_colors(card2);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement