Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <math.h>
- #include <stdlib.h>
- #include <string>
- using namespace std;
- void adivinar_numero();
- void adivinar_numero(){
- srand(time(NULL)); // cada vez que se ejecute el programa el valor de a va estar a cambiando
- int numero_secreto = rand()%10+1; // uso la libreria math para gener un numero aleatorio
- int intentos= 0; // este sera el numero de intentos , esta variable tiene un valor de 2
- //cout << numero_secreto << endl; //no mostremos el numero secreto o no sera secreto XD
- int numero_ingresado;
- cout << "Adivina un numero del 1 al 10: " << endl;
- while(intentos<3){
- intentos++;
- cout << "Este es el intento numero " << intentos <<" :";
- cin>>numero_ingresado;
- if(numero_ingresado == numero_secreto){
- cout << "Adivinaste el numero" << endl;
- break;
- }
- else if(intentos == 3){
- cout << "perdiste" << endl;
- }
- }
- }
- int main(){
- char opcion;
- do{
- adivinar_numero();
- cout << "desea jugar otra vez y/n: ";
- }while(cin>>opcion && opcion == 'y');
- /*
- char opcion; // respuesta
- do { // loop principal
- adivinar_numero();
- cout <<"Jugar otra vez? [y/n]: ";
- }while(cin >> opcion and tolower(opcion)=='y');
- */
- cin.get();
- return 0;
- }
- /*version de Daniel
- #include <iostream>
- #include <ctime> // en C++ las librerias C comienzan con c y pierden el .h
- // #include <math.h> // <cmath>
- #include <cstdlib>
- //
- #include <chrono> // high_resolution_clock
- #include <random>
- using namespace std;
- // se que la definicion de la funcion es un poco "expert friendly"
- // la libreria random es bastante sofisticada y explicarla tomaria un
- // tutorial (mas adelante). Por los momentos usarla cuando se necesite numeros
- // aleatoreos en [menor:mayor]
- int rand_int(int menor, int mayor)
- {
- using clck = chrono::high_resolution_clock;
- static mt19937 re{}; // pseudo random number generator
- using distribution = uniform_int_distribution<int>; // distribucion uniforme
- re.seed(static_cast<mt19937::result_type>(clck::to_time_t(clck::now())));
- static distribution dist{menor, mayor};
- return dist(re);
- }
- //void adivinar_numero(); <-- redundante
- void adivinar_numero(){
- //int numero_secreto = rand()%10+1; // uso la libreria math para gener un numero aleatorio
- int numero_secreto = rand_int(1, 10);
- //int intentos= 0; // este sera el numero de intentos , esta variable tiene un valor de 2
- cout << numero_secreto << endl; //no mostremos el numero secreto o no sera secreto XD
- //int numero_ingresado;
- //este loop se puede hacer de varias formas
- int nro = -1; // lo inicializo con un numero fuera de rango
- for(int intento=1; intento != 4 and nro != numero_secreto; ++intento)
- {
- cout << "Adivina un numero del 1 al 10 (intento #" << intento << "): ";
- cin >> nro;
- }
- // aqui no sabemos como termino el loop
- if(nro == numero_secreto)
- cout << "Adivinaste el numero!!!\n";
- else cout << "Perdiste!\n";
- }
- int main(){
- char opcion; // respuesta
- do { // loop principal
- adivinar_numero();
- cout <<"Jugar otra vez? [y/n]: ";
- }while(cin >> opcion and tolower(opcion)=='y');
- // informa al usuario que debe presionar una tecla
- cout << "Adios!! Presione enter....";
- cin.get();
- // return 0; <-- si el programa llega hasta aquí entonces return 0; es implicito
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement