Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Sorting bug (http://www.programmingforums.org/showthread.php?t=9004)

Cabochon Mar 23rd, 2006 1:00 PM

Sorting bug
 
Hi. At College we've been asked to make an application in C that sort a card game. No big deal, but have made a sorting function that sorting strangely...
It's a Bubblesort.

Can you help me to fix it? Here's the code, sorry, but the name of variables are in french. I put in bold the bugging Bubblesort function.
Thanks a lot.
:

#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>

#define AFFICHE 'A'
#define BRASSE 'B'
#define CLOCHE '\a'
#define TRI 'T'
#define FIN 27

enum Couleur {Coeur=3, Carreau, Trefle, Pique};
enum Valeur {Deux=2, Trois, Quatre, Cinq, Six, Sept, Huit, Neuf, Dix, Valet, Dame, Roi, As};
struct Carte
{
  enum Valeur valeur ;
  enum Couleur couleur ;
};

void fullscreen(void);
void initialiseJeu(struct Carte jeu[]);
void afficheJeu(struct Carte jeu []);
void brasseJeu(struct Carte jeu []);
void triJeu(struct Carte jeu []);
int menu(void);

//struct Carte carte, jeuCarte[52];
//enum Valeur valeur;
//enum Couleur couleur;

int main(int argc, char* argv[])
{
  struct Carte jeuCarte[52];
  int choix;

  fullscreen();
  randomize();
  initialiseJeu(jeuCarte);
  afficheJeu(jeuCarte);
  do
  {
    choix = menu();
    switch(choix)
    {
      case AFFICHE  : putch(choix);
                      getch();
                      afficheJeu(jeuCarte);
                      getch();
                      break;
      case BRASSE  : putch(choix);
                      getch();
                      brasseJeu(jeuCarte);
                      break;
      case TRI      : putch(choix);
                      getch();
                      triJeu(jeuCarte);
                      break;
      case FIN      : break;
      default      : putch(CLOCHE);
    }
  } while (choix != FIN);
  //system("pause");
  return 0;

}
void initialiseJeu(struct Carte jeu[])
{
  int i=0;
  enum Couleur c;
  enum Valeur v;
  for (c=Coeur;c<=Pique;c=c+1)
  {
    for (v=Deux;v<=As;v=v+1)
    {
    jeu[i].couleur=c;
    jeu[i].valeur=v;
    i++;
    }
  }
}
void afficheJeu(struct Carte jeu [])
{
  int i=0;
  enum Couleur c;
  enum Valeur v;
  clrscr();
  for (c=Coeur;c<=Pique;c=c+1)
  {
    for (v=Deux;v<=As;v=v+1)
    {
/*    if(jeu[i].couleur==Coeur || jeu[i].couleur==Carreau)
        textcolor(RED);
      else
        textcolor(BLACK); */
      switch(jeu[i].valeur)
      {
        case Valet  : cprintf("%3c",'J');
                      break;
        case Dame  : cprintf("%3c",'D');
                      break;
        case Roi    : cprintf("%3c",'K');
                      break;
        case As    : cprintf("%3c",'A');
                      break;
        default    : cprintf("%3d",jeu[i].valeur);
                      break;
      }
      cprintf("%c",jeu[i].couleur);
      i++;
    }
    cputs("\n\r");
  }
}
void brasseJeu(struct Carte jeu [])
{
  struct Carte c;
  int i, j;

  for (i=0;i<52;i++)
  {
    j=random(52);
    c = jeu[i];
    jeu[i] = jeu[j];
    jeu[j] = c;
  }
}

void triJeu(struct Carte jeu [])
{
        int i  = 0;
        int j  = 0;
        int tmp = 0;
        int permut;

        permut = 1;
        for(i = 0 ; (i < 52) && permut==1; i++)
        {
                permut = 0;
                for(j = 1 ; j < 52 ; j++)
                {

                        if(jeu[j].couleur < jeu[j-1].couleur)
                        {
                                tmp = jeu[j-1].couleur;
                                jeu[j-1].couleur = jeu[j].couleur;
                                jeu[j].couleur = tmp;
                                permut = 1;
                        }
                        else if(jeu[j].valeur < jeu[j-1].valeur)
                        {
                                tmp = jeu[j-1].valeur;
                                jeu[j-1].valeur = jeu[j].valeur;
                                jeu[j].valeur = tmp;
                                permut = 1;
                        }

                }
        }
}


int menu(void)
{
  int touche;
  clrscr();
  //textbackground(BLUE);
  //textcolor(RED);
  gotoxy(33,5);
  cputs("Jeu de cartes");
  gotoxy(25,10);
  cputs("Affiche le jeu de cartes");
  gotoxy(25,15);
  cputs("Brasse le jeu de cartes");
  gotoxy(25,20);
  cputs("Tri le jeu de cartes");
  gotoxy(25,25);
  cputs("Quitter le jeu de cartes");
  cputs("(Esc)");
  gotoxy(35,40);
  cputs("Choix: ");
  touche = toupper(getch());
  return touche;
}
void fullscreen(void)
{
  keybd_event(VK_MENU,0x38,0,0);
  keybd_event(VK_RETURN,0x1c,0,0);
  keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
  keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
  window(0,0,80,50);
}


DaWei Mar 23rd, 2006 2:08 PM

I would observe thusly: If I had a card whose color or value put it out of sort, I would sort the card. I would not scrape off the number or the symbol and paste it on another card.

Cabochon Mar 23rd, 2006 2:14 PM

Thanks, but I don't really understand what you mean.....

jim mcnamara Mar 23rd, 2006 2:43 PM

I removed everything except what was needed to show you how to sort -
:

#include <stdlib.h>
#include <ctype.h>

#define AFFICHE 'A'
#define BRASSE 'B'
#define CLOCHE '\a'
#define TRI 'T'
#define FIN 27

enum Couleur {Coeur=3, Carreau, Trefle, Pique};
enum Valeur {Deux=2, Trois, Quatre, Cinq, Six, Sept, Huit, Neuf, Dix, Valet, Dame, Roi, As};
struct Carte
{
  enum Valeur valeur ;
  enum Couleur couleur ;
};

void initialiseJeu(struct Carte jeu[]);
void triJeu(struct Carte jeu [],int num);

int main(int argc, char* argv[])
{
  struct Carte jeuCarte[52];
  initialiseJeu(jeuCarte);
  triJeu(jeuCarte, 52);
  return 0;

}
void initialiseJeu(struct Carte jeu[])
{
  int i=0;
  enum Couleur c;
  enum Valeur v;
  for (c=Coeur;c<=Pique;c=c+1)
  {
    for (v=Deux;v<=As;v=v+1)
    {
    jeu[i].couleur=c;
    jeu[i].valeur=v;
    i++;
    }
  }
}
int compar(void *un, void *deux)
{  /* sort by Coleur, valeur */
  struct Carte *a=(struct Carte *)un;
  struct Carte *b=(struct Carte *)deux;
  int diff=0;
  if (a->couleur == b->couleur)
  { /* same suit */
      diff=a->valeur - b->valeur;
  }
  else
  {    /* different suit (couleur) */
                  diff=a->couleur - b->couleur;
  }
  return diff;   
}


void triJeu(struct Carte jeu [], int num)
{
        qsort(jeu,num,sizeof(struct Carte),compar);       
}


Cabochon Mar 23rd, 2006 2:57 PM

Thanks, but i paste it on a blank file and it bug on this line :
:

        qsort(jeu,num,sizeof(struct Carte),compar);

Borland give me a huge error of missmatch of parameters....


Thanks!

Cabochon Mar 23rd, 2006 3:27 PM

Forget about it, a classmate had found the solution.

:

void triJeu(struct Carte jeu [])
  {
  int i  = 0;
        int j  = 0;
        enum Couleur tmp;
        int tm;
        int permut;

        permut = 1;
        for(i = 0 ; (i < 52) && permut==1; i++)
        {
                permut = 0;
                for(j = 1 ; j < 52 ; j++)
                {

                        if(jeu[j].couleur < jeu[j-1].couleur)
                        {
                                tmp = jeu[j-1].couleur;
                                jeu[j-1].couleur = jeu[j].couleur;
                                jeu[j].couleur = tmp;
                                tm = jeu[j-1].valeur;
                                jeu[j-1].valeur = jeu[j].valeur;
                                jeu[j].valeur = tm;
                                permut = 1;
                            }
                }
        }
        permut = 1;
      for (i = 13;i<53;i=i+13){

        do {
        permut = 1;
                for (j=(i-13);j<(i-1);j++){
                if(jeu[j].valeur > jeu[j+1].valeur)
                        {
                                tm = jeu[j].valeur;
                                jeu[j].valeur = jeu[j+1].valeur;
                                jeu[j+1].valeur = tm;
                                permut = 0;
                        }
                }
        } while (!permut);
      }

}


See ya next time.

The Dark Mar 23rd, 2006 3:50 PM

You could remove your second loop if you change your test in the first loop from
:

if(jeu[j].couleur < jeu[j-1].couleur)
to
:

if((jeu[j].couleur *100 + jeu[j].valeur) < (jeu[j-1].couleur * 100 + jeu[j-1].valeur))

This makes the first loop work out what order the cards should go in by suit and value. The "* 100" weighting for the couleur is pretty arbitrary, it could be any value above 14.

jim mcnamara Mar 24th, 2006 5:42 PM

qsort is supported by Borland compilers....

Jimbo Mar 24th, 2006 7:58 PM

Quote:

Originally Posted by jim mcnamara
qsort is supported by Borland compilers....

qsort is also supported by VC++. Also, the OP specified that they were using bubblesort, which is probably part of the assignment, so recommending a quicksort probably isnt very useful...

bl00dninja Mar 25th, 2006 2:10 AM

well, it would probably not be allowed for the assignment.


All times are GMT -5. The time now is 5:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC