#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
        char name[20];
        double punteggio;
};


int main(int argc, char** argv)
{
    struct student Giocatore = {"Antonio", 0.000};
    printf("Inserire il nome giocatore \r\n");
    scanf("%s", Giocatore.name);
    printf("GIOCATORE %s \r\n",Giocatore.name);
    int stanco = 0;
    int numero = 0;
    int randomnumber = 0;
    double conta_vittorie = 0;
    double conta_sconfitte = 0;
    float percentuale;

    int decisione = 0;
    while(stanco == 0)
    {
            do{
                printf("Indovina un numero da 0 a 9 \r\n");
                scanf("%d", &numero);
                if(numero<0 || numero >10)
                {
                    printf("Inserito valore errato \r\n");
                } }while (numero<0 || numero >10);
            randomnumber = rand()%10;
            printf("Il numero scelto e': %d ed il numero casuale e': %d \r\n", numero, randomnumber);
            if(randomnumber == numero)
            {
                printf("Hai indovinato! \r\n");
                conta_vittorie ++;
            }
            else
            {
                printf("Hai sbagliato! \r\n");
                conta_sconfitte++;
            }

            printf("\r\n VITTORIE %d \r\n SCONFITTE %d \r\n Sei stanco? 1-SI Other-NO\r\n", conta_vittorie, conta_sconfitte);
            scanf("%d", &decisione);
            if(decisione == 1)
            {
                stanco = 1;
            }


    }

    Giocatore.punteggio = (conta_vittorie)/(conta_vittorie + conta_sconfitte)*100;
    printf("Percentuale Vittorie: %f \r\n", Giocatore.punteggio);


    int n = 0;
    struct student giocatore[100];
    int q=1;
    FILE *fd;
    int trovato=0;
    fd = fopen("record.dat", "rw");
    if(fd != NULL)
    {

            fread(&n, sizeof(int), 1, fd);
            q = n;
            for(int i=0; i<n; i++)
            {

                fread(&giocatore[i], sizeof(giocatore[i]), 1, fd);
            }

            for(int i=0; i<n; i++)
            {
                if(strcmp(giocatore[i].name,Giocatore.name)==0)
                    {

                        giocatore[i].punteggio = Giocatore.punteggio;
                        trovato = 1;
                    }
            }

            if (trovato ==0)
            {
                q++;
                giocatore[n] = Giocatore;


            }



            fclose(fd);

            fopen("record.dat", "w");
            fwrite(&q, sizeof(int),1,fd);
            for(int j=0; j<q; j++)
            {
                fwrite(&giocatore[j], sizeof(giocatore[j]),1,fd);
            }
            fclose(fd);


    }
    else
    {

        fd = fopen("record.dat", "w");
        fwrite(&q, sizeof(int),1,fd);
        fwrite(&Giocatore, sizeof(Giocatore), 1, fd);
        giocatore[0] = Giocatore;
        fclose(fd);
    }


    for (int k=0; k<q; k++)
    {

        printf("%s %f",giocatore[k].name, giocatore[k].punteggio);
    }

}