//================================================
//   File:     hell98.c
//   By:       Roger Z. Rios
//   Date:     Mr 1998
//================================================
//
//   Input:
//      - iter  := number of Montecarlo iterations
//
//   Output:
//      Percentage of times a given team had the 
//      lowest points/games ratio
//
//===============================================
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Forward declarations
int rnd_unif(int& seed, const int, const int);


int main(int argc, char** argv) {

    //
    // Working parameters
    // (these must me modified everyweek)
    int week = 27;
    int tig_pts = 34;
    int cel_pts = 105;
    int pue_pts = 108;
    int ver_pts = 107;

    int tig_p, cel_p, pue_p, ver_p;
    int tig_u, cel_u, pue_u, ver_u;
    
    int iter, p, pmin;

    tig_u = cel_u = pue_u = ver_u = 0;

    // Initialize random numer generator
    int seed = time(NULL);

     //
     //  Reading command line
     //
     char* prog_name = argv[0];
     argc--; // Substract program name
 
     if (argc == 0) {
          printf("Sorry, too few arguments.\n");
          printf("Correct syntax:\n\n");
          printf("%s <w> <n>\n\n", prog_name);
          printf("where <w> is the current week and\n"\
                 "<n> is the number of iterations\n");
          exit(1);
     } else if (argc > 1) {
          printf("Sorry, too many arguments.\n");
          exit(1);
     }

     //
     //  Read <iter>
     //
     sscanf(argv[1], "%d", &iter);
     if (iter < 1) {
          printf("Numero de iteraciones debe ser positivo\n");
          exit(1);
     }

    //
    //   Do <iter> iterations
    //
    for (int i = 0; i < iter; i++) {

        //   Initialize pint counters
        tig_p = tig_pts;
        cel_p = cel_pts;
        pue_p = pue_pts;
        ver_p = ver_pts;

        //  Start at Week + 1

        for (int j = week + 1; j <= 34; j++) {
            //printf("j=\%d\n",j);

            if (j == 30 ) {

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    cel_p += 3;
                 else if (p == 0)
                    tig_p += 3;
                 else {
                    cel_p += 1;
                    tig_p += 1;
                 }

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    pue_p += 3;
                 else if (p == 1)
                    pue_p += 1;

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    ver_p += 3;
                 else if (p == 1)
                    ver_p += 1;

            } else if (j == 33) {

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    cel_p += 3;
                 else if (p == 0)
                    ver_p += 3;
                 else {
                    cel_p += 1;
                    ver_p += 1;
                 }

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    pue_p += 3;
                 else if (p == 1)
                    pue_p += 1;

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    tig_p += 3;
                 else if (p == 1)
                    tig_p += 1;

	    } else if (j == 34) {

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    cel_p += 3;
                 else if (p == 0)
                    pue_p += 3;
                 else {
                    cel_p += 1;
                    pue_p += 1;
                 }

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    tig_p += 3;
                 else if (p == 1)
                    tig_p += 1;

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    ver_p += 3;
                 else if (p == 1)
                    ver_p += 1;

            } else {

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    tig_p += 3;
                 else if (p == 1)
                    tig_p += 1;

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    cel_p += 3;
                 else if (p == 1)
                    cel_p += 1;

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    pue_p += 3;
                 else if (p == 1)
                    pue_p += 1;

                 p = rnd_unif(seed, 0, 2);
                 if (p == 2)
                    ver_p += 3;
                 else if (p == 1)
                    ver_p += 1;

            }

        } // end for (j)

	  /*
        printf("Start of sorting\n");
        printf("tig_p=\%d\n",tig_p);
        printf("cel_p=\%d\n",cel_p);
        printf("pue_p=\%d\n",pue_p);
        printf("ver_p=\%d\n",ver_p);
	*/

        // Compute ratios and sort
        pmin = 3 * tig_p;
        if (cel_p < pmin)
             pmin = cel_p;
        if (pue_p < pmin)
             pmin = pue_p;
        if (ver_p < pmin)
             pmin = ver_p;

        if (3 * tig_p == pmin)
             ++tig_u;
        if (cel_p == pmin)
             ++cel_u;
        if (pue_p == pmin)
             ++pue_u;
        if (ver_p == pmin)
             ++ver_u;

    } // end for (i)

    printf("\nSimulacion Montecarlo con N = %d \n\n", iter);
    printf("Probabilidades de tener peor cociente\n");
    printf("al termino del torneo calculado en la\n");
    printf("jornada %d (%d fechas restantes)\n", week, 34 - week);
    printf("   Tigres:   %.4lf\n", (double) tig_u / (double) iter);
    printf("   Celaya:   %.4lf\n", (double) cel_u / (double) iter);
    printf("   Puebla:   %.4lf\n", (double) pue_u / (double) iter);
    printf("   Veracruz: %.4lf\n", (double) ver_u / (double) iter);
    printf("\n\n");

    return(1); // OK
}

///////////////////////////////////////////////
//     Generates a random number uniformly
//     distributed between <low> and <high>.
//     The value of <seed> is modified.
///////////////////////////////////////////////
int rnd_unif(int& seed, const int low, const int high) {
 
     const int m = 2147483647, a = 16807, b = 127773, c = 2836;
     int k;
     double value_0_1;
 
     k = seed / b;
     seed = a * (seed % b) - k * c;
 
     if (seed < 0)
          seed += m;
 
     value_0_1 = (double) seed / (double) m;
 
     return (low + (int) (value_0_1 * (high - low + 1))); 
}
