Advertisement
Kyrexar

Patata caliente v0.4

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