Guest User

Untitled

a guest
Apr 22nd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.63 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // prova_joc_cartes.c
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #define MAXJUGS 12
  7. #ifndef CARTA
  8. #define CARTA
  9. #include <stdio.h>
  10. #define BASTOS 0
  11. #define ESPASES 1
  12. #define COPES 2
  13. #define OROS 3
  14. #define MAXCARTES 48
  15.  
  16. typedef struct {
  17.     int pal;
  18.     int figura;
  19. } Tcarta;
  20.  
  21.  
  22. typedef struct{
  23.     int ncartes;
  24.     Tcarta cartes[MAXCARTES];
  25. } Tjoc;
  26.  
  27. void mostra_carta(Tcarta c){
  28.      
  29.     int num, cont;
  30.     char palo, fig;
  31.     cont=0;
  32.  
  33.     if(c.pal == OROS){                                                            
  34.         palo = 'O';                                      
  35.     }
  36.     if(c.pal == BASTOS){                                                          
  37.         palo = 'B';
  38.     }
  39.     if(c.pal == COPES){                                                          
  40.         palo = 'C';
  41.     }
  42.     if(c.pal == ESPASES){                                                        
  43.         palo = 'E';
  44.     }
  45.  
  46.     if(c.figura == 12){                                                          
  47.         fig = 'R';
  48.         cont++;
  49.     }
  50.     if(c.figura == 11){                                                          
  51.         fig = 'C';
  52.         cont++;
  53.     }
  54.     if(c.figura == 10){                                                          
  55.         fig = 'S';
  56.         cont++;
  57.     }
  58.     if(cont != 0){                                                                
  59.        printf("(%c,%c)",fig,palo);                    
  60.     }
  61.     else{
  62.         printf("(%d,%c)",c.figura,palo);
  63.     }
  64. }
  65. void copia_jocs(Tjoc *jdesti,Tjoc jorigen){
  66.      
  67.      int i;
  68.      (*jdesti).ncartes = jorigen.ncartes;                                      
  69.      
  70.      for(i = 0; i < jorigen.ncartes; i++){                                    
  71.           (*jdesti).cartes[i].pal = jorigen.cartes[i].pal;                      
  72.           (*jdesti).cartes[i].figura = jorigen.cartes[i].figura;            
  73.     }
  74. }
  75.  
  76. int ncartes(Tjoc joc){
  77.  
  78.     int c = joc.ncartes;                                                      
  79.     return c;
  80. }
  81.  
  82. void joc_buit(Tjoc *joc){
  83.  
  84.     joc->ncartes=0;                                                            
  85. }
  86.  
  87. void mostra_joc(Tjoc joc){
  88.     int i;
  89.  
  90.     for(i = 0; i < joc.ncartes; i++){                                          
  91.         if( i % 12 == 0){                                                      
  92.            printf("\n");
  93.         }
  94.     mostra_carta(joc.cartes[i]);                                                
  95.     }
  96. }  
  97. int insereix(Tcarta carta,int pos,Tjoc *joc){
  98.    
  99.     int i;
  100.    
  101.     if (pos < MAXCARTES  &&  pos >= 0  ){                
  102.         for(i = joc->ncartes ; i >= pos; i--){                                
  103.             joc->cartes[i] = joc->cartes[i-1];                                  
  104.         }
  105.         joc->ncartes ++;                                                        
  106.         joc->cartes[pos] = carta;                                            
  107.         return 1;
  108.     }
  109.     else return 0;
  110.  
  111. }
  112.  
  113. int extreu (int pos, Tjoc *joc, Tcarta *carta){
  114.  
  115.     int i;
  116.    
  117.     if ( (*joc).ncartes == 0 )                                                
  118.         return(0);
  119.     else {
  120.         if ( (pos < 0) || (pos > (*joc).ncartes) )                              
  121.             return(0);
  122.         else {
  123.             (*carta)=(*joc).cartes[pos];                                        
  124.             for (i = 0; i < ((*joc).ncartes-pos); i++){                        
  125.                 (*joc).cartes[pos+i]=(*joc).cartes[pos+i+1];                    
  126.             }
  127.             (*joc).ncartes--;                                                  
  128.         }
  129.     }
  130.        
  131.     return(1);
  132. }
  133.  
  134. void barreja(Tjoc *joc){
  135.     int i,n,m;
  136.    
  137.     for (i = 0; i < ncartes(*joc); i++) {            
  138.         n = rand()%ncartes(*joc);                       // Posicion aleatoria n
  139.         m = rand()%ncartes(*joc);                       // Posicion aleatoria m
  140.         insereix (joc->cartes[i],n,&*joc);                            // Inserta la carta de i en la posicion n
  141.         extreu (i,&*joc,&joc->cartes[i]);                             // Inserta la carta de m en la posiion m  
  142.                            
  143.     }
  144.     joc->ncartes = ncartes(*joc);
  145. }
  146.  
  147. int talla(int pos,Tjoc *primer, Tjoc *segon){
  148.     int i,f,n;                                              // Numero de cartas de el segundo juego
  149.     joc_buit(&*primer);                           // Inicializamos el primer juego a 0
  150.     if ( pos < 0 || pos > ncartes(*segon) || ncartes(*segon) == 0) return (0);      
  151.     else{
  152.         copia_jocs(&*primer,*segon);                   // Copiamos el segundo juego en el primero      
  153.         for (f = n; i > pos; f--){
  154.              extreu (f,&*primer,&primer->cartes[f]);  // Extrae las cartas desde pos hasta ncartes y las deja en el segundo juego  
  155.         }    
  156.         for (i=0; i < pos; i++){
  157.             extreu (i,&*segon,&segon->cartes[i]);        // Extrae las cartas desde 0 hasta pos y las guarda en el segundo juego
  158.         }
  159.    
  160.     return(1);
  161.     }
  162.  
  163.  
  164. }
  165.  
  166. main(){
  167.     int pos;
  168.     pos = 0;
  169.     Tjoc joc1,joc2,joc10;
  170.     joc_buit(&joc10);  
  171.     Tjoc jugadors[MAXJUGS];
  172.     int i,ncartes,njugs,ok,postall;
  173.     srand(time(0)); // Inicialitza rand()
  174.     Tjoc joc6={3,{{BASTOS,3},{OROS,8},{COPE S,5}}};
  175.  
  176.    
  177.    
  178.    
  179.     printf("Provem barreja \n");
  180.     printf ("Mostrem per pantalla el joc6:");
  181.     mostra_joc(joc6);
  182.     printf("\nBarrejarem el joc6 i el mostrem per pantalla");
  183.     barreja(&joc6);
  184.     mostra_joc(joc6);
  185.     printf ("\n");
  186.    
  187.    
  188.    
  189.     printf ("\nProvem talla \n");
  190.     printf ("Introduiex la posicio per on vols tallar el joc6: ");
  191.     scanf (" %d",&pos);
  192.     ok = talla(pos,&joc10,&joc6);
  193.     if (ok == 0) printf ("Posicio incorrecte\n");
  194.     else {
  195.          printf ("Tallem el joc6 i mostrem les seves dues parts\n");
  196.          talla(pos,&joc10,&joc6);
  197.          printf ("Primera part: \n");
  198.          mostra_joc(joc10);
  199.          printf ("\nSegona part: \n");
  200.          mostra_joc(joc6);
  201.          printf ("\n");
  202.       }
  203. system("pause");   
  204. #endif
  205. }
Add Comment
Please, Sign In to add comment