Created
August 4, 2013 23:53
-
-
Save anonymous/6152559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| td { | |
| background: cyan; | |
| min-width: 100px; | |
| height: 100px; | |
| text-align: center; | |
| font-family: Arial; | |
| font-weight: 800; | |
| font-size:30px; | |
| } | |
| .wt { | |
| background: white; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset=utf-8 /> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <div id="tabla"></div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function run(){ | |
| var tabla = window.tabla | |
| , tb = document.createElement('table') | |
| , dt = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] | |
| , kys = {38: 'up', 40: 'down', 37: 'left', 39: 'rigth'} | |
| , pos = {} | |
| for (var i = 1; i <= 3; i++){(function(){ | |
| var tr = document.createElement('tr') | |
| tr.innerHTML = getItem(i, 'a') + getItem(i, 'b') + getItem(i, 'c') | |
| tb.appendChild(tr) | |
| })(i)} | |
| function getItem(i, p){ | |
| var pos = Math.floor(Math.random() * dt.length) | |
| var isCenter = (i == 2 && p == 'b'); | |
| var it = isCenter ? '' : dt.splice(pos, 1)[0] | |
| return '<td class="' + (isCenter ? 'wt' : i + p) + '" data-position="' + i + p + '" data-has="' + it + '">' + | |
| it + '</td>' | |
| } | |
| function keys (e){if (!(e.keyCode in kys)) return | |
| var move = kys[e.keyCode] | |
| , wtt = document.querySelector('[data-position="'+ pos.y + pos.x + '"]') | |
| , ltt | |
| , val | |
| switch (move){ | |
| case 'down': | |
| pos.y -= 1 | |
| if (pos.y < 1) pos.y = 1 | |
| break; | |
| case 'up': | |
| pos.y += 1 | |
| if (pos.y > 3) pos.y = 3 | |
| break; | |
| case 'rigth': | |
| if (pos.x == 'a') pos.x = 'a' | |
| else if (pos.x == 'b') pos.x = 'a' | |
| else if (pos.x == 'c') pos.x = 'b' | |
| break; | |
| case 'left': | |
| if (pos.x == 'a') pos.x = 'b' | |
| else if (pos.x == 'b') pos.x = 'c' | |
| else if (pos.x == 'c') pos.x = 'c' | |
| break; | |
| default: | |
| break; | |
| } | |
| ltt = document.querySelector('[data-position="' + pos.y + pos.x + '"]') | |
| val = ltt.dataset.has + '' | |
| wtt.removeAttribute('class') | |
| ltt.removeAttribute('class') | |
| ltt.setAttribute('class', 'wt') | |
| wtt.dataset.has = val | |
| wtt.textContent = val | |
| ltt.dataset.has = '' | |
| ltt.textContent = ' ' | |
| didIWin() | |
| } | |
| function didIWin(){ | |
| var res = tabla.textContent | |
| if (res == 'abcdefgh ') alert('w00t! ganaste') | |
| } | |
| window.onkeydown = keys | |
| pos = { x: 'b', y: 2 } | |
| tabla.appendChild(tb) | |
| } | |
| window.onload = run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment