Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- #define CARD_TEST
- #define TRUE 1
- #define FALSE 0
- enum T_cardColour {
- BLANK,//warość potrzebna do zainicjowania kart w razie, gdyby jakaś strategia nie została przewidziana
- HEART,
- DIAMOND,
- CLUB,
- SPADE
- };
- enum T_cardValue {
- ZERO,//warość potrzebna do zainicjowania kart w razie, gdyby jakaś strategia nie została przewidziana
- TWO,
- THREE,
- FOUR,
- FIVE,
- SIX,
- SEVEN,
- EIGHT,
- NINE,
- TEN,
- JACK,
- QUEEN,
- KING,
- ACE
- };
- enum T_strategy {
- NONE,
- RANDOM,
- PEACEFUL,
- AGRESSIVE
- };
- typedef struct card {
- T_cardColour colour;
- T_cardValue value;
- }T_card;
- typedef struct hand {
- T_card my_card;
- struct hand* next_card;
- }T_hand;
- struct player {
- T_hand hand_start;
- T_hand* hand_bottom;
- T_card* table;
- int moves_left;//ile maksymalnie ruchów gracz może wykonać w danej rundzie, jeżeli przekroczy tą wartość rozgrywka jest uznana za nieskończoną
- int versionB = FALSE;
- T_strategy strategy=NONE;
- int chooser;
- int martial_law=FALSE;
- };
- #ifdef CARD_TEST
- void PrintCard(const T_card printed_card);
- void ShuffleCards(T_card* allcards, const int number_of_cards);
- #endif
- void CreateNewHand(player* P) {
- P->hand_start.next_card = NULL;
- }
- void ReturnToHand(player* P, const T_card new_card) {
- T_hand* beggining = &(P->hand_start);
- T_hand* x = (T_hand*)malloc(sizeof(T_hand));
- if (x == NULL) {
- cout << "There is to little space for this element" << endl;
- }
- else {
- x->my_card = new_card;
- x->next_card = beggining->next_card;
- beggining->next_card = x;
- }
- }
- void AddToHand(player* P, const T_card new_card) {
- T_hand* x = (T_hand*)malloc(sizeof(T_hand));
- if (x == NULL) {
- cout << "There is to little space for this element" << endl;
- }
- else {
- x->my_card = new_card;
- x->next_card = P->hand_bottom->next_card;
- P->hand_bottom->next_card = x;
- while (P->hand_bottom->next_card != NULL) {
- P->hand_bottom = P->hand_bottom->next_card;
- }
- }
- }
- T_card RemoveFromHand(player* P) {
- if (P->hand_start.next_card != NULL) {
- T_hand* x = P->hand_start.next_card;
- P->hand_start.next_card = P->hand_start.next_card->next_card;
- T_card removed_card;
- removed_card = x->my_card;
- free(x);
- if (P->hand_start.next_card == NULL) {//jeżeli gracz wyłożył swoją ostatnią kartę to hand_bottom znowu musi pokazywać na head
- P->hand_bottom = &P->hand_start;
- }
- return removed_card;
- }
- }
- T_card* CreateCards(const int number_of_cards) {
- T_card* all_cards = (T_card*)malloc(number_of_cards * sizeof(T_card));
- if (all_cards == NULL) {
- cout << "There is to little space for this element" << endl;
- return NULL;
- }
- else {
- #ifdef CARD_TEST
- cout << endl;
- cout << "CREATE CARDS FUNCTION" << endl;
- #endif
- for (int i = 0; i < number_of_cards; i++) {
- T_cardValue value=ZERO;
- if (i < 4) { value = ACE; }
- else if (i < 8) { value = KING; }
- else if (i < 12) { value = QUEEN; }
- else if (i < 16) { value = JACK; }
- else if (i < 20) { value = TEN; }
- else if (i < 24) { value = NINE; }
- else if (i < 28) { value = EIGHT; }
- else if (i < 32) { value = SEVEN; }
- else if (i < 36) { value = SIX; }
- else if (i < 40) { value = FIVE; }
- else if (i < 44) { value = FOUR; }
- else if (i < 48) { value = THREE; }
- else if (i < 52) { value = TWO; }
- T_cardColour colour=BLANK;
- if (i % 4 == 0) { colour = HEART; }
- else if (i % 4 == 1) { colour = DIAMOND; }
- else if (i % 4 == 2) { colour = CLUB; }
- else if (i % 4 == 3) { colour = SPADE; }
- all_cards[i].colour = colour;
- all_cards[i].value = value;
- #ifdef CARD_TEST
- PrintCard(all_cards[i]);
- #endif
- }
- return all_cards;
- }
- }
- void Deal(player* P1, player* P2, T_card* allcards, const int number_of_cards) {//podzielenie kart na dwie polowy i zrobienie nich dwoch list, przypisanie listy do gracza
- for (int i = 0; i < number_of_cards; i++) {
- T_card assigned_card = allcards[i];
- if (i <= (number_of_cards / 2) - 1) {
- AddToHand(P1, assigned_card);
- }
- else
- AddToHand(P2, assigned_card);
- }
- }
- void ShuffleCards(T_card* allcards, const int number_of_cards) { //potasowanie kart
- for (int i = number_of_cards - 1; i > 0; i--) {
- int j = rand() % (i + 1);
- T_card temp = allcards[i];
- allcards[i] = allcards[j];
- allcards[j] = temp;
- }
- #ifdef CARD_TEST
- cout << endl;
- cout << "SHUFFLE CARDS FUNCTION" << endl;
- for (int i = 0; i < number_of_cards; i++) {
- PrintCard(allcards[i]);
- }
- cout << endl;
- #endif
- }
- void HandOfCards(player* P1, player* P2, T_card* allcards, const int number_of_cards) {
- ShuffleCards(allcards, number_of_cards);
- P1->hand_bottom = &P1->hand_start;//przypisanie wskażnika na głowę i potem uzywamy tego wsk na dodawanie elementów
- P2->hand_bottom = &P2->hand_start;//na koniec listy, a head zawsze pokazuje na pierwszy element
- Deal(P1, P2, allcards, number_of_cards);
- }
- void FirstHandOfCards(player* P1, player* P2, const int number_of_cards) {
- CreateNewHand(P1);
- CreateNewHand(P2);
- T_card* allcards = CreateCards(number_of_cards);
- HandOfCards(P1, P2, allcards, number_of_cards);
- free(allcards);
- }
- void PrintCard(const T_card printed_card) {
- if (printed_card.value == TWO) { cout << "2"; }
- else if (printed_card.value == THREE) { cout << "3"; }
- else if (printed_card.value == FOUR) { cout << "4"; }
- else if (printed_card.value == FIVE) { cout << "5"; }
- else if (printed_card.value == SIX) { cout << "6"; }
- else if (printed_card.value == SEVEN) { cout << "7"; }
- else if (printed_card.value == EIGHT) { cout << "8"; }
- else if (printed_card.value == NINE) { cout << "9"; }
- else if (printed_card.value == TEN) { cout << "10"; }
- else if (printed_card.value == JACK) { cout << "J"; }
- else if (printed_card.value == QUEEN) { cout << "Q"; }
- else if (printed_card.value == KING) { cout << "K"; }
- else if (printed_card.value == ACE) { cout << "A"; }
- cout << "|";
- if (printed_card.colour == HEART) { cout << "h"; }
- else if (printed_card.colour == DIAMOND) { cout << "d"; }
- else if (printed_card.colour == CLUB) { cout << "c"; }
- else if (printed_card.colour == SPADE) { cout << "s"; }
- cout << " ";
- }
- int CountCards(const player P) {
- int number_of_cards = 0;
- T_hand* skim = P.hand_start.next_card;
- while (skim != NULL) {
- number_of_cards++;
- skim = skim->next_card;
- }
- return number_of_cards;
- }
- void ShowAllCards(const player P) {
- #ifdef CARD_TEST
- cout << endl;
- cout << endl;
- cout << endl;
- cout << endl;
- cout << endl;
- cout << "peeped" << endl;
- #endif
- T_hand* peek = P.hand_start.next_card;
- while (peek != NULL) {
- PrintCard(peek->my_card);
- peek = peek->next_card;
- }
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include "deck.h"
- using namespace std;
- //#define VersionBTEST
- //#define TEST
- #define NO_MORE_CARDS_TO_GIVE -1
- #define WIN_P1 1
- #define WIN_P2 2
- #define DRAW 0
- #define INFINITE -1
- int InitiatingBattle(player* P1, player* P2,int* crd_nr, int* help);
- void Battle(player* P1, player* P2, int* crd_nr, int* help);
- int FunctionCallingCounter() {
- static unsigned int count;
- count++;
- return count;
- }
- void CardsForWinner(player* winner, player* looser, const int amount_cards) {
- #ifdef TEST
- cout << "CardsForWinner FUNCTION--> cards_from_winner-->";
- for (int i = 0; i < amount_cards /2; i++) {
- PrintCard(winner->table[i]);
- }
- cout << endl;
- cout << "CardsForWinner FUNCTION--> cards_from_looser-->";
- for (int i = 0; i < amount_cards /2; i++) {
- PrintCard(looser->table[i]);
- }
- cout << endl;
- cout << endl;
- #endif
- if (winner->strategy != NONE) {
- if (amount_cards > 3) {
- int quantity = amount_cards / 2;
- ShuffleCards(winner->table, quantity);
- ShuffleCards(looser->table, quantity);
- }
- }
- for (int i = 0; i < amount_cards / 2; i++) {
- T_card assigned_card = winner->table[i];
- AddToHand(winner, assigned_card);
- }
- free(winner->table);
- winner->table = NULL;
- for (int i = 0; i < amount_cards / 2; i++) {
- T_card assigned_card = looser->table[i];
- AddToHand(winner, assigned_card);
- }
- free(looser->table);
- looser->table = NULL;
- }
- T_card* AddToTable(T_card* table, const T_card card_to_add, const int nr_of_cards_on_table) {
- FunctionCallingCounter();
- //T_card* increased_table = (T_card*)realloc(table, (sizeof(T_card)) * ((nr_of_cards_on_table / 2) + 1));
- T_card* increased_table = (T_card*)malloc((sizeof(T_card)) * ((nr_of_cards_on_table / 2) + 1));
- #ifdef TEST
- cout << endl;
- cout << "AddToTable FUNCTION--> table-->";
- if (nr_of_cards_on_table == 0 || nr_of_cards_on_table == 1) {
- cout << "0" << endl;
- }
- else {
- for (int i = 0; i < nr_of_cards_on_table/2; i++) {
- PrintCard(table[i]);
- }
- cout << endl;
- }
- cout << "Card to add--> ";
- PrintCard(card_to_add);
- #endif
- if (increased_table == NULL) {
- cout << "There is to little space for this element" << endl;
- return NULL;//zkonczenie gry
- }
- else {
- for (int i = 0; i < nr_of_cards_on_table / 2; i++) {
- increased_table[i].value = table[i].value;
- increased_table[i].colour = table[i].colour;
- }
- increased_table[nr_of_cards_on_table / 2].value = card_to_add.value;
- increased_table[nr_of_cards_on_table / 2].colour = card_to_add.colour;
- #ifdef TEST
- cout << endl;
- cout << "AddToTable FUNCTION--> increased_table-->";
- for (int i = 0; i < nr_of_cards_on_table/2 + 1; i++) {
- PrintCard(increased_table[i]);
- }
- cout << endl;
- #endif
- free(table);
- table = NULL;
- return increased_table;
- }
- }
- int OpponentsHelp(player* PTaker, player* PGiver,int crd_nr) {
- int nr_of_lacking_cards = 2 - CountCards(*PTaker);
- for (int i = 0; i < nr_of_lacking_cards; i++) {
- if (PGiver->hand_start.next_card == NULL) {
- return NO_MORE_CARDS_TO_GIVE;
- }
- else if (i == 0) {
- T_card given_card = RemoveFromHand(PGiver);
- PTaker->table = AddToTable(PTaker->table, given_card, crd_nr++);
- }
- else if (i == 1) {
- T_card given_card = RemoveFromHand(PGiver);
- AddToHand(PTaker, given_card);
- #ifdef VersionBTEST
- cout << "OpponentsHelp card given to hand-->";
- PrintCard(given_card);
- cout << endl;
- #endif
- }
- }
- #ifdef VersionBTEST
- cout << "OpponentsHelp takers table-->";
- for (int i = 0; i < crd_nr / 2 + 1; i++) {
- PrintCard(*takers_table[i]);
- }
- cout << endl;
- #endif
- cout << endl;
- cout << "***Opponents help was used!***" << endl;
- cout << endl;
- return nr_of_lacking_cards;
- }
- T_card RandomSelect(const T_card card1, const T_card card2) {
- T_card random;
- int j = rand() % 2;
- if (j == 0) { random = card1; }
- else
- random = card2;
- return random;
- }
- T_card Strategy(player* P, const T_card card_op1, const T_card card_op2, T_card opponents_card) {
- T_card choosen;
- choosen.value = ZERO;
- choosen.colour = BLANK;
- //if (P->strategy == RANDOM) {}//zawsze random, dlatego brak specyfikacji
- if (P->strategy == PEACEFUL) {
- if (card_op1.value < opponents_card.value) {
- if (card_op1.value < card_op2.value) { choosen = card_op1; }
- else if (card_op1.value > card_op2.value) { choosen = card_op2; }
- }
- else if (card_op1.value > opponents_card.value) {
- if (card_op1.value < card_op2.value) { choosen = card_op1; }
- else if (card_op1.value>card_op2.value) {
- if (card_op2.value != opponents_card.value) {choosen = card_op2;}
- else { choosen = card_op1; }
- }
- }
- else if (card_op1.value == opponents_card.value) {
- if (card_op1.value != card_op2.value) { choosen = card_op2; }
- }
- }
- else if (P->strategy == AGRESSIVE) {
- if (card_op1.value < opponents_card.value) {
- if (card_op1.value < card_op2.value) {
- if (opponents_card.value <= card_op2.value) { choosen = card_op2; }
- else if(opponents_card.value > card_op2.value){ choosen = card_op1; }
- }
- else if (card_op1.value > card_op2.value) { choosen = card_op2; }
- }
- else if (card_op1.value > opponents_card.value) {
- if (card_op1.value < card_op2.value) { choosen = card_op1; }
- if (card_op1.value > card_op2.value) {
- if (card_op2.value == opponents_card.value) { choosen = card_op2; }
- if (card_op2.value < opponents_card.value) { choosen = card_op1; }
- }
- }
- else if (card_op1.value == opponents_card.value) {
- if (card_op2.value != opponents_card.value) { choosen = card_op1; }
- }
- }
- if(choosen.value==ZERO || choosen.colour==BLANK){
- choosen = RandomSelect(card_op1, card_op2);
- }
- if (choosen.value == card_op1.value && choosen.colour == card_op1.colour) {
- ReturnToHand(P, card_op2);
- }
- else {
- ReturnToHand(P, card_op1);
- }
- return choosen;
- }
- void War(player* P1, player* P2, int* crd_nr, int* help) {
- T_card hidden_card_P1 = RemoveFromHand(P1);
- T_card hidden_card_P2 = RemoveFromHand(P2);
- P1->table = AddToTable(P1->table, hidden_card_P1, (*crd_nr)++);
- P2->table = AddToTable(P2->table, hidden_card_P2, (*crd_nr)++);
- --(P1->moves_left);
- --(P2->moves_left);
- InitiatingBattle(P1, P2,crd_nr, help);
- }
- void InitiatingWar(player* P1, player* P2,int* crd_nr, int* help) {
- if (*help == TRUE) {
- if (CountCards(*P1) < 2 || CountCards(*P2) < 2) {
- int nr_of_given_cards = 0;
- player** giver = NULL;
- T_card** givers_table = NULL;
- *help = FALSE;
- if (CountCards(*P1) < 2) {
- nr_of_given_cards = OpponentsHelp(P1, P2, *crd_nr);
- if (nr_of_given_cards == NO_MORE_CARDS_TO_GIVE) { //ponieważ InitiateBattle uznaje gracza, który nie ma kart za przegranego,
- InitiatingBattle(P1, P2,crd_nr, help); //to w tej funkcji zamieniamy kolejność table graczy, żeby wygrał gracz,
- } //który pozbył się wszyskich kart kiedy oddawał je na stos przeciwnika
- else {
- giver = &P2;
- givers_table = &(P2->table);
- }
- }
- else if (CountCards(*P2) < 2) {
- nr_of_given_cards = OpponentsHelp(P2, P1,*crd_nr);
- if (nr_of_given_cards == NO_MORE_CARDS_TO_GIVE) {
- InitiatingBattle(P1, P2,crd_nr, help);//tak samo tutaj
- }
- else {
- giver = &P1;
- givers_table = &(P1->table);
- }
- }
- if (giver != NULL && givers_table != NULL) {
- T_card battle_card = RemoveFromHand(*giver);
- *givers_table = AddToTable(*givers_table, battle_card, (*crd_nr)++);
- (*crd_nr) += 1;
- }
- #ifdef VersionBTEST
- cout << "InitiatingWar P1 table-->";
- for (int i = 0; i < (*crd_nr) / 2; i++) {
- PrintCard(table_P1[i]);
- }
- cout << endl;
- cout << "InitiatingWar P2 table-->";
- for (int i = 0; i < (*crd_nr) / 2; i++) {
- PrintCard(table_P2[i]);
- }
- cout << endl;
- #endif
- InitiatingBattle(P1, P2,crd_nr, help);
- }
- else {
- War(P1, P2, crd_nr, help);
- }
- }
- else if (*help == FALSE) {
- P1->martial_law = TRUE;
- P2->martial_law = TRUE;
- War(P1, P2, crd_nr, help);
- }
- else { cout << "!!!Something is really wrong-->wsk_help not working as it should!!!" << endl; }
- }
- int InitiatingBattle(player* P1, player* P2,int* crd_nr, int* help) {
- if ((P1->moves_left)<0||(P2->moves_left)<0) {
- return INFINITE;
- }
- if (P1->hand_start.next_card == NULL && P2->hand_start.next_card == NULL) {//Obydwu graczom skończą się karty -->remis
- return DRAW;
- }
- if (P1->hand_start.next_card == NULL) {//Player2 wygrywa
- return WIN_P2;
- }
- else if (P2->hand_start.next_card == NULL) {//Player1 wygrywa
- return WIN_P1;
- }
- T_card battle_card_P1, battle_card_P2;
- battle_card_P1.colour = BLANK;
- battle_card_P1.value = ZERO;
- battle_card_P2.colour = BLANK;
- battle_card_P2.value = ZERO;
- if (P1->strategy != NONE) {
- if (P1->martial_law == FALSE) {
- if (P1->chooser != FALSE) { //w tej kolejce wybiera P1
- if (CountCards(*P1) < 2) {
- battle_card_P1 = RemoveFromHand(P1);
- }
- else {
- battle_card_P2 = RemoveFromHand(P2); //dlatego jako pierwszy kartę wykłada P2
- T_card option1 = RemoveFromHand(P1);
- T_card option2 = RemoveFromHand(P1);
- battle_card_P1 = Strategy(P1, option1, option2, battle_card_P2);
- }
- P1->chooser = FALSE;//w następnej kolejce P1 nie ma prawa wybierać
- P2->chooser = TRUE;
- }
- else if (P2->chooser != FALSE) {//w tej kolejce wybiera P2
- if (CountCards(*P2) < 2) {//jeżeli gracz ma wybierać, a została mu tylko jedna karta to tak naprawdę nie ma wyboru
- battle_card_P2 = RemoveFromHand(P2);
- }
- else{
- battle_card_P1 = RemoveFromHand(P1);
- T_card option1 = RemoveFromHand(P2);
- T_card option2 = RemoveFromHand(P2);
- battle_card_P2 = Strategy(P2, option1, option2, battle_card_P1);
- }
- P1->chooser = TRUE;
- P2->chooser = FALSE;//w następnej kolejce znowu wybiera P1
- }
- }
- else {
- battle_card_P1 = RemoveFromHand(P1);
- battle_card_P2 = RemoveFromHand(P2);
- }
- }
- else {
- battle_card_P1 = RemoveFromHand(P1);
- battle_card_P2 = RemoveFromHand(P2);
- }
- if (battle_card_P1.colour != BLANK && battle_card_P2.colour != BLANK) {
- P1->table = AddToTable(P1->table, battle_card_P1, (*crd_nr)++);
- P2->table = AddToTable(P2->table, battle_card_P2, (*crd_nr)++);
- --(P1->moves_left);
- --(P2->moves_left);
- Battle(P1, P2, crd_nr, help);
- }
- }
- void Battle(player* P1, player* P2,int* crd_nr, int* help) {
- const T_card battle_card_P1 = P1->table[(*crd_nr) / 2 - 1];
- const T_card battle_card_P2 = P2->table[(*crd_nr) / 2 - 1];
- PrintCard(battle_card_P1);
- cout << " vs ";
- PrintCard(battle_card_P2);
- cout << endl;
- if (battle_card_P1.value == battle_card_P2.value) {//ta sama value karty
- cout << endl;
- cout << " ***WAR*** " << endl;
- cout << endl;
- InitiatingWar(P1, P2,crd_nr, help);
- }
- else {
- if (battle_card_P1.value > battle_card_P2.value) {//P1 ma starszą kartę
- CardsForWinner(P1, P2, *crd_nr);
- }
- else {//P2 ma starszą kartę
- CardsForWinner(P2, P1,*crd_nr);
- }
- if (P1->versionB == TRUE) {
- *help = true;
- }
- P1->martial_law = FALSE;
- P2->martial_law = FALSE;
- *crd_nr = 0;
- InitiatingBattle(P1, P2,crd_nr, help);
- }
- }
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <iostream>
- #include "wargame.h"
- using namespace std;
- #define ROUNDS 40
- int SelectDecksSize();
- //---------------------------------------------------RANK CHECK ----------------------------------------------
- /*int DetermineCardsRank(const T_card checked_card) {
- if (checked_card.value == ACE) { return 12; }
- else if (checked_card.value == KING) { return 11; }
- else if (checked_card.value == QUEEN) { return 10; }
- else if (checked_card.value == JACK) { return 9; }
- else if (checked_card.value == TEN) { return 8; }
- else if (checked_card.value == NINE) { return 7; }
- else if (checked_card.value == EIGHT) { return 6; }
- else if (checked_card.value == SEVEN) { return 5; }
- else if (checked_card.value == SIX) { return 4; }
- else if (checked_card.value == FIVE) { return 3; }
- else if (checked_card.value == FOUR) { return 2; }
- else if (checked_card.value == THREE) { return 1; }
- else { return 0; }
- }
- int CountMaxPoints(const int number_of_cards) {
- int max_points = 0;
- for (int i = 0; i < (number_of_cards / 2); i++) {
- if (i < 4) { max_points += 12; }
- else if (i < 8) { max_points += 11; }
- else if (i < 12) { max_points += 10; }
- else if (i < 16) { max_points += 9; }
- else if (i < 20) { max_points += 8; }
- else if (i < 24) { max_points += 7; }
- else if (i < 28) { max_points += 6; }
- }
- return max_points;
- }
- int CountMinPoints(const int number_of_cards) {
- int min_points = 0;
- for (int i = number_of_cards / 2; i < number_of_cards; i++) {
- if (i < 16) { min_points += 9; }
- else if (i < 20) { min_points += 8; }
- else if (i < 24) { min_points += 7; }
- else if (i < 28) { min_points += 6; }
- else if (i < 28) { min_points += 6; }
- else if (i < 32) { min_points += 5; }
- else if (i < 36) { min_points += 4; }
- else if (i < 40) { min_points += 3; }
- else if (i < 44) { min_points += 2; }
- else if (i < 48) { min_points += 1; }
- else if (i < 52) { min_points += 0; }
- }
- return min_points;
- }
- int DetermineHandsRank(const int points, const int number_of_cards) {
- int max_points = CountMaxPoints(number_of_cards);
- int min_points = CountMinPoints(number_of_cards);
- int gap = (max_points - min_points) / 12;
- if (points <= min_points + gap) { return 0; }
- else if (points <= min_points + 2 * gap) { return 1; }
- else if (points <= min_points + 3 * gap) { return 2; }
- else if (points <= min_points + 4 * gap) { return 3; }
- else if (points <= min_points + 5 * gap) { return 4; }
- else if (points <= min_points + 6 * gap) { return 5; }
- else if (points <= min_points + 7 * gap) { return 6; }
- else if (points <= min_points + 8 * gap) { return 7; }
- else if (points <= min_points + 9 * gap) { return 8; }
- else if (points <= min_points + 10 * gap) { return 9; }
- else if (points <= min_points + 11 * gap) { return 10; }
- else if (points < min_points + 12 * gap) { return 11; }
- else if (points == max_points) { return 12; }
- }
- int RankCheck(const player Player, const int number_of_cards) {
- int cards_rank = 0;
- T_hand* peek = Player.hand_start.next_card;
- while (peek != NULL) {
- cards_rank += DetermineCardsRank(peek->my_card);
- peek = peek->next_card;
- }
- free(peek);
- int rank = DetermineHandsRank(cards_rank, number_of_cards);
- return rank;
- }
- void AddToRanks(int** rank_tab, int winner_rank, int looser_rank) {
- ((*rank_tab)[winner_rank])++;
- ((*rank_tab)[looser_rank + 1])++;
- }
- void PrintRanks(int* rank_tab) {
- for (int i = 0; i < 13; i++) {
- cout << "RANK " << i << " -->";
- cout << rank_tab[i * 2] << " ";
- cout << rank_tab[i * 2 + 1] << " " << endl;
- }
- cout << endl;
- }
- int* CreateRanksTable() {
- int* hand_rank = (int*)malloc(26 * sizeof(int));
- if (hand_rank != NULL) {
- for (int i = 0; i < 26; i++) {
- hand_rank[i] = 0;
- }
- return hand_rank;
- }
- else { return NULL; }
- }*/
- //---------------------------------------------------RANK CHECK ----------------------------------------------
- void Clear(player* P1, player* P2) {
- free(P1->table);
- free(P2->table);
- while (P1->hand_start.next_card != NULL) {
- RemoveFromHand(P1);
- }
- while (P2->hand_start.next_card != NULL) {
- RemoveFromHand(P2);
- }
- }
- int GameA(player* player1, player* player2, int* opponents_help, const int number_of_cards) {
- player1->table = NULL;
- player2->table = NULL;
- player1->moves_left = 1200;
- player2->moves_left = 1200;
- int cards_on_table = 0, gameA_winner = 0;
- //Rozdanie po raz pierwszy kard + zainicjowanie list graczy
- FirstHandOfCards(player1, player2, number_of_cards);//tworzy,tasuje i rozdaje karty
- /*int rank1 = RankCheck(*player1, number_of_cards);
- int rank2 = RankCheck(*player2, number_of_cards);*/
- gameA_winner = InitiatingBattle(player1, player2,&cards_on_table, opponents_help);
- //sprawdzenie czy gameA_winner zwróciło poprawną wartość
- if (gameA_winner != DRAW && gameA_winner != INFINITE && gameA_winner != WIN_P1 && gameA_winner != WIN_P2) {
- gameA_winner = InitiatingBattle(player1, player2, &cards_on_table, opponents_help);
- }
- Clear(player1, player2);
- //if (gameA_winner == 1) { AddToRanks(rank_table, rank1, rank2); }
- //else if (gameA_winner == 2) { AddToRanks(rank_table, rank2, rank1); }
- return gameA_winner;
- }
- int GameB(player* player1, player* player2, const int number_of_cards) {
- int opponents_help = TRUE;
- player1->versionB = TRUE;
- player2->versionB = TRUE;
- int gameB_winner = GameA(player1, player2, &opponents_help, number_of_cards);
- return gameB_winner;
- }
- T_strategy SelectStrategy() {
- char strategy;
- cout << "Which strategy do you choose for this player?\n\n A) RANDOM \n B) PEACEFUL \n C) AGRESSIVE" << endl;
- cin >> strategy;
- if (strategy == 'A' || strategy == 'a') { return RANDOM; }
- if (strategy == 'B' || strategy == 'b') { return PEACEFUL; }
- if (strategy == 'C' || strategy == 'c') { return AGRESSIVE; }
- else {
- cout << "Your strategy option is invalid. Try one more time" << endl;
- SelectStrategy();
- }
- }
- int GameSmartWar(player* player1, player* player2, int* opponents_help, const int number_of_cards) {
- player1->chooser = TRUE;
- player2->chooser = FALSE;
- int gameSW_winner=0;
- return gameSW_winner= GameA(player1,player2,opponents_help,number_of_cards);
- }
- int SelectDecksSize() {
- int number_of_cards;
- cout << "What size of deck do you choose?\n\n52 cards(full deck)\n48 cards\n44 cards\n40 cards\n36 cards\n32 cards\n28 cards\n24 cards\n20 cards" << endl;
- cin >> number_of_cards;
- if (number_of_cards == 52) { return 52; }
- else if (number_of_cards == 48) { return 48; }
- else if (number_of_cards == 44) { return 44; }
- else if (number_of_cards == 40) { return 40; }
- else if (number_of_cards == 36) { return 36; }
- else if (number_of_cards == 32) { return 32; }
- else if (number_of_cards == 28) { return 28; }
- else if (number_of_cards == 24) { return 24; }
- else if (number_of_cards == 20) { return 20; }
- else {
- cout << "The size of deck you chose is invalid. Try one more time." << endl;
- SelectDecksSize();
- }
- }
- char SelectVersion() {
- char game_version;
- cout << "Which version of WAR would you like to play?\nA)Normal version\nB)Weird version\nS)Smart War" << endl;
- cin >> game_version;
- if (game_version == 'A' || game_version == 'a') { return 'A'; }
- if (game_version == 'B' || game_version == 'b') { return'B'; }
- if (game_version == 'S' || game_version == 's') { return 'S'; }
- else {
- cout << "Your Game option is invalid. Try one more time" << endl;
- SelectVersion();
- }
- }
- int main() {
- srand(time(NULL));
- player player1, player2;
- unsigned long int player1_wins = 0, player2_wins = 0, moves = 0;
- int winner = 0, infinite_games=0, draw = 0;
- int opponents_help = FALSE;
- int game_version = SelectVersion();
- int number_of_cards = SelectDecksSize();
- /*int* ranks_table = CreateRanksTable();
- if (ranks_table == NULL) {
- cout << "There is no space for ranks_table element" << endl;
- return 0;
- }*/
- if (game_version == 'S') {
- cout << "PLAYER 1" << endl;
- player1.strategy = SelectStrategy();
- cout << "PLAYER 2" << endl;
- player2.strategy = SelectStrategy();
- }
- for (int i = 0; i < ROUNDS; i++) {
- if (game_version == 'A') { winner = GameA(&player1, &player2, &opponents_help, number_of_cards); }
- if (game_version == 'B') { winner = GameB(&player1, &player2, number_of_cards); }
- if (game_version == 'S') { winner = GameSmartWar(&player1, &player2, &opponents_help, number_of_cards); }
- if (winner == WIN_P2) {
- player2_wins+=1;
- }
- else if (winner == WIN_P1) {
- player1_wins+=1;
- }
- else if (winner == DRAW) {
- draw+=1;
- }
- else if (winner == INFINITE) {
- infinite_games+=1;
- }
- }
- cout << "Player 1 Won= " << player1_wins << " times" << endl;
- cout << "Player 2 Won= " << player2_wins << " times" << endl;
- cout << "Draws= " << draw << endl;
- cout << "Infinite games " << infinite_games << endl;
- moves = (FunctionCallingCounter() - 1) / 2;
- cout << "Average of moves= " << moves / ROUNDS << endl;
- //PrintRanks(ranks_table);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement