Search code examples
c++blackjack

How to ask each player if they want another card


Made a couple changes.. But still not working. Now what happens is if I Enter 'y' nothing happens program just freeze there, if i enter 'n', it continues the for loop.

Here's what I have so far. Players are represented by the lines of my array and their hands are cols. Cardtab and CardHandTab is the wchar representing the cards they have EX: 1♥ and the rest is analog array containing the values of each card.

Actually just realized they would all get a card no matter their answer since a do while checks condition after executing the code but that doesn't solve my problem..

void anotherCard(const wchar_t* CardTab[], int ValTab[], const wchar_t* CardHandTab[4][5], int ValHandTab[4][5])
{
    for (int i = 0; i < 4; i++)
    {
        char answer = 0;
        wcout << "Player" << i + 1 << "Would you like another? y/n" << endl;
        cin >> answer;
        while (answer == 'y' || answer == 'Y')
        {
            int x;
            do
            {
                x = rand() % 51 + 0;
            } while (CardTab[x] == NULL);

            CardHandTab[i][3] = CardTab[x];
            ValHandTab[i][3] = ValTab[x];
            CardTab[x] = NULL;
            ValTab[x] = 0;
        }
    }
}

Solution

  •  I forgot it needs a way to exit.. adding a break; fixed it 
    
       void anotherCard(const wchar_t* CardTab[], int ValTab[], const wchar_t* CardHandTab[4][5], int ValHandTab[4][5])
        {
            for (int i = 0; i < 4; i++)
            {
                char answer = 0;
                wcout << "Player" << i + 1 << "Desirez vous une autre carte? y/n" << endl;
                cin >> answer;
                while (answer == 'y' || answer == 'Y')
                {
                    int x;
                    do
                    {
                        x = rand() % 51 + 0;
                    } while (CardTab[x] == NULL);
    
                    CardHandTab[i][3] = CardTab[x];
                    ValHandTab[i][3] = ValTab[x];
                    CardTab[x] = NULL;
                    ValTab[x] = 0;
                    wcout << CardHandTab[i][3];
                    break;
                }
            }
        }