!function(){function s(a){var c=document.createElement("div"),d=c.style;c.setAttribute("style","position:fixed;top:20%;width:100%;opacity:0;transition:all .5s;text-align:center;"),c.innerHTML=a,b.appendChild(c),setTimeout(function(){d.opacity=1,d.fontSize="25em"},10),setTimeout(function(){d.opacity=0},510)}function t(b,c){a.fillStyle=b,a.fillRect(h*j,i*j,j,j),a.font=k+"px Courier New",a.fillStyle="#333",c&&a.fillText(c,h*j+1,i*j+l)}var e,g,d=[],f=0,h=0,i=0,j=25,k=37,l=24,m=99,n=30,o="#FFF",p="#FC3",q="#C96",r="\u263a";for(b.style.margin=0,c.height=m*j,c.width=(n+1)*j,c.style.margin="0 auto",c.style.display="block";m>i;i++){for(e=[];n>=h;h++)g=Math.random(),.2>g&&(g=.05>g?9:0),g>1?t(p,r):g?t(q,"\u25a1"):t(o),e.push(g);d.push(e),h=0}i=0,t(o,r),b.onkeydown=function(a){if(a.preventDefault(),t(o),a=a.keyCode-37,a%2){if(!(a>1))return t(o,r);i++}else 2>a?h--:h++;if(h>n&&(h=0),0>h&&(h=n),i>m-2)return alert("Game Over "+f+r);for(9==d[i][h]&&(d[i][h]=0,s(++f+r));!d[i+1][h];)t(o),i++;t(o,r),scrollTo(0,(i-9)*j)}}();
!function(){
var lines = [],
row,
score = 0,
random,
x = 0,
y = 0,
cellSize = 25,
fontSize = 37,
fontOffsetTop = 24,
rows = 99,
cols = 30,
colorClearCell = '#FFF',
colorStar = '#FC3',
colorSand = '#C96',
glyph = '☺';
// Layout
b.style.margin = 0;
c.height = rows * cellSize;
c.width = (cols + 1) * cellSize;
c.style.margin = '0 auto';
c.style.display = 'block';
/**
* Shows splash message
*
* @param {String} message
*/
function showMessage(message) {
var z = document.createElement('div'),
zStyle = z.style;
z.setAttribute("style", "position:fixed;top:20%;width:100%;opacity:0;transition:all .5s;text-align:center;");
z.innerHTML = message;
b.appendChild(z);
setTimeout(function () {
zStyle.opacity = 1;
zStyle.fontSize = "25em";
}, 10);
setTimeout(function () {
zStyle.opacity = 0;
}, 510);
}
/**
* Puts symbol in current cell
*
* @param {String} color
* @param {String} [symbol]
*/
function symbol(color, symbol) {
a.fillStyle = color;
a.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
a.font = fontSize + "px Courier New";
a.fillStyle = '#333';
symbol && a.fillText(symbol, x * cellSize + 1, y * cellSize + fontOffsetTop);
}
// initialize
for (; y < rows; y++) {
row = [];
for (; x <= cols; x++) {
random = Math.random();
if (random < .2) {
random = random < .05 ? 9 : 0;
}
random > 1 ? symbol(colorStar, glyph) : !random ? symbol(colorClearCell) : symbol(colorSand, '□');
row.push(random);
}
lines.push(row);
x = 0;
}
y = 0;
symbol(colorClearCell, glyph);
b.onkeydown = function (e) {
// prevent scrolling
e.preventDefault();
symbol(colorClearCell);
e = e.keyCode - 37;
// 1
// 0 2
// 3
if (e % 2) {
if (e > 1) {
y++;
} else {
// do not move & restore smile
return symbol(colorClearCell, glyph);
}
} else {
if (e < 2) {
x--;
} else {
x++;
}
}
// roll
if (x > cols) x = 0;
if (x < 0) x = cols;
// bottom reached
if (y > rows - 2) {
return alert('Game Over ' + score + glyph);
}
// score++ if star;
if (lines[y][x] == 9) {
lines[y][x] = 0;
showMessage(++score + glyph);
}
// fall
while (!lines[y + 1][x]) {
symbol(colorClearCell);
y++;
}
symbol(colorClearCell, glyph);
scrollTo(0, (y - 9) * cellSize);
}
}();