wu-e002.cpp 3.38 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
#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);
void generar_hmtl(int np[], char ganador);

using namespace std;

vector<int> respuestas;

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; 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);
				respuestas.push_back(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;
			generar_hmtl(numero_pensado, 'B');
			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;
	generar_hmtl(numero_pensado, 'A');



	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;
}


void generar_hmtl(int np[], char ganador){

	FILE *archivo; //Crear un puntero a archivo
	archivo = fopen("toros_y_vacas.html", "w"); //abre el archivo en escritura

	fprintf(archivo,"<html>\n\
	<head>\n\
		<title>Toros y Vacas</title>\n\
		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n\
	</head>\n\
	<body>\n\
		<h1>Toros y vacas</h1>\n\
		<p>El número elegido por jugador A fue: %d%d%d%d<br/>\n\
		Los intentos de B fueron:<br/>\n", np[0], np[1], np[2], np[3]);
	cout << respuestas.size() << endl;
	cout << respuestas[0] << endl;
	cout << respuestas[1] << endl;
	
	for(int i=0; i<respuestas.size(); i++){
		fprintf(archivo,"		%d. %d<br/>\n", i, respuestas[i]);
		cout << i << endl;
	}

	fprintf(archivo,"		El ganador fue %c\n", ganador);

	fprintf(archivo,"		</p>\n\
	</body>\n\
</html>");
	

	fclose(archivo); //cierra el archivo
}