Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C] Problema gioco del poker

    Ciao a tutti,
    per cambiare vi porto un problema del mio testo. La prima parte è tratta da un esempio che io dovevo modificare. Per dichiarazione del testo stesso non è molto efficente ma non deve essere modificata. Quello che ho aggiunto io sono le funzioni coppia, doppia coppia, etc. contenute nella funz result. Il programma è ok, so che è un po' lungo ma vorrei solo qlch consiglio per migliorarlo.
    Tnks
    ps. non conosco ancora le struct
    codice:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle (int wDeck[][13]);
    void deal (int wDeck[][13], const char *wFace[], const char *wSuit[], 
               int *oneSuit, int *oneFace, 
               int *twoSuit, int *twoFace);
    void result (int *seed, int *pict, int *score);
    
    int main()
    {
       const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
       const char *face[13] = 
           {"Ace", "Deuce", "Three", "Four", 
            "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Jack", "Queen", "King"};
       int pOneSuit[4] = {0}, pOneFace[13] = {0};
       int pTwoSuit[4] = {0}, pTwoFace[13] = {0};
       int deck[4][13] = {0};
       int onePoint = 0, twoPoint = 0;
    
       srand (time(0));
    
       shuffle (deck);
       deal (deck, face, suit, pOneSuit, pOneFace, pTwoSuit, pTwoFace);
       
       printf ("Player one has: ");
       result (pOneSuit, pOneFace, &onePoint);
       if (onePoint == 0)
          printf ("Nothing");
       
       printf ("\n");
       
       printf ("Player two has: ");
       result (pTwoSuit, pTwoFace, &twoPoint);
       if (twoPoint == 0)
          printf ("Nothing");
          
       printf ("\n");
       
       if (onePoint > twoPoint){
          printf ("--------------\n");
          printf ("Player one win\n");
          printf ("--------------\n");
       }
       else if (twoPoint > onePoint){
          printf ("--------------\n");
          printf ("Player two win\n");
          printf ("--------------\n");
       }   
       system ("pause");
    
       return 0;
    }
    void shuffle (int wDeck[][13])
    {
       int row;
       int column;
       int card;
    
       for (card = 1; card <= 52; card++){
    
          do{
             row = rand() % 4;
             column = rand() % 13;
          }while (wDeck[row][column] != 0);
    
          wDeck[row][column] = card;
       }
    }
    void deal (int wDeck[][13], const char *wFace[], const char *wSuit[], 
               int *oneSuit, int *oneFace, 
               int *twoSuit, int *twoFace)
    {
       int card;
       int row;
       int column;
       
       printf ("%15s%22s", "Player One", "Player Two\n\n");
    
       for (card = 1; card <= 10; card++){
    
          for (row = 0; row <= 3; row++){
    
             for (column = 0; column <= 12; column++){
    
                if (wDeck[row][column] == card){
                   if ((card % 2) == 0){
                      ++twoSuit[row];
                      ++twoFace[column];
                   }
                   else{
                      ++oneSuit[row];
                      ++oneFace[column];
                   }
                   printf ("%8s of %-8s", wFace[column], wSuit[row]);
                   
                   if ((card % 2) == 0)
                      printf ("\n");
                }
             }
          }
       }
       printf ("\n");
    }
    void result (int *seed, int *pict, int *score)
    {
         int pair (int *pict);
         int twoPair (int *pict);
         int threeOfKind (int *pict);
         int poker (int *pict);
         int straight (int *pict);
         int color (int *seed);
         
         *score += pair (pict);
         *score += twoPair (pict);
         *score += threeOfKind (pict);
         *score += poker (pict);
         *score += straight (pict);
         *score += color (seed);
         
    }
    int pair (int *pict)
    {
         int card, fPair, count = 0;
         
         for (card = 0; card <= 12; card++){
             if (pict[card] == 2){
             fPair = card;
             ++count;
             if (pict[card] == 3)
                return 0
                ;
             }
         }
         
         if (count == 1){
            printf ("Pair");
            return 1 + fPair;
         }
         else return 0;
    }
    int twoPair (int *pict)
    {
         int card, count = 0, fPair, sPair;
         
         for (card = 0; card <= 12; card++){
             if (pict[card] == 2 && count == 1){
                sPair = card;
                count++;
             }
             else if (pict[card] == 2 && count == 0){
                count++;
                fPair = card;
             }
         }
         
         if (count == 2){
            printf ("Two Pair");
            return 20 + sPair;
         }
         else return 0;
    }
    int threeOfKind (int *pict)
    {
         int card, tris;
         
         for (card = 0; card <= 12; card++){
             if (pict[card] == 3){
                tris = card;
                printf ("Three of a kind");
                return 40 + tris;
             }
         }
    }
    int poker (int *pict)
    {
         int card, pok;
         
         for (card = 0; card <= 12; card++){
             if (pict[card] == 4){
                pok = card;
                printf ("Poker!!!");
                return 100 + pok;
             }
         }
         return 0;
    }
    int straight (int *pict)
    {
         int card, count = 0, firstPict, lastPict;
         
         for (card = 0; card <= 12; card++){
             if (pict[card] == 1){
                ++count;
                if (count == 1)
                   firstPict = card;
                if (count == 5)
                   lastPict = card;
             }
         }
             
         if ((lastPict - firstPict) == 4 && count == 5){
            printf ("Straight");
            return 60 + lastPict;
         }
         else return 0;
    }
    int color (int *seed)
    {
         int card;
         
         for (card = 0; card <= 3; card++){
             if (seed[card] == 5){
                printf ("Color");
                return 80 + card;
             }
         }
         return 0;
    }

  2. #2

    We

    Ciao Phantaman!
    Usi il mio stesso libro!!!!

    Tornando all esercizio, anche a me e venuto un listato lungo, proprio perche non si possono usare le struct!!!!! Quindi direi che anke il tuo va bene!

    Saluti!
    The Beginner

  3. #3

    Re: We

    Originariamente inviato da UltraBeginner
    Ciao Phantaman!
    Usi il mio stesso libro!!!!

    Tornando all esercizio, anche a me e venuto un listato lungo, proprio perche non si possono usare le struct!!!!! Quindi direi che anke il tuo va bene!

    Saluti!
    The Beginner
    Ciao,
    lo avevo notato e ti ho mandato un pvt i giorni scorsi, ma evidentemente non l'hai visto. Io ho trovato questa soluzione, ma non so quanto sia efficente. Da un'occhiata al mess e fammi sapere.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.