Card game in which from 2 to 10 people.
For the game over 4 people takes a new deck.

Tell me please:

  • How to distribute cards so that each player has at least one card of EVERY suit?
  • How to remove those cards that have already been distributed?

I try to use struct Card in which the suit and value of the card is stored. My method so far only randomly distributes suits (numbers from 1 to 4) and the cards themselves (from 1 to 13). + a method which shows these cards.

void dealCards( Card playersCardsData[MAX_NUM_OF_PLAYERS][NUM_OF_CARDS], int numOfPlayers ) { for( int player = 0; player < numOfPlayers; player++ ) { for( int card = 0; card < NUM_OF_CARDS; card++ ) { playersCardsData[player][card].suit = rand() % 4 + 1; //generates random numbers from 1 till 4 --> faces printf( "%2d | ", playersCardsData[player][card].suit ); playersCardsData[player][card].value = rand() % 12 + 2; //generates random numbers from 2 till 14 //printf("%2d |", playersCardsData[player][card].value); } //inner for printf( "\n" ); } //outer for } void diplayPlayersCards( Player players[MAX_NUM_OF_PLAYERS], Card playersCardsData[MAX_NUM_OF_PLAYERS][NUM_OF_CARDS], int numOfPlayers ) { printf( "PLAYERS DATA \n" ); printf( "%3s %10s |", " ", " " ); for( int i = 0; i < NUM_OF_CARDS; i++ ) { printf( "%2d |", i + 1 ); } printf( "\n----------------------------------------------------------------------------------------------\n" ); for( int playerIndex = 0; playerIndex < numOfPlayers; playerIndex++ ) { printf( "%3s %10s |", " ", players[playerIndex].name ); for( int card = 0; card < NUM_OF_CARDS; card++ ) { switch( playersCardsData[playerIndex][card].suit ) { case 1: printf( "%2c", 'H' ); //hearts break; case 2: printf( "%2c", 'D' ); //diamonds break; case 3: printf( "%2c", 'S' ); //spades break; case 4: printf( "%2c", 'C' ); //clubs break; } if( 1 < playersCardsData[playerIndex][card].value && playersCardsData[playerIndex][card].value < 11 ) { printf( "%2d |", playersCardsData[playerIndex][card].value ); } else { switch( playersCardsData[playerIndex][card].value ) { case 11: printf( "%2c |", 'J' ); //jack break; case 12: printf( "%2c |", 'Q' ); //queen break; case 13: printf( "%2c |", 'K' ); //king break; case 14: printf( "%2c |", 'A' ); //ace break; } } } // cards printf( "\n" ); } printf( "\n" ); } 
  • one
    I would first divide the cards into suits and randomly select one card of each suit for each player. Then - the remnants would be mixed and distributed the remaining number of cards already randomly from this residue. - Harry
  • "How to remove those cards that have already been dealt?" - what are you talking about? Remove from where ? - AnT

2 answers 2

I understand you just take, and randomly generate a "card" and give it to the player. But at the same time, since this is a random process, the cards are repeated - in which there is nothing surprising. Your mistake is that this process is too random - you do not have a deck! You need to first create a deck, enter values ​​into it, and distribute cards from it. In this case, you will need another variable - the number of cards in the deck. Now I will explain the algorithm:

 Π° = количСство ΠΊΠ°Ρ€Ρ‚ Π² ΠΊΠΎΠ»ΠΎΠ΄Π΅ b = rand () % a раздаваСмая ΠΊΠ°Ρ€Ρ‚Π° = array [b] array [b] = array [a-1] a-- 

I came to this algorithm when I wrote a program that creates sudoku. Thus, your cards will never be repeated. As for the distribution of each suit - it will be a little more complicated. I think for this you will need to create a deck not as a one-dimensional array, but with four lines that symbolize the deck. Then you pull out the first four cards in turn from each row, and the next from a pseudo-random row and pseudo-random column. To prevent the resulting cards from hurting the eyes of the player, shuffle them, and only then show them to the player.

PS That code with random pulling from the deck can not be used (unless it is only for the choice of suit in the case of a four-line array), but first to shuffle it and draw cards in order.

    So, what ideas can come in handy from here, it will work on both C and C ++, your task is to redraw in a modern way using OOP.

    You can try to do on the array of strings using the methods of their processing, or use lists ... or like this ..

     #include <time.h> #include <stdlib.h> #include <stdio.h> using namespace std; int players_cards[5][14]; // [][] ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Π°, ΠΊΠ°Ρ€Ρ‚Ρ‹ Π½Π° Ρ€ΡƒΠΊΠ°Ρ… ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² // [][0] Π²Ρ‹Π΄Π°Π½ΠΎ ΠΊΠ°Ρ€Ρ‚ ΠΏΠΎ мастям // [0][0]количСство ΠΏΠΎΠ»Π½ΠΎΡΡ‚ΡŒΡŽ Π²Ρ‹Π΄Π°Π½Π½Ρ‹Ρ… мастСй // [0][] Π—Π°Ρ€Π΅Π·Π΅Ρ€Π²ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ int players = 6; int deals = 2; // количСство Ρ€Π°Π·Π΄Π°Ρ‡ послС ΠΏΠ΅Ρ€Π²ΠΎΠΉ void dispenser(int s, int p){ // Π²Ρ‹Π΄Π°Ρ‘ΠΌ ΠΊΠ°Ρ€Ρ‚Ρƒ ΠΈΠ³Ρ€ΠΎΠΊΡƒ int offset = rand() % (13 - players_cards[s][0]); // ΠΏΠΎ случайному ΡΠΌΠ΅Ρ‰Π΅Π½ΠΈΡŽ int card = 1; while(players_cards[s][card] || offset){ // ΠΈΠ· списка ΠΈΠΌΠ΅ΡŽΡ‰ΠΈΡ…ΡΡ ΠΊΠ°Ρ€Ρ‚ if(!players_cards[s][card]) offset --; card++; } players_cards[s][card] = p; if (++players_cards[s][0] >= 13) players_cards[0][0]++; // Ссли Π½Π΅ ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ ΠΊΠ°Ρ€Ρ‚ этой масти, ΠΈΡ… Π²Ρ‹Π±ΠΎΡ€ сокращаСтся } int main(void) { char cards[] = "2 3 4 5 6 7 8 9 0 JQKA"; char suits[] = " HDCS"; srand(time(NULL)); for(int i = 0 ; i <= 4; i++) // обнуляСм ΠΌΠ°ccΠΈΠ² Π²Ρ‹Π΄Π°Π½Π½Ρ‹Ρ… ΠΊΠ°Ρ€Ρ‚ for(int j = 0 ; j <= 13; j++) players_cards[i][j] = 0; for(int p = 1 ; p <= players ; p++) // ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡƒ ΠΈΠ³Ρ€ΠΎΠΊΡƒ for(int suit = 1; suit <= 4; suit++) // ΠΊΠ°ΠΆΠ΄ΠΎΠΉ масти ΠΏΠΎ 1 dispenser(suit, p); for(int k = 0; k < deals && players_cards[0][0] < 4; k++) // ΠΈ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Ρ‚ΠΎ Ρ‡Ρ‚ΠΎ ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ Ρ€Π°Π·Π΄Π°Ρ‘ΠΌ K Ρ€Π°Π· for(int p = 1 ; p <= players && players_cards[0][0] < 4; p++){// ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡƒ ΠΈΠ³Ρ€ΠΎΠΊΡƒ int offset = rand() % (4 - players_cards[0][0]); // ΠΏΠΎ случайному ΡΠΌΠ΅Ρ‰Π΅Π½ΠΈΡŽ int suit = 1; while(players_cards[suit][0] >= 13 || offset){ // ΠΈΠ· списка ΠΈΠΌΠ΅ΡŽΡ‰ΠΈΡ…ΡΡ мастСй (Π²Ρ‹Π΄Π°Π½ΠΎ <13) if(players_cards[suit][0] < 13) offset--; suit++; } dispenser(suit, p); } printf(" | %s\n",cards); printf("------------------------------------------\n"); for(int i = 1 ; i <= 4; i++){ printf("%c|",suits[i]); for(int j = 1 ; j <= 13; j++) if (players_cards[i][j] > 0) printf("%2d ", players_cards[i][j]); else printf(" "); printf("| All cards: %d\n", players_cards[i][0]); } return 0; } 

    Result: (this is a matrix of cards with player numbers)

      | 2 3 4 5 6 7 8 9 0 JQKA ------------------------------------------ H| 4 3 1 4 3 5 2 6 3 6 | All cards: 10 D| 4 1 3 6 2 1 5 5 6 5 | All cards: 10 C| 5 4 3 2 4 2 6 1 | All cards: 8 S| 1 5 2 4 1 6 3 2 | All cards: 8 

    Example here: IDEONE

    • thanks to all!!! - Lena Makarenko