Skip to content
Snippets Groups Projects
initialisation.c 3.37 KiB
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "initialisation.h"

int numJoueur;
// TODO bug if input a string
int inputYN()
{
    char tmp;
    scanf("%c", &tmp);
    getchar();
    return (tmp == 'y' || tmp == 'Y');
}
void presenceJoueur(Joueur* current)
{
    while (1)
    {
        printf("\nVeuillez le joueur \"%s\" presenter devant l'ecran!\n",
               current->nom);
        printf("Vous etes le joueur \"%s\"? Y/N ", current->nom);
        if (inputYN())
            break;
    }
}
void MultiSuivant(int i)
{
    while (1)
    {
        if(i==1)
        {
            printf("Passer au joueur suivant? Y/N ");
            if (inputYN())break;
        }
        if(i==2)
        {
            printf("\nPasser au manche suivant? Y/N ");
            if (inputYN())break;
        }

        if(i==3)
        {
            printf("Passer a la partie suivante? Y/N ");
            if (inputYN())break;
        }

    }
    return;
}

void inputPlayersNum()
{
    printf("Veuillez-vous saisir le nombre des joueurs au debut: ");
    scanf("%d", &numJoueur);
    while (numJoueur > MAX_JOUEURS || numJoueur <= 1)
    {
        printf("Le nombre de joueurs n'est pas valide!");
        printf("Veuillez-vous resaisir: ");
        scanf("%d", &numJoueur);
        getchar();
    }
    getchar();
}

Joueur* saisirJoueur()
{
    printf("\nVeuillez-vous numeroter selon votre position.\n");
    printf("Appuyer sur Enter pour finir la saisie.\n");
    printf("Remarque: La personne a votre gauche correspond a numero suivant.\n");
    Joueur *p,*head;


    for (int i = 1; i < numJoueur + 1; i++)
    {
        if (i == 1)
        {
            p = (Joueur*) malloc(sizeof(Joueur));
            p->nombreDes=5;// on peut le modifier pour faciliser le test
            head = p;
        }
        else
        {
            p->next = (Joueur*) malloc(sizeof(Joueur));
            p = p->next;
            p->nombreDes=5;
        }
        printf("\nLe nom du joueur N %d: ", i);
        fgets(p->nom, 20, stdin);
        p->nom[strlen(p->nom) - 1] = 0;

    }
    p->next = head;

    return head;
}

/**
 * Return an integer number between [1, max]
 */
int randInt(int max)
{
    return (rand() % max + 1);
}

Joueur* getPremierJoueur(Joueur* head)
{
    int num = randInt(numJoueur) - 1;
    printf("\nOn a choisi par harsard le numero %d comme le permier joueur\n",
           num + 1);
    return getJoueur(head, num);
}

Joueur* getJoueur(Joueur* head, int num)
{
    Joueur *tmp = head;
    for (int i = 0; i < num; i++)
        tmp = tmp->next;
    return tmp;
}

void distribuerDes(Joueur* p)
{

    printf("\nNous commencons a distribuer les des.\n");
    printf("Le premier joueur \"%s\" doit etre devant l'ecran.\n", p->nom);

    for (int i = 0; i < numJoueur; i++)
    {
        presenceJoueur(p);

        printf("\nVotre resultat est: ");
        for (int j = 0; j < p->nombreDes; j++)
        {
            p->des[j] = randInt(6);
            (p->des[j]==1)?printf("paco "):printf("%d ", p->des[j]);

        }
        printf("\n");

        while (i < numJoueur-1)
        {
            MultiSuivant(1);
            break;
        }
        if(i==numJoueur-1)
        {
            printf("\nVeuillez-vous appuyer sur Enter pour passer a l'etape de pari.");
            getchar();
        }
        p = p->next;
        system("clear");
    }
    printf("Tous les des sont distribues aux joueurs.\n");
}