Advertisement
Kyrexar

Patata caliente v0.5

May 16th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h> // Contiene getpid (Obtener procesos)
  2. #include <stdlib.h> // Contiene rand y srand
  3. #include <time.h> // Contiene time, localtime y strftime
  4.  
  5. #define RANGO 100
  6. #define VIDAS 9
  7.  
  8. int patata( int trial, int jug, int num ){
  9.     int v;
  10.    
  11.     srand(time(NULL)); // Si se usa como recurso aleatorio el getpid()
  12.                        // Da todo el rato el mismo numero
  13.     if( jug == 1 ) num = (rand()%RANGO)+1;
  14.  
  15.     for( v=VIDAS ; (v>=0 && num!=trial) ; v-- ){
  16.        scanf("%d",&trial);
  17.        if( num<trial && v!=0 ) printf(" Menor que %d (Intentos restantes: %d) ",trial,v);
  18.        if( num>trial && v!=0 ) printf(" Mayor que %d (Intentos restantes: %d) ",trial,v);
  19.        if( v==0 && num!=trial ) printf(" \n Has fallado, lo siento ");
  20.     }
  21.     if( num==trial )printf(" \n Correcto! Era %d ",num);
  22.     return v;
  23. }
  24.  
  25. int main(){
  26.     FILE *records=fopen("Records patata caliente (2J).txt","a");
  27.     int jug, v1, v2, num, trial=0, retry;
  28.     char jug1[4], jug2[4];
  29.    
  30.     do{
  31.        do{
  32.           system("cls");
  33.           printf(" \n -= La patata caliente =- \n 1 o 2 Jugadores? ");
  34.           scanf("%d",&jug);
  35.        }while( jug!=1 && jug!=2 && jug!=0 );
  36.        if( jug==0 ) return 0; // Salida desde el menu
  37.  
  38. // MODO: UN JUGADOR
  39.        if( jug==1 ){
  40.           printf(" \n Averigua que numero estoy pensando entre 0 y 100: ");
  41.           patata(trial,jug,num);
  42.           }
  43.  
  44. // MODO: DOS JUGADORES
  45.        if( jug==2 ){
  46.  
  47. // INICIALES JUGADORES
  48.            printf(" \n Jugador 1, introduce tus iniciales: ");
  49.            scanf("%s",jug1);
  50.            printf(" \n Jugador 2, introduce tus iniciales: ");
  51.            scanf("%s",jug2);
  52.  
  53. // JUGADOR 1 JUEGA
  54.            do{
  55.               system("cls");
  56.               printf(" \n -= Turno de %s =- ",jug1);
  57.               printf(" \n %s, introduce un numero del 1 al %d cuando %s no mire: ",jug2,RANGO,jug1);
  58.               scanf("%d",&num);
  59.            }while( num<1 || num>RANGO );
  60.            system("cls");
  61.            printf(" \n Bien, adivina el numero, %s: ",jug1);
  62.            v1=patata(trial,jug,num);
  63.            system("PAUSE");
  64.  
  65. // JUGADOR 2 JUEGA
  66.            do{
  67.               system("cls");
  68.               printf(" \n -= Turno del %s =- ",jug2);
  69.               printf(" \n %s, introduce un numero del 1 al %d cuando %s no mire: ",jug1,RANGO,jug2);
  70.               scanf("%d",&num);
  71.            }while( num<1 || num>RANGO );
  72.            system("cls");
  73.            printf(" \n Bien, adivina el numero, %s: ",jug2);
  74.            v2=patata(trial,jug,num);
  75.            system("PAUSE");
  76.  
  77. // RESULTADOS
  78.            system("cls");
  79.            if(v1>v2){
  80.               printf(" \n El ganador es %s con %d intentos ",jug1,VIDAS-v1);
  81.               fprintf(records,"%s con %d puntos gano a %s con %d puntos el ",jug1,10*v1,jug2,10*v2);
  82.            }
  83.            if(v2>v1){
  84.               printf(" \n El ganador es %s con %d intentos ",jug2,VIDAS-v2);
  85.               fprintf(records,"%s con %d puntos gano a %s con %d puntos el ",jug2,10*v2,jug1,10*v1);
  86.            }
  87.            if(v1==v2){
  88.               printf(" \n Empate con %d intentos ",VIDAS-v1);
  89.               fprintf(records,"Hubo un empate entre %s Y %s a %d puntos el ",jug1,jug2,10*v1);
  90.            }
  91.        
  92. // SE APUNTAN LOS RECORDS CON FECHA
  93.            time_t tiempo = time(NULL);
  94.            struct tm *tlocal = localtime(&tiempo);
  95.            char output[128];
  96.            strftime(output,128,"%d/%m/%y a las %H:%M:%S",tlocal);
  97.            fprintf(records,"%s\n",output);
  98.            fclose(records);
  99.        }    
  100.        printf(" \n Volver al menu(1=si)? ");
  101.  
  102.        scanf("%d",&retry);
  103.     }while( retry==1 );
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement