Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // prova_joc_cartes.c
- #include <stdlib.h>
- #include <time.h>
- #define MAXJUGS 12
- #ifndef CARTA
- #define CARTA
- #include <stdio.h>
- #define BASTOS 0
- #define ESPASES 1
- #define COPES 2
- #define OROS 3
- #define MAXCARTES 48
- typedef struct {
- int pal;
- int figura;
- } Tcarta;
- typedef struct{
- int ncartes;
- Tcarta cartes[MAXCARTES];
- } Tjoc;
- void mostra_carta(Tcarta c){
- int num, cont;
- char palo, fig;
- cont=0;
- if(c.pal == OROS){
- palo = 'O';
- }
- if(c.pal == BASTOS){
- palo = 'B';
- }
- if(c.pal == COPES){
- palo = 'C';
- }
- if(c.pal == ESPASES){
- palo = 'E';
- }
- if(c.figura == 12){
- fig = 'R';
- cont++;
- }
- if(c.figura == 11){
- fig = 'C';
- cont++;
- }
- if(c.figura == 10){
- fig = 'S';
- cont++;
- }
- if(cont != 0){
- printf("(%c,%c)",fig,palo);
- }
- else{
- printf("(%d,%c)",c.figura,palo);
- }
- }
- void copia_jocs(Tjoc *jdesti,Tjoc jorigen){
- int i;
- (*jdesti).ncartes = jorigen.ncartes;
- for(i = 0; i < jorigen.ncartes; i++){
- (*jdesti).cartes[i].pal = jorigen.cartes[i].pal;
- (*jdesti).cartes[i].figura = jorigen.cartes[i].figura;
- }
- }
- int ncartes(Tjoc joc){
- int c = joc.ncartes;
- return c;
- }
- void joc_buit(Tjoc *joc){
- joc->ncartes=0;
- }
- void mostra_joc(Tjoc joc){
- int i;
- for(i = 0; i < joc.ncartes; i++){
- if( i % 12 == 0){
- printf("\n");
- }
- mostra_carta(joc.cartes[i]);
- }
- }
- int insereix(Tcarta carta,int pos,Tjoc *joc){
- int i;
- if (pos < MAXCARTES && pos >= 0 ){
- for(i = joc->ncartes ; i >= pos; i--){
- joc->cartes[i] = joc->cartes[i-1];
- }
- joc->ncartes ++;
- joc->cartes[pos] = carta;
- return 1;
- }
- else return 0;
- }
- int extreu (int pos, Tjoc *joc, Tcarta *carta){
- int i;
- if ( (*joc).ncartes == 0 )
- return(0);
- else {
- if ( (pos < 0) || (pos > (*joc).ncartes) )
- return(0);
- else {
- (*carta)=(*joc).cartes[pos];
- for (i = 0; i < ((*joc).ncartes-pos); i++){
- (*joc).cartes[pos+i]=(*joc).cartes[pos+i+1];
- }
- (*joc).ncartes--;
- }
- }
- return(1);
- }
- void barreja(Tjoc *joc){
- int i,n,m;
- for (i = 0; i < ncartes(*joc); i++) {
- n = rand()%ncartes(*joc); // Posicion aleatoria n
- m = rand()%ncartes(*joc); // Posicion aleatoria m
- insereix (joc->cartes[i],n,&*joc); // Inserta la carta de i en la posicion n
- extreu (i,&*joc,&joc->cartes[i]); // Inserta la carta de m en la posiion m
- }
- joc->ncartes = ncartes(*joc);
- }
- int talla(int pos,Tjoc *primer, Tjoc *segon){
- int i,f,n; // Numero de cartas de el segundo juego
- joc_buit(&*primer); // Inicializamos el primer juego a 0
- if ( pos < 0 || pos > ncartes(*segon) || ncartes(*segon) == 0) return (0);
- else{
- copia_jocs(&*primer,*segon); // Copiamos el segundo juego en el primero
- for (f = n; i > pos; f--){
- extreu (f,&*primer,&primer->cartes[f]); // Extrae las cartas desde pos hasta ncartes y las deja en el segundo juego
- }
- for (i=0; i < pos; i++){
- extreu (i,&*segon,&segon->cartes[i]); // Extrae las cartas desde 0 hasta pos y las guarda en el segundo juego
- }
- return(1);
- }
- }
- main(){
- int pos;
- pos = 0;
- Tjoc joc1,joc2,joc10;
- joc_buit(&joc10);
- Tjoc jugadors[MAXJUGS];
- int i,ncartes,njugs,ok,postall;
- srand(time(0)); // Inicialitza rand()
- Tjoc joc6={3,{{BASTOS,3},{OROS,8},{COPE S,5}}};
- printf("Provem barreja \n");
- printf ("Mostrem per pantalla el joc6:");
- mostra_joc(joc6);
- printf("\nBarrejarem el joc6 i el mostrem per pantalla");
- barreja(&joc6);
- mostra_joc(joc6);
- printf ("\n");
- printf ("\nProvem talla \n");
- printf ("Introduiex la posicio per on vols tallar el joc6: ");
- scanf (" %d",&pos);
- ok = talla(pos,&joc10,&joc6);
- if (ok == 0) printf ("Posicio incorrecte\n");
- else {
- printf ("Tallem el joc6 i mostrem les seves dues parts\n");
- talla(pos,&joc10,&joc6);
- printf ("Primera part: \n");
- mostra_joc(joc10);
- printf ("\nSegona part: \n");
- mostra_joc(joc6);
- printf ("\n");
- }
- system("pause");
- #endif
- }
Add Comment
Please, Sign In to add comment