Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2006, 12:00 PM   #1
Cabochon
Newbie
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0 Cabochon is on a distinguished road
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);
}
Cabochon is offline   Reply With Quote
Old Mar 23rd, 2006, 1:08 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 23rd, 2006, 1:14 PM   #3
Cabochon
Newbie
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0 Cabochon is on a distinguished road
Thanks, but I don't really understand what you mean.....
Cabochon is offline   Reply With Quote
Old Mar 23rd, 2006, 1:43 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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);	
}
jim mcnamara is offline   Reply With Quote
Old Mar 23rd, 2006, 1:57 PM   #5
Cabochon
Newbie
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0 Cabochon is on a distinguished road
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 is offline   Reply With Quote
Old Mar 23rd, 2006, 2:27 PM   #6
Cabochon
Newbie
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0 Cabochon is on a distinguished road
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.
Cabochon is offline   Reply With Quote
Old Mar 23rd, 2006, 2:50 PM   #7
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Mar 24th, 2006, 4:42 PM   #8
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
qsort is supported by Borland compilers....
jim mcnamara is offline   Reply With Quote
Old Mar 24th, 2006, 6:58 PM   #9
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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...
Jimbo is offline   Reply With Quote
Old Mar 25th, 2006, 1:10 AM   #10
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
well, it would probably not be allowed for the assignment.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:59 PM.

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