Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // Contiene getpid (Obtener procesos)
- #include <stdlib.h> // Contiene rand y srand
- #include <time.h> // Contiene time, localtime y strftime
- #define RANGO 100
- #define VIDAS 9
- int patata( int trial, int jug, int num ){
- int v;
- srand(time(NULL)); // Si se usa como recurso aleatorio el getpid()
- // Da todo el rato el mismo numero
- if( jug == 1 ) num = (rand()%RANGO)+1;
- for( v=VIDAS ; (v>=0 && num!=trial) ; v-- ){
- scanf("%d",&trial);
- if( num<trial && v!=0 ) printf(" Menor que %d (Intentos restantes: %d) ",trial,v);
- if( num>trial && v!=0 ) printf(" Mayor que %d (Intentos restantes: %d) ",trial,v);
- if( v==0 && num!=trial ) printf(" \n Has fallado, lo siento ");
- }
- if( num==trial )printf(" \n Correcto! Era %d ",num);
- return v;
- }
- int main(){
- FILE *records=fopen("Records patata caliente (2J).txt","a");
- int jug, v1, v2, num, trial=0, retry;
- char jug1[4], jug2[4];
- do{
- do{
- system("cls");
- printf(" \n -= La patata caliente =- \n 1 o 2 Jugadores? ");
- scanf("%d",&jug);
- }while( jug!=1 && jug!=2 && jug!=0 );
- if( jug==0 ) return 0; // Salida desde el menu
- // MODO: UN JUGADOR
- if( jug==1 ){
- printf(" \n Averigua que numero estoy pensando entre 0 y 100: ");
- patata(trial,jug,num);
- }
- // MODO: DOS JUGADORES
- if( jug==2 ){
- // INICIALES JUGADORES
- printf(" \n Jugador 1, introduce tus iniciales: ");
- scanf("%s",jug1);
- printf(" \n Jugador 2, introduce tus iniciales: ");
- scanf("%s",jug2);
- // JUGADOR 1 JUEGA
- do{
- system("cls");
- printf(" \n -= Turno de %s =- ",jug1);
- printf(" \n %s, introduce un numero del 1 al %d cuando %s no mire: ",jug2,RANGO,jug1);
- scanf("%d",&num);
- }while( num<1 || num>RANGO );
- system("cls");
- printf(" \n Bien, adivina el numero, %s: ",jug1);
- v1=patata(trial,jug,num);
- system("PAUSE");
- // JUGADOR 2 JUEGA
- do{
- system("cls");
- printf(" \n -= Turno del %s =- ",jug2);
- printf(" \n %s, introduce un numero del 1 al %d cuando %s no mire: ",jug1,RANGO,jug2);
- scanf("%d",&num);
- }while( num<1 || num>RANGO );
- system("cls");
- printf(" \n Bien, adivina el numero, %s: ",jug2);
- v2=patata(trial,jug,num);
- system("PAUSE");
- // RESULTADOS
- system("cls");
- if(v1>v2){
- printf(" \n El ganador es %s con %d intentos ",jug1,VIDAS-v1);
- fprintf(records,"%s con %d puntos gano a %s con %d puntos el ",jug1,10*v1,jug2,10*v2);
- }
- if(v2>v1){
- printf(" \n El ganador es %s con %d intentos ",jug2,VIDAS-v2);
- fprintf(records,"%s con %d puntos gano a %s con %d puntos el ",jug2,10*v2,jug1,10*v1);
- }
- if(v1==v2){
- printf(" \n Empate con %d intentos ",VIDAS-v1);
- fprintf(records,"Hubo un empate entre %s Y %s a %d puntos el ",jug1,jug2,10*v1);
- }
- // SE APUNTAN LOS RECORDS CON FECHA
- time_t tiempo = time(NULL);
- struct tm *tlocal = localtime(&tiempo);
- char output[128];
- strftime(output,128,"%d/%m/%y a las %H:%M:%S",tlocal);
- fprintf(records,"%s\n",output);
- fclose(records);
- }
- printf(" \n Volver al menu(1=si)? ");
- scanf("%d",&retry);
- }while( retry==1 );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement