JavaScript - Juego de fosforos

Iniciado por Adalher, Octubre 18, 2019, 05:33:53 AM

Tema anterior - Siguiente tema

0 Miembros y 3 Visitantes están viendo este tema.

Octubre 18, 2019, 05:33:53 AM Ultima modificación: Octubre 18, 2019, 06:31:54 PM por AXCESS
Hola a todos,

la mayoría de ustedes saben bien de este juego. El javascript trata de quitar los
fósforos de tal manera que al final no se tenga que sacar el último. Con ello se
juega contra la computadora.

Al principio se fija cuantos fósforos se alistaran en el juego.

En cada jugada se puede quitar entre uno y tres maderas. Entonces el
javascript calcula los fósforos restantes y actualiza la visualización en la página
principal.

Naturalmente, al mismo tiempo, la computadora o el javascript también trata de
sacarles el último fósforo y con eso ganar el juego.

Para iniciar nuevamente el javascript tienen que actualizar la página principal en el
navegador.

Código:
Código: javascript

<!DOCTYPE html>
<html>
<head>
  <title>Ejemplo de javascript</title>
  <meta charset="UTF-8">
</head>
<body>
<!-- Presentado por javascripts-gratis.de -->
<script type='text/javascript'>
<!--
var obj1 = new Array(100), mc,mc1, cur_obj, total_sel, win = false, cpu_sel, ost, user_sel, game = true;

function RemoveElementByNum(num) {
document.getElementById("ch"+num).style.display = 'none';
document.getElementById("im"+num).style.display = 'none';
}

function RemoveCpuSel(num) {
del = num;
for ( i=0; i<mc1; i++ ) {
ename = document.getElementById("ch"+i);
if (del!=0) {
if (ename.style.display != 'none') {
ename.style.display = 'none';
document.getElementById("im"+i).style.display = 'none';
del-=1;
}
}

}
}

function GetClickedElement(){
total=0;
result=false;
for (i=0; i<mc1; i++) {
ename = document.getElementById("ch"+i);
if ((ename.style.display != 'none') && (ename.checked)) {
total++;
}
}
if (total>3) {
alert('Demasiados fosforos seleccionados. No puedes seleccionar mas de tres fosforos!');
result=false;
} else {
result=true;
}
document.getElementById("maderalog").value = "Tu tomas "+total+" pieza/s.";
total_sel=total;
user_sel=total;
return result;
}

function AI() {
if ( (mc>1) && (win==false) ) {
game = true;
}
if (game == true) {
if ( (mc-user_sel)==1 ) {
win=true;
game=false;
}
if ( (mc%4)!=1 ) {
ost=(mc-user_sel)%4;
if (ost==0) {
ost=4;
}
if (ost>1) {
cpu_sel=ost-1;
} else {
cpu_sel=Math.floor( (3*Math.random()) );
cpu_sel++;
if (cpu_sel>mc) {
cpu_sel=mc;
}
}
}
else {
cpu_sel=4-user_sel;
}
RemoveCpuSel(cpu_sel);
mc=mc-(cpu_sel+user_sel);
document.getElementById("maderalog").value='La computadora toma '+cpu_sel+' pieza/s.';
}
if ( (mc==1) || (mc<1)) {
game = false;
if (win == true) {
document.getElementById("maderalog1").style.visibility='hidden';
document.getElementById("maderalog").style.visibility='hidden';
document.getElementById("eliminar").style.visibility='hidden';
alert('Felicitaciones! Tu has ganado!!!!');
game=false;
} else {
document.getElementById("maderalog1").style.visibility='hidden';
document.getElementById("maderalog").style.visibility='hidden';
alert('Tu has perdido. La inteligencia artificial ha ganado!!!! JA - JA - JA!!!!');
game=false;
document.body.innerHTML = ""
}
}
document.getElementById("maderalog1").value = "Sobra/n " +mc+ " pieza/s";
}

function RemoveSelected(){
if ((total_sel!=0) && (total_sel<4)) {
user_sel=total_sel;
for (i=0; i<mc1; i++) {
ename = document.getElementById("ch"+i);
if ((ename.style.display != 'none') && ename.checked) {
RemoveElementByNum(i);
}
total_sel=0;
}
AI();
} else {
if (total_sel>3) {
alert("Demasiados fosforos seleccionados.");
} else {
alert('Nada seleccionado');
}
}
}


function initMadera() {
mc = prompt("Cantidad de fosforos?. La cantidad debe hallarse entre 7 y 50", "23");
         if (mc<7) mc=7;
         if (mc>50) mc=50;

mc1 = mc;
document.write('<center><table border="0" cellspacing="0" cellpadding="0"><tr>');
for (i=0; i<mc; i++) {
document.write('<td align="center"><div style="height: 70px; width: 7px; background-color: #C0C077;" name="im'+i+'" id="im'+i+'"><div style="height: 7px; width: 7px; background-color: #FF3300;"></div></div></td>');
obj1[i]=1;
}
document.write('</tr><tr>');
for (i=0; i<mc; i++) {
document.write('<td><input type="checkbox" onclick="GetClickedElement();" name="ch'+i+'" id="ch'+i+'" /></td>');
}
document.write('</tr></table>');
document.write('<br /><input type="button" value="Eliminar fosforos seleccionados" onclick="RemoveSelected();" id="eliminar" />');
document.write('<br /><br /><br /><br />');
document.write('<input type="text" name="maderalog" id="maderalog" size="30" /><br />');
document.write('<input type="text" name="maderalog1" id="maderalog1" size="30" /><br />');
document.write('</center>');
}
//-->
</script>
<script type="text/javascript">initMadera();</script>
<!-- Presentado por javascripts-gratis.de -->

</body>
</html>


Página web: No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Saludos
Este es el mayor reproche al pueblo hispanohablante:

Que a pesar de su inteligencia y a pesar de su valentía siempre adoran el poder.