Skip to content

Instantly share code, notes, and snippets.

@alejandro
Forked from anonymous/jsbin.itasem.css
Created August 6, 2013 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandro/6169534 to your computer and use it in GitHub Desktop.
Save alejandro/6169534 to your computer and use it in GitHub Desktop.
td {
background: cyan;
min-width: 100px;
height: 100px;
text-align: center;
font-family: Arial;
font-weight: 800;
font-size:30px;
}
.wt {
background: white;
}
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[add your bin description]" />
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id="tabla"></div>
</body>
</html>
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)
, isCenter = (i == 2 && p == 'b')
, 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
if (move == 'down'){
pos.y -= 1
if (pos.y < 1) pos.y = 1
} else if (move == 'up'){
pos.y += 1
if (pos.y > 3) pos.y = 3
} else if (move == 'rigth'){
if (pos.x == 'a') pos.x = 'a'
else if (pos.x == 'b') pos.x = 'a'
else if (pos.x == 'c') pos.x = 'b'
} else if (move == 'left') {
if (pos.x == 'a') pos.x = 'b'
else if (pos.x == 'b') pos.x = 'c'
else if (pos.x == 'c') pos.x = 'c'
}
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(){
if (tabla.textContent === '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