Vote count:
0
Hello, I am just starting learning programming with javascript and i made a "game" where the computer generates a random number and you should guess it, however when i press the button to start playing the browser freezes like if there is an infinite loop. I tryed removing the loop "Do" and the game worked fine, however i can't get working the counter that registers the number of attempts made by the user before finding the correct number, because without loop every time i click the button "Go" in order to run the script the variable "counter" gets back to 0.., the text in the game is in Spanish, if you can't understand howthe program works because of it please tell me and i will translate it even further.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Prueba de Juego</title>
<script type="text/javascript">
//Selector de Modo
var modo=0;
function configuracion(){
var pregunta;
pregunta=confirm("[Seleccionar Modo] Would you like to use a custom range of values to generate the number?, if not we will proceed using standard mode.");
if (pregunta==true) {
modo=1;
var cambiar=document.getElementById('modo').innerHTML='<span id="modo" style="color: blue;">Modo Personalizado</span>';
}else{
modo=2;
var cambiar2=document.getElementById('modo').innerHTML='<span id="modo" style="color: green;">Modo Estandard</span>';
}
}
function juego(){
var contador,numero,ingresar,adivinar,aproximacion,minimo,maximo,mingen,maxgen;
contador=0;
numero=0;
minimo=100;
maximo=1000;
mingen=0;
maxgen=0;
//Verificar que modo se utiliza
if(modo=="0"){alert("[Atención] Modo no seleccionado.");return;}
//Modo Personalizado
if(modo=="1"){
mingen=document.getElementById("mingen").value;
maxgen=document.getElementById("maxgen").value;
if(mingen=="" || maxgen==""){
document.getElementById('status').innerHTML='<span id="status" style="color: #DBA901;">Please type in the maximum and minimum values..</span>';
return;
}
//Procesar a integer
mingen=parseInt(mingen);
maxgen=parseInt(maxgen);
if(mingen=="0" || maxgen=="0"){
document.getElementById('status').innerHTML='<span id="status" style="color: red;">Error. Imposible generar numero a partir de cero.</span>';
return;
}
//Continuar corriendo codigo
numero=Math.floor(Math.random() * (maxgen - mingen + 1)) + mingen;
//alert("Número generado exitosamente utilizando "+mingen+" y "+maxgen+" como valores máximos y minimos.");
//En caso de que no se desee utilizar un rango personalizado continuar en modo 2
}else if(modo=="2"){
//alert("[Cancelado] Utilizando modo estandard.")
//Pedir valor
adivinar=document.getElementById("adivinar").value;
//Cancelar codigo si el numero maximo esta vacio
if(adivinar==""){
document.getElementById('status').innerHTML='<span id="status" style="color: #DBA901;">Por favor ingresa el numero maximo posible de genrar.</span>';
return;
}
//Continuar corriendo codigo
adivinar=parseInt(adivinar);
if(adivinar>=100 && adivinar<=1000){
//alert("Numero máximo seleccionado: "+adivinar);
numero=Math.floor(Math.random()*adivinar)+1;
}else{
document.getElementById('status').innerHTML='<span id="status" style="color: #DBA901;">Por favor ingresa un numero maximo entre 100 y 1000.</span>';
return;
}
}
//Processing [PROBABLY WHERE THE ERROR IS AT]
do{
ingresar=document.getElementById("ingresar").value;
//Cancelar codigo si no se pone numero
if (ingresar==""){
document.getElementById('status').innerHTML='<span id="status" style="color: #DBA901;">Ingresa un numero a adivinar..</span>';
return;
}
ingresar=parseInt(ingresar);
contador++;
//¿Tenes la respuesta Maikol?
if(ingresar==numero){
document.getElementById('status').innerHTML='<span id="status" style="color: #DBA901;">El numero es: '+numero+ '¡Bien! ¡Lo Lograstes en '+contador+' veces!</span>';
//alert("¡Bien!, ¡lo lograstes!. el número era: "+numero);
}else{
//Algoritmo de de aproximación a número aleatorio
if(ingresar>numero)aproximacion=", es menor a lo que ingresastes.";else aproximacion=", es mayor a lo que ingresastes.";
//Algoritmo de aproximacion entre dos números
aviso=0;
if(ingresar>numero || ingresar<numero){
if(numero>1000 && numero>mingen && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número es mayor de ' +mingen+',además'+aproximacion+'</span>'; aviso=1; }
if(numero<1000 && numero>700 && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número está entre 700 y 1000, además'+aproximacion+'</span>'; aviso=1; }
if(numero<700 && numero>500 && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número está entre 500 y 700, además'+aproximacion+'</span>'; aviso=1; }
if(numero<500 && numero>200 && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número está entre 200 y 500, además'+aproximacion+'</span>'; aviso=1; }
if(numero<200 && numero>100 && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color:green;">El número está entre 100 y 200, además'+aproximacion+'</span>'; aviso=1; }
if(numero<100 && numero>50 && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número está entre 50 y 100, además'+aproximacion+'</span>'; aviso=1; }
if(numero<50 && numero<maximo && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número es menor de '+maximo+' y tambien '+aproximacion+'</span>'; aviso=1; }
if(numero<50 && aviso==0) {document.getElementById('status').innerHTML='<span id="status" style="color: green;">El número es menor a 50'; aviso=1; }
}
}
}while(ingresar!=numero);
//Felicidades
//alert("¡Felicidades, lograstes descubrirlo en unas "+contador+" veces!");
}
</script>
</head>
<body>
<center>
<h1>Adivina tu Número V4.1 Box</h1>Versión mejorada, acepta generar rango de valores personalizados.
<p>¿Estás preparado para el reto?</p>
<img src="http://ift.tt/1i3mhqn" style="width:400px;eight:400px;" alt="Hola"/>
<br/><br/>
<p><h1><span id="status">You are not playing yet..</span></h1></p>
<form>
<h4>Custom Value Range</h3>
Minimum Value <input type="text" style="width:100px;" id="mingen"/><br><br>
Maximum Value <input type="text" style="width:100px;" id="maxgen"/>
</form>
<form>
<h4>Maximum number to generate in normal mode</h3>
Máximo <input type="text" style="width:100px;" id="adivinar"/><br><br>
</form>
<form>
<h3>Adivina tu Número!</h3>
<input type"text" id="ingresar"/>
</form>
<button style="height:40px; width:150px" onclick="configuracion()">Mode Selection</button>
<button style="height:40px; width:150px" onclick="juego()">Go</button>
<p>Modo Actual: <span id="modo" style="color: red;">N/A</span></p>
</center>
</body>
</html>
Thank you very much. Marcos MC
asked 2 mins ago
Aucun commentaire:
Enregistrer un commentaire