elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  javascript - Juego de fosforos
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: javascript - Juego de fosforos  (Leído 1,212 veces)
TickTack


Desconectado Desconectado

Mensajes: 428


CipherX


Ver Perfil
javascript - Juego de fosforos
« en: 26 Febrero 2019, 21:33 pm »

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:
<!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: https://drive.google.com/open?id=14Gu9OuiYfRsDIHCZ4CT_AIQtfvRgvD-N

Saludos


« Última modificación: 26 Febrero 2019, 21:37 pm por TickTack » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Juego javascript
Desarrollo Web
Puntoinfinito 4 2,351 Último mensaje 20 Junio 2012, 21:23 pm
por Puntoinfinito
[javascript] Crear un mapa alaeatorio (es para un juego)
Programación General
z3nth10n 8 6,423 Último mensaje 2 Julio 2012, 18:45 pm
por z3nth10n
[javascript] Probabilidades de Captura en Juego
Desarrollo Web
Brian1511 7 3,750 Último mensaje 23 Julio 2015, 20:36 pm
por Brian1511
MOVIDO: [javascript] Probabilidades de Captura en Juego
Java
Eleкtro 0 1,325 Último mensaje 23 Julio 2015, 04:26 am
por Eleкtro
Hacer juego con javascript
Desarrollo Web
Robocop8 2 1,732 Último mensaje 19 Agosto 2017, 00:56 am
por ivancea96
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines