Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <conio.h>
- #include <cstdlib>
- #define DECK_SIZE 52
- //Stworzenie struktury card oraz tablicy zawierającej wszystkie karty - talii początkowej.
- struct card {
- char suit[9];
- char rank[6];
- int value;
- };
- typedef struct card card;
- card empty {" ", " ", 0 };
- card spades_2 { "Spades", "Two", 2 };
- card spades_3 { "Spades", "Three", 3 };
- card spades_4 { "Spades", "Four", 4 };
- card spades_5 { "Spades", "Five", 5 };
- card spades_6 { "Spades", "Six", 6 };
- card spades_7 { "Spades", "Seven", 7 };
- card spades_8 { "Spades", "Eight", 8 };
- card spades_9 { "Spades", "Nine", 9 };
- card spades_10 { "Spades", "Ten", 10 };
- card spades_jack { "Spades", "Jack", 11 };
- card spades_queen { "Spades", "Queen", 12 };
- card spades_king { "Spades", "King", 13 };
- card spades_ace { "Spades", "Ace", 14 };
- card hearts_2 { "Hearts", "Two", 2 };
- card hearts_3 { "Hearts", "Three", 3 };
- card hearts_4 { "Hearts", "Four", 4 };
- card hearts_5 { "Hearts", "Five", 5 };
- card hearts_6 { "Hearts", "Six", 6 };
- card hearts_7 { "Hearts", "Seven", 7 };
- card hearts_8 { "Hearts", "Eight", 8 };
- card hearts_9 { "Hearts", "Nine", 9 };
- card hearts_10 { "Hearts", "Ten", 10 };
- card hearts_jack { "Hearts", "Jack", 11 };
- card hearts_queen { "Hearts", "Queen", 12 };
- card hearts_king { "Hearts", "King", 13 };
- card hearts_ace { "Hearts", "Ace", 14 };
- card clubs_2 { "Clubs", "Two", 2 };
- card clubs_3 { "Clubs", "Three", 3 };
- card clubs_4 { "Clubs", "Four", 4 };
- card clubs_5 { "Clubs", "Five", 5 };
- card clubs_6 { "Clubs", "Six", 6 };
- card clubs_7 { "Clubs", "Seven", 7 };
- card clubs_8 { "Clubs", "Eight", 8 };
- card clubs_9 { "Clubs", "Nine", 9 };
- card clubs_10 { "Clubs", "Ten", 10 };
- card clubs_jack { "Clubs", "Jack", 11 };
- card clubs_queen { "Clubs", "Queen", 12 };
- card clubs_king { "Clubs", "King", 13 };
- card clubs_ace { "Clubs", "Ace", 14 };
- card diamonds_2 { "Diamonds", "Two", 2 };
- card diamonds_3 { "Diamonds", "Three", 3 };
- card diamonds_4 { "Diamonds", "Four", 4 };
- card diamonds_5 { "Diamonds", "Five", 5 };
- card diamonds_6 { "Diamonds", "Six", 6 };
- card diamonds_7 { "Diamonds", "Seven", 7 };
- card diamonds_8 { "Diamonds", "Eight", 8 };
- card diamonds_9 { "Diamonds", "Nine", 9 };
- card diamonds_10 { "Diamonds", "Ten", 10 };
- card diamonds_jack { "Diamonds", "Jack", 11 };
- card diamonds_queen { "Diamonds", "Queen", 12 };
- card diamonds_king { "Diamonds", "King", 13 };
- card diamonds_ace { "Diamonds", "Ace", 14 };
- // koejnosc kart w talii trzeba zmodyfikować aby umożliwić dowolne wybieranie talii początkowej jak jest to opisane w wymaganiach
- const card deck[52] = { spades_2,spades_3,spades_4,spades_5,spades_6,spades_7,spades_8,
- spades_9, spades_10, spades_jack, spades_queen, spades_king, spades_ace,
- hearts_2, hearts_3, hearts_4, hearts_5, hearts_6, hearts_7, hearts_8,
- hearts_9, hearts_10, hearts_jack, hearts_queen, hearts_king, hearts_ace,
- clubs_2, clubs_3, clubs_4, clubs_5, clubs_6, clubs_7, clubs_8, clubs_9,
- clubs_10, clubs_jack, clubs_queen, clubs_king, clubs_ace,
- diamonds_2, diamonds_3, diamonds_4, diamonds_5, diamonds_6, diamonds_7,
- diamonds_8, diamonds_9, diamonds_10,diamonds_jack, diamonds_queen, diamonds_king, diamonds_ace };
- //deklaracje funkcji
- void deal( card P1_hand[], card P2_hand[]); // funkcja losowo rozdaje karty
- void q_add( card hand[], card card); // funkcja dodaje kartę na koniec ręki
- card q_remove( card hand[]); // funkcja zwraca pierwszą kartę w ręce i ją usuwa
- int count_cards( card hand[]); // funkcja zlicza karty w ręce bądź stole
- void play( card P1_hand[], card P2_hand[]); // funkcja rozgrywa 1 turę gry
- int war( card P1_hand[], card P2_hand[], card card_1, card card_2); // funkcja zwraca nr gracza który wygrywa wojnę
- void clear_table( card table[], int i); // funkcja czyści stół po każdej wojnie
- int main()
- {
- card P1_hand[DECK_SIZE] = {};
- card P2_hand[DECK_SIZE] = {};
- int P1_cards_count=DECK_SIZE/2, P2_cards_count=DECK_SIZE/2;
- int* P1_count = &P1_cards_count;
- int* P2_count = &P2_cards_count;
- deal(P1_hand, P2_hand);
- _cprintf("Both players are ready, let's start the game!\n Press N in order to proceed in the game\n\n");
- _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
- while (_getch()) {
- P1_cards_count = count_cards(P1_hand);
- P2_cards_count = count_cards(P2_hand);
- if (P1_cards_count == 0) _cprintf("Player 2 has won the game!");
- else if (P2_cards_count == 0) _cprintf("Player 1 has won the game!");
- else if (P1_cards_count > 0 && P2_cards_count > 0) {
- play(P1_hand, P2_hand);
- }
- }
- }
- //definicje funkcji
- void deal( card P1_hand[], card P2_hand[]) {
- int flag[DECK_SIZE] = { 0 };
- srand(time(NULL));
- int i = 0;
- while (i < DECK_SIZE/2) {
- int k = rand() % DECK_SIZE;
- if (flag[k] == 0) {
- P1_hand[i] = deck[k];
- flag[k] = 1;
- ++i;
- }
- }
- int j = 0;
- while (j < DECK_SIZE / 2) {
- int k = rand() % DECK_SIZE;
- if (flag[k] == 0) {
- P2_hand[j] = deck[k];
- flag[k] = 1;
- ++j;
- }
- }
- P1_hand[DECK_SIZE / 2 + 1] = empty;
- P2_hand[DECK_SIZE / 2 + 1] = empty;
- }
- void q_add( card hand[], card card) {
- hand[count_cards(hand)] = card;
- }
- card q_remove( card hand[]){
- card card = hand[0];
- for (int i = 0; i < count_cards(hand); ++i) {
- hand[i] = hand[i + 1];
- }
- return card;
- }
- int count_cards( card hand[]) {
- int count = 0;
- for (int i = 0; i < DECK_SIZE; ++i) {
- if (hand[i].value>=2 && hand[i].value<=14) count++;
- }
- return count;
- }
- void play( card P1_hand[], card P2_hand[]) {
- card card_1 = q_remove(P1_hand);
- card card_2 = q_remove(P2_hand);
- _cprintf("%s of %s VS %s of %s\n", card_1.rank, card_1.suit, card_2.rank, card_2.suit);
- if (card_1.value > card_2.value) {
- q_add(P1_hand, card_1);
- q_add(P1_hand, card_2);
- _cprintf("Player 1 wins this turn and takes both cards! He now has %i cards.\n\n", count_cards(P1_hand));
- _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
- }
- else if (card_1.value < card_2.value) {
- q_add(P2_hand, card_2);
- q_add(P2_hand, card_1);
- _cprintf("Player 2 wins this turn and takes both cards! He now has %i cards\n\n", count_cards(P2_hand));
- _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
- }
- else if (card_1.value == card_2.value) {
- war(P1_hand, P2_hand, card_1, card_2);
- _cprintf("\n");
- }
- }
- int war( card P1_hand[], card P2_hand[], card card_1, card card_2) {
- _cprintf("\n War! those 2 card values are the same!\n");
- int p1_count = count_cards(P1_hand);
- int p2_count = count_cards(P2_hand);
- static card table[DECK_SIZE];
- q_add(table, card_1);
- q_add(table, card_2);
- int winner = 0; // zmienna sprawdzająca kto jest zwycięzcą wojny
- while (winner == 0) {
- q_add(table, q_remove(P1_hand));
- q_add(table, q_remove(P2_hand));
- _cprintf("Both players now put unknown cards on the table\n");
- card visible_1 = q_remove(P1_hand);
- card visible_2 = q_remove(P2_hand);
- _cprintf("Now they put another card on the table: \n%s of %s VS %s of %s\n", visible_1.rank, visible_1.suit, visible_2.rank, visible_2.suit);
- q_add(table, visible_1);
- q_add(table, visible_2);
- int i = count_cards(table)-1; // i to ostatni indeks pod którym znajduje się karta w tablicy stołu
- if (table[i].value < table[i-1].value) {
- for (int j = 0; j <= i; ++j) {
- if (j % 2 == 0) q_add(P1_hand, table[j]);
- }
- for (int j = 0; j <= i; ++j) {
- if (j % 2 == 1) q_add(P1_hand, table[j]);
- }
- _cprintf("Player 1 wins the war and takes all these cards! He now has %i cards.\n", count_cards(P1_hand));
- _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
- winner = 1;
- clear_table(table, i);
- return winner;
- }
- else if (table[i].value > table[i-1].value) {
- for (int j = 0; j <= i; ++j) {
- if (j % 2 == 1) {
- q_add(P2_hand, table[j]);
- }
- }
- for (int j = 0; j <= i; ++j) {
- if (j % 2 == 0) {
- q_add(P2_hand, table[j]);
- }
- }
- _cprintf("Player 2 wins the war and takes all these cards! He now has %i cards.\n", count_cards(P2_hand));
- _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
- winner = 2;
- clear_table(table, i);
- return winner;
- }
- else if (table[i + 1].value == table[i].value) {
- winner = war(P1_hand, P2_hand, table[i], table[i+1]);
- }
- }
- return winner;
- }
- void clear_table( card table[], int i) {
- for (int j = 0; j <= i; ++j) {
- q_remove(table);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement