![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 5
Rep Power: 0
![]() |
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);
} |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 5
Rep Power: 0
![]() |
Thanks, but I don't really understand what you mean.....
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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);
} |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 5
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2006
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
You could remove your second loop if you change your test in the first loop from
if(jeu[j].couleur < jeu[j-1].couleur) 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. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
qsort is supported by Borland compilers....
|
|
|
|
|
|
#9 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|