• [^] # Re: Mélange des cartes

    Posté par . En réponse au message Jeu de poker pour Linux.. Évalué à -3.

    Merci pour vos critiques,

    en faîte j'ai implémenter les algorithmes de mélange comme nous ont mélanges les cartes quand ont joue au cartes en famille.

    C.A.D il n'y a aucune base mathématique, afin d'obtenir une dispersion des cartes efficace.
    Donc oui j'ai simuler comment un humains mélange les cartes.

    Les 3 algorithmes sont issue de techniques de mélange que je connaît ou que j'ai inventer pour le programme.

    il aurait suffi de réassigner un nouvel ordre : on tire 32 nombres aléatoires ente 0 et 1, et on utilise l'ordre du vecteur pour réordonner le paquet

    J'ai pas bien compris si vous avez lus le code ou pas, mais le jeu de carte est basé sur une structure de donnée de type FILO (Stack) et les algorithmes de mélange utilisent aussi en complément une structure de type FIFO (Queue), issue de l'étude de ce bouquin.

    Je vous invite simplement a lire le code:

    int get_rand(int modulo) {
     /** Simply random function. **/
     struct timeval tv ;
     gettimeofday(&tv,NULL) ;
     return (int) ((rand_r((unsigned int *) &tv.tv_usec) % modulo) < 0) ? 0 : (rand_r((unsigned int *) &tv.tv_usec) % modulo) ;
    }
    
    
    void mix_card_game_classic(Stack *card_game) {
     /** Mix the card game by taken an random card from the game and reinsert in the game at an random position. **/ 
     int c ;
     for (c=0 ; c < 32 ; c++) {
     int cc ;
     ListElt *card_to_rm = list_head(card_game) ;
     ListElt *card_to_add = list_head(card_game) ; 
     /** Compute an random card taking position. **/
     for (cc=0 ; cc < get_rand(32) ; cc++ ) {
     card_to_rm=list_next(card_to_rm) ;
     }
     /** Taken one card from the game. **/
     Card *card_datas = malloc(sizeof(Card)) ;
     if (list_remove_next((List *) card_game,card_to_rm,(void *) &card_datas) != 0) {
     /** This should never append. **/
     continue ;
     }
     usleep(get_rand(25)) ; /** For random utils. **/
     /** Compute an random insert position. **/
     for (cc=0 ; cc < get_rand(32) ; cc++ ) {
     card_to_add=list_next(card_to_add) ;
     }
     /** Reinsert the card in the game. **/
     if (list_insert_next((List *) card_game,card_to_add,card_datas) != 0) {
     /** This should never append. **/
     exit(EXIT_FAILURE) ;
     }
     usleep(get_rand(25)) ; /** For random utils. **/
     }
     return ;
    }
    void mix_card_game_heaps(Stack *card_game) {
     /** Mix algorithm which construct 5 cards heaps randomly and push it in an different order to reconstruct the card game. **/
     Queue mix_heaps[5] ;
     uint8_t c ;
     uint8_t sum=0 ;
     uint8_t sizes[5] ;
     for (c=0 ; c < 5 ; c++) {
     init_queue(&mix_heaps[c],NULL) ;
     }
     for (c=0 ; c < 5 ; c++) {
     int cc, limit ;
     if (c != 4) {
     limit=get_rand(8) ;
     }
     else {
     limit= (32-sum < 0) ? 0 : 32-sum ;
     }
     if (limit == 0 && c != 4) {
     limit=2 ;
     }
     sizes[c]=limit ;
     sum += limit ;
     for (cc=0 ; cc < limit ; cc++) {
     Card *card = malloc(sizeof(Card)) ;
     /** Take a card from the card game. **/
     if (stack_pop(card_game,(void *) &card) != 0) {
     /** This should not append. **/
     continue ;
     }
     /** Push the taken card on the current stack. **/
     enqueue(&mix_heaps[c],card) ;
     }
     usleep(get_rand(55)) ;
     }
     for (c=0 ; c < sizes[4] ; c++) {
     /** Flush the heap 5 and put it content inn the card game. **/
     Card *card ;
     /** Take a card from the heap. **/
     dequeue(&mix_heaps[4],(void *) &card) ;
     /** Set card in the game. **/
     stack_push(card_game,card) ;
     }
     for (c=0 ; c < sizes[2] ; c++) {
     /** Flush the heap 3 and put it content inn the card game. **/
     Card *card ;
     /** Take a card from the heap. **/
     dequeue(&mix_heaps[2],(void *) &card) ;
     /** Set the card in the game. **/
     stack_push(card_game,card) ;
     }
     for (c=0 ; c < sizes[0] ; c++) {
     /** Flush the heap 1 and put it content inn the card game. **/
     Card *card ;
     /** Take a card from the heap. **/
     dequeue(&mix_heaps[0],(void *) &card) ;
     /** Set card in the game. **/
     stack_push(card_game,card) ;
     }
     for (c=0 ; c < sizes[3] ; c++) {
     /** Flush the heap 4 and put it content inn the card game. **/
     Card *card ;
     /** Take a card from the heap. **/
     dequeue(&mix_heaps[3],(void *) &card) ;
     /** Set the card in the game. **/
     stack_push(card_game,card) ;
     }
     for (c=0 ; c < sizes[1] ; c++) {
     /** Flush the heap 2 and put it content inn the card game. **/
     Card *card ;
     /** Take a card from the heap. **/
     dequeue(&mix_heaps[1],(void *) &card) ;
     /** Set the card in the game. **/
     stack_push(card_game,card) ;
     }
     return ;
    }
     void mix_card_game_alternate(Stack *card_game) {
     /** Mix algorithm wich construct 2 heaps from the card game
     * and alterantively take an card from an heap, and put it in the card game.
     ****************************************************************************/ 
     Queue *half_game_1 = malloc(sizeof(Queue)) ;
     Queue *half_game_2 = malloc(sizeof(Queue)) ;
     init_queue(half_game_1,NULL) ;
     init_queue(half_game_2,NULL) ;
     uint8_t c ;
     for (c=0 ; c < 32 ; c++) {
     /** Loop to construct the two heaps. **/
     Card *card ;
     /** Take an card from the card game. **/
     stack_pop(card_game,(void *) &card) ;
     if (c % 2 == 0) {
     /** push the card on the first heap. **/
     enqueue(half_game_1,card) ;
     }
     else {
     /** push the card on the second heap. **/
     enqueue(half_game_2,card) ;
     }
     }
     for (c=0 ; c < 32 ; c++) {
     Card *card ;
     if (c % 2 == 0) {
     /** Take a card from the heap one. **/
     dequeue(half_game_2,(void *) &card) ;
     }
     else {
     /** Take a card from the heap two. **/
     dequeue(half_game_1,(void *) &card) ;
     }
     /** Push the card on the card game. **/
     stack_push(card_game,card) ;
     }
     free(half_game_1) ;
     free(half_game_2) ;
     return ;
    }
    

    Après je suis assez fiers de mes algorithmes, malgré vos critiques sévères.

    En fin de compte les cartes sont mélangés car en début le jeu de cartes est générer de manière ordonner et puis après le mélange elle sont mélangées (les cartes ne se suivent plus du tout.).

    Concernant la rapidité et l'optimisation, j'ai bien fait attention pour que les animations s'exécute de manière optimisée, mais a part les animations le jeu n'a pas besoin d'être optimiser en matière de rapidité d'exécution comme cela doit l'être pour la plupart des jeux.

    Merci pour vos critiques.