vendredi 5 mai 2023

this is my football championship simulation school project where every team must play once a day and 1 match only against all the other teams randomly

so i had a problem that goes like this supposing i have 6 teams this is how the tournament would go(a,b,c,d,e,f) day 1 a-b c-d e-f day 2 a-d c-b and now the problem happens cuz only teams e and f are left and i cant form a match using these 2 teams

what i tried was making a matrix daytable[amountOfTeams/2][2] where i have to fill all the matrix before start assigning scores and points... but now the teams are playing more than one match a day this is my code

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct equipe{
    char nom[30];
    int adver[19];
    int points ;
    int ts;
}Equipe;
     void define(Equipe *league,int a){
         //defines the clubs
        int i;
        for(i=0;i<a;i++){
            printf("enter the name of the club number %d\n",i+1);
            Equipe pi;
            league[i] =  pi;
            scanf("%s",league[i].nom);
            league[i].points=0;
            printf("the %d club is %s \n",i+1,league[i].nom);}}
     bool nonplayed(Equipe *league,int kk,int first,int second){
         //to check if the teams have already played against each other
        int nc;
        for(nc=0;nc<kk;nc++){
            if(league[first].adver[nc]==second||league[second].adver[nc]==first){
                return true;
            }}
        return false;
    }
     bool checkday(int d[],int dsi,int cl){
         //to check if the teams played this day
        for(int di=0;di<dsi;di++){
            if(d[di]==cl){
                return true;
            }
        }
        return false;
    }
     void pointcount(Equipe *league,int fir,int sec){
         //counts points
      if(league[fir].ts>league[sec].ts){
        league[fir].points+=3;
      }else if(league[fir].ts==league[sec].ts){
        league[fir].points+=1;
        league[sec].points+=1;
      }else{
        league[sec].points+=3;
      }
     }
     void order(Equipe *league,int a){
         //orders the clubs depending on their points just for visuals
      Equipe mom,classement[a];
      int oi,oj;
      for(oi=0;oi<a;oi++){
        classement[oi]=league[oi];}
        oi = 1 ;
    while (oi < a){
        oj = oi;
        while (oj > 0 && classement[oj-1].points > classement[oj].points){
            mom=classement[oj];
            classement[oj]=classement[oj-1];
            classement[oj-1]=mom;
            oj = oj - 1;}
        oi = oi + 1;}
        for(oi=a-1;oi>=0;oi--){
            printf("%d:%s points:%d \n",a-oi,classement[oi].nom,classement[oi].points);
        }
     }
     void day(Equipe *league,int a,int kk,bool work){
         //simulates a day of the championship
        int f,s,j,sta,fal=0;
        int dp[a];
        int daytable[a/2][2];
        for(j=0;j<a/2;j++){
        do{
        f =rand()%a;
        s =rand()%a;
        fal++;
        if(fal>5000){
            break;
            work=false;
        }
        }while(f==s||checkday(dp,(j*2),s)||nonplayed(&league,kk,f,s)||checkday(dp,(j*2),f));
        daytable[j][1]=f;
        daytable[j][2]=s;
        dp[(j*2)]=f;
        dp[(j*2)+1]=s;}
        printf("ra7 nprinti tableau \n");
        for(j=0;j<a/2;j++){
            printf("%d %d \n",daytable[j][1],daytable[j][2]);
        }
        for(j=0;j<a/2;j++){
        league[daytable[j][1]].ts=rand()%10;
        league[daytable[j][2]].ts=rand()%10;
        printf("%s %d - %d %s\n",league[daytable[j][1]].nom,league[daytable[j][1]].ts,league[daytable[j][2]].ts,league[daytable[j][2]].nom);
        pointcount(league,daytable[j][1],daytable[j][2]);
        league[daytable[j][1]].adver[kk]=daytable[j][2];
        league[daytable[j][2]].adver[kk]=daytable[j][1];
        }
        order(league,a);
    }



int main()
{
    int a,i,kk;
    bool work;
        kk=0;
        printf("how many clubs do you want in your league\n");
        do{
        scanf("%d",&a);
        }while(a%2!=0||a>20||a<4);
        Equipe league[a];
        define(league,a);
        for(i=0;i<a-1;i++){
            matchstart:
            work=true;
            printf("press any key to continue \n");
            getch();
            printf("day %d\n",i+1);
            day(league,a,kk,work);
            if(work==false){
                goto matchstart;
            }
            kk++;}
            for(i=0;i<a-1;i++){
            printf("%s\n",league[league[0].adver[i]].nom);
            ;}
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire