Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<stdlib.h>
- #include <cstdlib>
- #include <time.h>
- #include<conio.h>
- #define ROZMIARTALII 52
- using namespace std;
- enum kolor
- {
- pik, kier, trefl, karo
- };
- enum ranga
- {
- II, III, IV, V, VI, VII, VIII, IX, X, walet, dama, krol, as
- };
- typedef struct Karta
- {
- int wartosc;
- int kolor;
- }Karta_t;
- Karta_t talia[ROZMIARTALII];
- //deklaracje funkcji
- void q_add(Karta_t hand[], Karta_t card); // funkcja dodaje kartę na koniec ręki
- Karta_t q_remove(Karta_t hand[]); // funkcja zwraca pierwszą kartę w ręce i ją usuwa
- int count_cards(Karta_t hand[]); // funkcja zlicza karty w ręce bądź stole
- void play(Karta_t P1_hand[], Karta_t P2_hand[]); // funkcja rozgrywa 1 turę gry
- int war(Karta_t P1_hand[], Karta_t P2_hand[], Karta_t card_1, Karta_t card_2); // funkcja zwraca nr gracza który wygrywa wojnę
- void clear_table(Karta_t table[], int i); // funkcja czyści stół po każdej wojnie
- void drukujKarte(Karta_t karta);//drukuje podana karte
- void wypiszTalie(Karta_t* karta);
- void tasowanie(Karta_t* tablica, int n);
- void rozdanie(Karta_t reka1[], Karta_t reka2[]);
- void generujTalie();
- void playA(Karta_t P1_hand[], Karta_t P2_hand[]);
- int warA(Karta_t P1_hand[], Karta_t P2_hand[], Karta_t card_1, Karta_t card_2);
- void playA(Karta_t P1_hand[], Karta_t P2_hand[]);
- int warB(Karta_t P1_hand[], Karta_t P2_hand[], Karta_t card_1, Karta_t card_2);
- void playB(Karta_t P1_hand[], Karta_t P2_hand[]);
- int main()
- {
- Karta_t P1_hand[ROZMIARTALII];
- Karta_t P2_hand[ROZMIARTALII];
- int P1_cards_count = ROZMIARTALII / 2, P2_cards_count = ROZMIARTALII / 2;
- int* P1_count = &P1_cards_count;
- int* P2_count = &P2_cards_count;
- srand(time(NULL));
- generujTalie();
- tasowanie(talia,ROZMIARTALII);
- //wypiszTalie(talia);
- rozdanie(P1_hand, P2_hand);
- int wybor = 0;
- int ruchy = 0;
- cout << "Both players are ready, let's start the game!" << endl << "Press button in order to proceed in the game"<<endl;
- cout << "WYBIERZ RODZAJ WOJNY A==1 LUB B==2"<<endl;
- cin >> wybor;
- if (wybor == 1) {
- while (P1_cards_count > 0 && P2_cards_count > 0) {
- playA(P1_hand, P2_hand);
- ruchy++;
- P1_cards_count = count_cards(P1_hand);
- P2_cards_count = count_cards(P2_hand);
- if (P1_cards_count == 0) cout << "Player 2 has won the game!" << "IT took " << ruchy << " moves to end the game";
- else if (P2_cards_count == 0) cout << "Player 1 has won the game!" << "IT took " << ruchy << " moves to end the game";
- }
- }
- if (wybor == 2) {
- while (P1_cards_count > 0 && P2_cards_count > 0) {
- playB(P1_hand, P2_hand);
- ruchy++;
- P1_cards_count = count_cards(P1_hand);
- P2_cards_count = count_cards(P2_hand);
- if (P1_cards_count == 0) cout << "Player 2 has won the game!" << "IT took " << ruchy << " moves to end the game";
- else if (P2_cards_count == 0) cout << "Player 1 has won the game!" << "IT took " << ruchy << " moves to end the game";
- }
- }
- }
- void q_add(Karta_t hand[], Karta_t card) {
- hand[count_cards(hand)] = card;
- }
- Karta_t q_remove(Karta_t hand[]) {
- Karta_t card = hand[0];
- for (int i = 0; i < count_cards(hand); ++i) {
- hand[i] = hand[i + 1];
- }
- return card;
- }
- int count_cards(Karta_t hand[]) {
- int count = 0;
- for (int i = 0; i < ROZMIARTALII; ++i) {
- if (hand[i].wartosc >= II && hand[i].wartosc <= as) count++;
- }
- return count;
- }
- void play(Karta_t P1_hand[], Karta_t P2_hand[]) {
- Karta_t card_1 = q_remove(P1_hand);
- Karta_t card_2 = q_remove(P2_hand);
- drukujKarte(card_1); cout << " VS "; drukujKarte(card_2); cout << endl;
- if (card_1.wartosc > card_2.wartosc) {
- q_add(P1_hand, card_1);
- q_add(P1_hand, card_2);
- cout << "Player 1 wins this turn and takes both cards! He now has " << count_cards(P1_hand) << "cards " << endl << endl;
- cout << "P1 has " << count_cards(P1_hand) << " cards, and P2 has " << count_cards(P2_hand) << " cards " << endl;
- }
- else if (card_1.wartosc < card_2.wartosc) {
- q_add(P2_hand, card_2);
- q_add(P2_hand, card_1);
- cout << "Player 2 wins this turn and takes both cards! He now has" << count_cards(P2_hand) << "cards" << endl << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- }
- else if (card_1.wartosc == card_2.wartosc) {
- war(P1_hand, P2_hand, card_1, card_2);
- cout << endl;
- }
- }
- int war(Karta_t P1_hand[], Karta_t P2_hand[], Karta_t card_1, Karta_t card_2) {
- cout << endl << " War!those 2 card values are the same!" << endl;
- int p1_count = count_cards(P1_hand);
- int p2_count = count_cards(P2_hand);
- Karta_t table[ROZMIARTALII];
- 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));
- cout<<"Both players now put unknown cards on the table" << endl;
- Karta_t visible_1 = q_remove(P1_hand);
- Karta_t visible_2 = q_remove(P2_hand);
- cout << "Now they put another card which is visible on the table:" << endl;
- drukujKarte(visible_1); cout << "VS"; drukujKarte(visible_2);
- 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].wartosc < table[i - 1].wartosc) {
- 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]);
- }
- cout << "Player 1 wins the war and takes all these cards! He now has" <<count_cards(P1_hand)<< "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 1;
- clear_table(table, i);
- return winner;
- }
- else if (table[i].wartosc > table[i - 1].wartosc) {
- 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]);
- }
- }
- cout << "Player 2 wins the war and takes all these cards! He now has" << count_cards(P2_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 2;
- clear_table(table, i);
- return winner;
- }
- else if (table[i + 1].wartosc == table[i].wartosc) {
- winner = war(P1_hand, P2_hand, table[i], table[i + 1]);
- }
- }
- return winner;
- }
- void clear_table(Karta_t table[], int i) {
- for (int j = 0; j <= i; ++j) {
- q_remove(table);
- }
- }
- void drukujKarte(Karta_t karta)
- {
- const char* rangiKart[] = {
- "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "Jack", "Queen", "King", "Ace"
- };
- const char* koloryKart[] = {
- "Spade","Heart","Club","Diamond"
- };
- cout << rangiKart[karta.wartosc] << " " << koloryKart[karta.kolor] << " ";
- }
- void wypiszTalie(Karta_t* karta)
- {
- const char* rangiKart[] = {
- "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "Jack", "Queen", "King", "Ace"
- };
- const char* koloryKart[] = {
- "Spade","Heart","Club","Diamond"
- };
- for (int a = 0; a < ROZMIARTALII; a++)
- {
- cout << rangiKart[talia[a].wartosc] << " " << koloryKart[talia[a].kolor] << endl;
- }
- }
- void tasowanie(Karta_t* tablica, int n)
- {
- if (n > 1)
- {
- int i;
- for (i = 0; i < n - 1; i++)
- {
- int j = i + rand() / (RAND_MAX / (n - i) + 1);
- Karta t = tablica[j];
- tablica[j] = tablica[i];
- tablica[i] = t;
- }
- }
- }
- void generujTalie()
- {
- int i = 0;
- for (int k = karo; k >= pik; k--)
- for (int w = as; w >= II; w--, i++)
- {
- talia[i].kolor = k;
- talia[i].wartosc = w;
- }
- }
- void rozdanie(Karta_t reka1[], Karta_t reka2[]) {
- int i = 0;
- int k = 0;
- while (i < ROZMIARTALII / 2) {
- reka1[i] = talia[k];
- k++;
- ++i;
- }
- int j = 0;
- while (j < ROZMIARTALII / 2) {
- reka2[j] = talia[k];
- k++;
- ++j;
- }
- }
- int warA(Karta_t P1_hand[], Karta_t P2_hand[], Karta_t card_1, Karta_t card_2) {
- cout << endl << " War!those 2 card values are the same!" << endl;
- int p1_count = count_cards(P1_hand);
- int p2_count = count_cards(P2_hand);
- Karta_t table[ROZMIARTALII];
- q_add(table, card_1);
- q_add(table, card_2);
- int winner = 0; // zmienna sprawdzająca kto jest zwycięzcą wojny
- while (winner == 0) {
- cout << "Both players now put unknown cards on the table" << endl;
- q_add(table, q_remove(P1_hand));
- q_add(table, q_remove(P2_hand));
- if (count_cards(P1_hand) == 0) {
- cout << "PLAYER 1 RAN OUT OF CARDS PLAYER 2 WINS";
- int i = count_cards(table)-1;
- for (int j = 0; j <= i; ++j) {
- q_add(P2_hand, table[j]);
- }
- winner = 2;
- clear_table(table, i);
- return winner;
- }
- if (count_cards(P2_hand) == 0) {
- cout << "PLAYER 2 RAN OUT OF CARDS PLAYER 1 WINS";
- int i = count_cards(table)-1;
- for (int j = 0; j <= i; ++j) {
- q_add(P1_hand, table[j]);
- }
- winner = 1;
- clear_table(table, i);
- return winner;
- }
- cout << "Now they put another card which is visible on the table:" << endl;
- Karta_t visible_1 = q_remove(P1_hand);
- Karta_t visible_2 = q_remove(P2_hand);
- if (count_cards(P1_hand) == 0) {
- cout << "PLAYER 1 RAN OUT OF CARDS PLAYER 2 WINS";
- int i = count_cards(table)-1;
- for (int j = 0; j <= i; ++j) {
- q_add(P2_hand, table[j]);
- }
- winner = 2;
- clear_table(table, i);
- return winner;
- }
- if (count_cards(P2_hand) == 0) {
- cout << "PLAYER 2 RAN OUT OF CARDS PLAYER 1 WINS";
- int i = count_cards(table) -1;
- for (int j = 0; j <= i; ++j) {
- q_add(P1_hand, table[j]);
- }
- winner = 1;
- clear_table(table, i);
- return winner;
- }
- drukujKarte(visible_1); cout << "VS"; drukujKarte(visible_2);
- 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].wartosc < table[i - 1].wartosc ) {
- 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]);
- }
- cout << "Player 1 wins the war and takes all these cards! He now has" << count_cards(P1_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 1;
- clear_table(table, i);
- return winner;
- }
- else if (table[i].wartosc > table[i - 1].wartosc) {
- 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]);
- }
- }
- cout << "Player 2 wins the war and takes all these cards! He now has" << count_cards(P2_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 2;
- clear_table(table, i);
- return winner;
- }
- else if (table[i + 1].wartosc == table[i].wartosc) {
- winner = warA(P1_hand, P2_hand, table[i], table[i + 1]);
- }
- }
- return winner;
- }
- void playA(Karta_t P1_hand[], Karta_t P2_hand[]) {
- Karta_t card_1 = q_remove(P1_hand);
- Karta_t card_2 = q_remove(P2_hand);
- drukujKarte(card_1); cout << " VS "; drukujKarte(card_2); cout << endl;
- if (card_1.wartosc > card_2.wartosc) {
- q_add(P1_hand, card_1);
- q_add(P1_hand, card_2);
- cout << "Player 1 wins this turn and takes both cards! He now has " << count_cards(P1_hand) << "cards " << endl << endl;
- cout << "P1 has " << count_cards(P1_hand) << " cards, and P2 has " << count_cards(P2_hand) << " cards " << endl;
- }
- else if (card_1.wartosc < card_2.wartosc) {
- q_add(P2_hand, card_2);
- q_add(P2_hand, card_1);
- cout << "Player 2 wins this turn and takes both cards! He now has" << count_cards(P2_hand) << "cards" << endl << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- }
- else if (card_1.wartosc == card_2.wartosc) {
- warA(P1_hand, P2_hand, card_1, card_2);
- cout << endl;
- }
- }
- int warB(Karta_t P1_hand[], Karta_t P2_hand[], Karta_t card_1, Karta_t card_2) {
- cout << endl << " War! those 2 card values are the same!" << endl;
- int p1_count = count_cards(P1_hand);
- int p2_count = count_cards(P2_hand);
- Karta_t table1[ROZMIARTALII];//stos gracza 1
- Karta_t table2[ROZMIARTALII];//stos gracza 2
- Karta_t table[ROZMIARTALII];
- q_add(table1, card_1);
- q_add(table2, card_2);
- q_add(table, card_1);
- q_add(table, card_2);
- int winner = 0; // zmienna sprawdzająca kto jest zwycięzcą wojny
- while (winner == 0) {
- if (count_cards(P1_hand) == 0) {
- cout << "PLAYER 1 has started war with only 1 card so PLAYER 2 puts 2 cards for him then 2 for himself" << endl;
- q_add(table1, q_remove(P2_hand));//zakryta
- Karta_t visible_b1 = q_remove(P2_hand);
- q_add(table1,visible_b1);//odkryta
- q_add(table2, q_remove(P2_hand));//zakryta
- Karta_t visible_b2 = q_remove(P2_hand);
- q_add(table2, visible_b2);//odkryta
- drukujKarte(visible_b1); cout << "VS"; drukujKarte(visible_b2);
- int a = count_cards(table1) - 1;
- int b = count_cards(table2) - 1;
- if (table1[a].wartosc>table2[b].wartosc) {
- for (int j = 0; j <= a; ++j) {
- q_add(P1_hand, table1[j]);
- q_add(P1_hand, table2[j]);
- }
- cout << "Player 1 wins the war and takes all these cards! He now has" << count_cards(P1_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 1;
- clear_table(table1, a);
- clear_table(table2, b);
- return winner;
- }
- else if (table1[a].wartosc < table2[b].wartosc) {
- for (int j = 0; j <= a; ++j) {
- q_add(P2_hand, table1[j]);
- q_add(P2_hand, table2[j]);
- }
- cout << "Player 2 wins the war and takes all these cards! He now has" << count_cards(P2_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 2;
- clear_table(table1, a);
- clear_table(table2, b);
- return winner;
- }
- }
- if (count_cards(P2_hand) == 0) {
- cout << "PLAYER 2 has started war with only 1 card so PLAYER 1 puts 2 cards for him then 2 for himself" << endl;
- q_add(table2, q_remove(P1_hand));//zakryta
- Karta_t visible_b1 = q_remove(P1_hand);
- q_add(table2, visible_b1);//odkryta
- q_add(table1, q_remove(P1_hand));//zakryta
- Karta_t visible_b2 = q_remove(P1_hand);
- q_add(table1,visible_b2);//odkryta
- drukujKarte(visible_b1); cout << "VS"; drukujKarte(visible_b2);
- int a = count_cards(table1) - 1;
- int b = count_cards(table2) - 1;
- if (table1[a].wartosc > table2[b].wartosc) {
- for (int j = 0; j <= a; ++j) {
- q_add(P1_hand, table1[j]);
- q_add(P1_hand, table2[j]);
- }
- cout << "Player 1 wins the war and takes all these cards! He now has" << count_cards(P1_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 1;
- clear_table(table1, a);
- clear_table(table2, b);
- return winner;
- }
- else if (table1[a].wartosc < table2[b].wartosc) {
- for (int j = 0; j <= a; ++j) {
- q_add(P2_hand, table1[j]);
- q_add(P2_hand, table2[j]);
- }
- cout << "Player 2 wins the war and takes all these cards! He now has" << count_cards(P2_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 2;
- clear_table(table1, a);
- clear_table(table2, b);
- return winner;
- }
- else if (table1[a].wartosc == table2[b].wartosc) {
- winner = warB(P1_hand, P2_hand, table1[a], table[b]);
- }
- }
- else {
- cout << "Both players now put unknown cards on the table" << endl;
- q_add(table, q_remove(P1_hand));//zakryta
- q_add(table, q_remove(P2_hand));//zakryta
- cout << "Now they put another card which is visible on the table:" << endl;
- Karta_t visible_1 = q_remove(P1_hand);
- Karta_t visible_2 = q_remove(P2_hand);
- drukujKarte(visible_1); cout << "VS"; drukujKarte(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].wartosc < table[i - 1].wartosc) {
- for (int j = 0; j <= i; ++j) {
- q_add(P1_hand, table[j]);
- }
- cout << "Player 1 wins the war and takes all these cards! He now has" << count_cards(P1_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 1;
- clear_table(table, i);
- return winner;
- }
- else if (table[i].wartosc > table[i - 1].wartosc) {
- for (int j = 0; j <= i; ++j) {
- q_add(P2_hand, table[j]);
- }
- cout << "Player 2 wins the war and takes all these cards! He now has" << count_cards(P2_hand) << "cards" << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- winner = 2;
- clear_table(table, i);
- return winner;
- }
- else if (table[i + 1].wartosc == table[i].wartosc) {
- winner = warB(P1_hand, P2_hand, table[i], table[i + 1]);
- }
- }
- }
- return winner;
- }
- void playB(Karta_t P1_hand[], Karta_t P2_hand[]) {
- Karta_t card_1 = q_remove(P1_hand);
- Karta_t card_2 = q_remove(P2_hand);
- drukujKarte(card_1); cout << " VS "; drukujKarte(card_2); cout << endl;
- if (card_1.wartosc > card_2.wartosc) {
- q_add(P1_hand, card_1);
- q_add(P1_hand, card_2);
- cout << "Player 1 wins this turn and takes both cards! He now has " << count_cards(P1_hand) << "cards " << endl << endl;
- cout << "P1 has " << count_cards(P1_hand) << " cards, and P2 has " << count_cards(P2_hand) << " cards " << endl;
- }
- else if (card_1.wartosc < card_2.wartosc) {
- q_add(P2_hand, card_2);
- q_add(P2_hand, card_1);
- cout << "Player 2 wins this turn and takes both cards! He now has" << count_cards(P2_hand) << "cards" << endl << endl;
- cout << "P1 has " << count_cards(P1_hand) << "cards, and P2 has " << count_cards(P2_hand) << "cards" << endl;
- }
- else if (card_1.wartosc == card_2.wartosc) {
- warB(P1_hand, P2_hand, card_1, card_2);
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement