-2

I was creating a card game called broom, my while I need to make sure that all the cards in the hand are played, a card that is played automatically turns into 0 and theoretically, once all the cards in hand become 0 would come out of the while, this unfortunately does not happen, after playing the first card the program closes. I removed the part in which the card changes to 0 and the program does not close, consequently I think the problem is the while. (translated with google)

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int temp = 3;
int deck[40];
string seed[40];
int hand[3];
string seed_hand[3];
int bot_hand[3];
string seed_hand_bot[3];
int table[10];
string seed_table[10];
int choice;
string choice_seed;

void deck_generator();
void shuffle();
void distribution();
void hand_check();

int main()
{
    deck_generator();
    shuffle();
    distribution();
    cout << "----- WELLCOME -----" << endl << "this is just a beta of one hand of -scopa- have fun" << endl;
    cout << endl;
    cout << "Table:" << endl;
    for (int i = 0; i < 4; i++) {
        cout << table[i] <<" " << seed_table[i] << "  ";
    }
    cout << endl << endl;
    cout << "your hand:" << endl;
    for (int i = 0; i < 3; i++) {
        cout << hand[i] <<" " << seed_hand[i] << "  ";
    }

    while (hand[0] != 0 && hand[1] != 0 && hand[2] != 0) {
        cout << endl << "choose a card that you want to play typing the number and then the seed: " << endl;
        cin >> choice;
        cin >> choice_seed;
        hand_check();

        cout << endl;
        cout << "table:" << endl;
        for (int i = 0; i < 10; i++) {
            cout << table[i] << " " << seed_table[i] << "  ";
        }

        cout << endl << endl;
        cout << "your hand:" << endl;
        for (int i = 0; i < 3; i++) {
            cout << hand[i] << " " << seed_hand[i] << "  ";
        }
    }
    return 0;
}

//genera un mazzo completamente ordinato con numeri e semi
void deck_generator() {
    int j = 0;
    for (int i = 0; i < 40; i++) {
        j++;
        deck[i] = j;
        if (j == 10) {
            j = 0;
        }
        if (i < 10) {
            seed[i] = "golds";
        }
        else if (i >= 10 && i < 20) {
            seed[i] = "sticks";
        }
        else if (i >= 20 && i < 30) {
            seed[i] = "swords";
        }
        else if (i >= 30 && i < 40) {
            seed[i] = "cups";
        }
    }
}

// tramite un numero estratto casualmente mischia le carte
void shuffle() {
    int number;
    int random;
    int exchange;
    string swap;
    srand(time(NULL));
    for (int i = 0; i < 40; i++) {
        number = rand() % 40;
        random = rand() % 40;//metto due numeri a confronto e dopo di che faccio delle sostituzioni sia dei semi sia dei numeri
        if (number != random) {
            exchange = deck[number + 1];
            deck[number + 1] = deck[random];
            deck[random] = exchange;

            swap = seed[number + 1];
            seed[number + 1] = seed[random];
            seed[random] = swap;
        }
    }
}

void distribution() {
    int m = 4;
    int n = 8;
    //qui vengono distribuite le prime carte presenti nell'array al giocatore
    for (int i = 0; i < 3; i++) {
        hand[i] = deck[i];
        seed_hand[i] = seed[i];
    }
    //qui metteremo le carte in campo
    for (int i = 0; i < 4; i++) {
        table[i] = deck[m];
        seed_table[i] = seed[m];
        m++;
    }
    //qui daremo le carte al bot
    for (int i = 0; i < 3; i++) {
        bot_hand[i] = deck[n];
        seed_hand_bot[i] = seed[n];
        n++;
    }
}

void hand_check() {
    temp++;
    for (int i = 0; i < 3; i++) {
        if (choice == hand[i] && choice_seed==seed_hand[i]){
            table[temp] = choice; 
            seed_table[temp] = choice_seed;
            hand[i] = 0;
            seed_hand[i] = " ";
        }
    }
}
Ted Lyngmo
  • 60,763
  • 5
  • 37
  • 77
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 27 '22 at 08:29
  • 1
    You're causing undefined behaviour by incrementing `temp` to an invalid index. It is very unclear what the purpose of that variable is. – molbdnilo May 27 '22 at 08:36
  • thanks for the comments anyway the while problem was solved instead of && I just put || and now it works ... regarding wenzel's comment: I can't look at things line by line, because I'm not very familiar with my debugger (visual studio). answer to molbdnilo: temp is needed to find to find a space on the table, in practice it is a counter, with temp I understand where the player plays his card or in a free space, and taking into account the position of that card I can check the other cards for of the possible outlets, a bit difficult to explain, but I assure you it is needed. Google translator – Foxelby May 28 '22 at 10:29

0 Answers0