Advertisement
Mastercpp

adivinar el numero secreto mejorado final

Aug 15th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  #include <string>
  6. using namespace std;
  7.  
  8. void adivinar_numero();
  9.  
  10. void adivinar_numero(){
  11.  
  12.         srand(time(NULL)); // cada vez que se ejecute el programa el valor de a va estar a cambiando
  13.  
  14.         int numero_secreto = rand()%10+1; // uso la libreria math para gener un numero aleatorio
  15.         int intentos= 0; // este sera el numero de intentos  , esta variable tiene un valor de 2  
  16.         //cout << numero_secreto << endl; //no mostremos el numero secreto o no sera secreto XD
  17.  
  18.         int numero_ingresado;
  19.         cout << "Adivina un  numero del 1 al 10: " << endl;
  20.                                
  21.         while(intentos<3){
  22.                 intentos++;                                                              
  23.                 cout << "Este es el intento numero " << intentos <<" :";
  24.                 cin>>numero_ingresado;
  25.                
  26.                  if(numero_ingresado == numero_secreto){
  27.                         cout << "Adivinaste el numero" << endl;
  28.                         break;
  29.                 }
  30.                 else if(intentos == 3){
  31.                         cout << "perdiste" << endl;
  32.                 }
  33.  
  34.         }
  35. }
  36.  
  37. int main(){
  38.         char opcion;
  39.         do{
  40.             adivinar_numero();
  41.             cout << "desea jugar otra vez y/n: ";
  42.         }while(cin>>opcion && opcion == 'y');
  43.        
  44.         /*
  45.            
  46.         char opcion; // respuesta
  47.         do { // loop principal
  48.                 adivinar_numero();
  49.                 cout <<"Jugar otra vez? [y/n]: ";
  50.         }while(cin >> opcion and tolower(opcion)=='y');
  51.  
  52.         */
  53.         cin.get();
  54.         return 0;
  55. }
  56.  
  57.  
  58. /*version de Daniel
  59. #include <iostream>
  60. #include <ctime> // en C++ las librerias C comienzan con c y pierden el .h
  61. // #include <math.h> // <cmath>
  62. #include <cstdlib>
  63. //
  64. #include <chrono> // high_resolution_clock
  65. #include <random>
  66.  
  67.  
  68. using namespace std;
  69. // se que la definicion de la funcion es un poco "expert friendly"
  70. // la libreria random es bastante sofisticada y explicarla tomaria un
  71. // tutorial (mas adelante). Por los momentos usarla cuando se necesite numeros
  72. // aleatoreos en [menor:mayor]
  73. int rand_int(int menor, int mayor)
  74. {
  75.     using clck = chrono::high_resolution_clock;
  76.     static mt19937 re{}; // pseudo random number generator
  77.     using distribution = uniform_int_distribution<int>; // distribucion uniforme
  78.     re.seed(static_cast<mt19937::result_type>(clck::to_time_t(clck::now())));
  79.     static distribution dist{menor, mayor};
  80.     return dist(re);
  81.  
  82. }
  83.  
  84. //void adivinar_numero(); <-- redundante
  85.  
  86. void adivinar_numero(){
  87.  
  88.        
  89.         //int numero_secreto = rand()%10+1; // uso la libreria math para gener un numero aleatorio
  90.         int numero_secreto = rand_int(1, 10);
  91.         //int intentos= 0; // este sera el numero de intentos  , esta variable tiene un valor de 2
  92.         cout << numero_secreto << endl; //no mostremos el numero secreto o no sera secreto XD
  93.  
  94.         //int numero_ingresado;
  95.         //este loop se puede hacer de varias formas
  96.         int nro = -1; // lo inicializo con un numero fuera de rango
  97.         for(int intento=1; intento != 4 and nro != numero_secreto; ++intento)
  98.         {
  99.             cout << "Adivina un  numero del 1 al 10 (intento #" << intento << "): ";
  100.             cin >> nro;
  101.         }
  102.  
  103.         // aqui no sabemos como termino el loop
  104.         if(nro == numero_secreto)
  105.             cout << "Adivinaste el numero!!!\n";
  106.         else cout << "Perdiste!\n";
  107.  
  108. }
  109.  
  110. int main(){
  111.  
  112.         char opcion; // respuesta
  113.         do { // loop principal
  114.                 adivinar_numero();
  115.                 cout <<"Jugar otra vez? [y/n]: ";
  116.         }while(cin >> opcion and tolower(opcion)=='y');
  117.  
  118.         // informa al usuario que debe presionar una tecla
  119.         cout << "Adios!! Presione enter....";
  120.         cin.get();
  121.        // return 0; <-- si el programa llega hasta aquí entonces return 0; es implicito
  122. }
  123.  
  124. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement