torosvacas.cpp 2.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#include <bits/stdc++.h>
#define N 4
#define INTENTOS 12

void separar_numeros(int vector[N], int numero);
int aleatorio(int x, int y);
bool tiene_cuatro_digitos(int n);

using namespace std;



int main(){

	srand(time(NULL));

	int numero_pensado[N];
	int numero_input[N];
	int input;

	bool error = false;

	for(int i=0; i<N; i++){
		int temp = aleatorio(1,9);

		for(int j=0; j<(i+1); j++){
			
			if(numero_pensado[j] == temp){
				temp = aleatorio(1,9);
				j=0;
			}

		}

		numero_pensado[i] = temp;
	}


	for(int i=0; i<N; i++){
		cout << numero_pensado[i];
	}
	cout << endl;

	cout << "Ya pensé en número!" << endl;

	



	int toros=0;
	int vacas=0;
	for (int i = 0; i < INTENTOS; i++)
	{
		
		do{
			cout << "INTENTO B 0" << i+1 << " : ";
			
			toros = 0;
			vacas = 0;

			
			cin >> input;
			if(!tiene_cuatro_digitos(input)){
				cout << "Jugada Inválida -  Número Inválido\n";
				error = true;
				//i--;
				continue;
			}else{
				error = false;
				
			
				separar_numeros(numero_input, input);


				for (int j = 0; j < N; j++)
				{
					for(int k=j+1; k<N; k++){
						if(numero_input[j]==numero_input[k]){
							cout << "Jugada Inválida - Número con dígitos repetidos" << endl;
							error = true;
							j=N;
						}
					}
				}
			}

		}while(error);



		int iguales=0;
		for (int j = 0; j < 4; j++)
		{
			if(numero_pensado[j] == numero_input[j]){
				iguales++;
			}
		}

		if(iguales == 4){
			cout << "FIN DEL JUEGO - GANÓ B en ";
			cout << i+1 << " INTENTOS" << endl;
			return 0;
		}

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if(numero_pensado[i]==numero_input[j]){
					if(i == j){
						toros++;
					}else{
						vacas++;
					}
				}
			}
		}

		cout << "RESPUESTA A: ";
		cout << toros << " TOROS, ";
		cout << vacas << " VACAS" << endl;

	}

	cout << "SUPERASTE LOS " << INTENTOS << " INTENTOS" << endl;
	cout << "PERDISTE B)" << endl;



	return 0;
}


//función para generar numeros aleatorios
int aleatorio(int x, int y){	
	return int(x + rand()% (y+1-x));
}


//función para separar los digitos en un vector
void separar_numeros(int vector[N], int numero){
	int dividendo= 1000;

	for(int i=0; i<N; i++){
		int x = numero/dividendo;
		vector[i] = x;
		numero = numero - (x*dividendo);
		dividendo = dividendo/10;
	}


}

bool tiene_cuatro_digitos(int n){

	if(int(n/1000) != 0 and int(n/10000) ==0){
		return true;
	}

	return false;


}