Initializing...
Liveweave
expand_more
home
Home
data_object
CSS Explorer
arrow_outward
Palette
Color Explorer
arrow_outward
Polyline
Graphics Editor
arrow_outward
outbox_alt
Generative AI
arrow_outward
build
Tools
expand_more
restart_alt
Load base template
post_add
Generate Lorem ipsum...
code
Format HTML
code_blocks
Format CSS
data_object
Format JavaScript
library_add
Library
expand_more
A
Algolia JS
Animate CSS
Apex Charts JS
B
Bulma CSS
Bootstrap
C
Chart JS
Chartist
Create JS
D
D3
Dojo
F
Foundation
Fullpage JS
G
Granim JS
Google Charts
H
Halfmoon
J
jQuery
M
Materialize
Moment JS
Masonry JS
Milligram CSS
P
Pure CSS
Primer CSS
Popper JS
Pattern CSS
Picnic CSS
R
React JS
Raphael JS
Raisin CSS
S
Semantic UI
Skeleton CSS
Spectre CSS
Tachyons CSS
T
Tailwind
Three JS
U
UI Kit
Vis JS
W
Water CSS
download
Download
expand_more
developer_mode
Download as HTML
folder_zip
Download as .ZIP
cloud_upload
Save
account_circle
Login
settings
Settings
expand_more
14
px
Live mode
Night mode
Line number
Mini map
Word wrap
sync_alt
Reset Settings
smart_display
Run
<!DOCTYPE html> <html> <head> <title>HTML5, CSS3 and JavaScript demo</title> </head> <body> <!-- Start your code here --> <table> <tr> <td id="a1"> </td> <td id="b1"> </td> <td id="c1"> </td> <td id="d1"> </td> </tr> <tr> <td id="a2"> </td> <td id="b2"> </td> <td id="c2"> </td> <td id="d2"> </td> </tr> <tr> <td id="a3"> </td> <td id="b3"> </td> <td id="c3"> </td> <td id="d3"> </td> </tr> <tr> <td id="a4"> </td> <td id="b4"> </td> <td id="c4"> </td> <td id="d4"> </td> </tr> </table> <!-- End your code here --> </body> </html>
td { width: 40px; height: 40px; border: 1px solid #000; text-align: center; font-weight: bold; } .sum2 { background-color: #FFF8DC; } .sum4 { background-color: #F5DEB3; } .sum8 { background-color: #F4A460; } .sum16 { background-color: #DAA520; } .sum32 { background-color: #B8860B; } .sum64 { background-color: #CD853F; } .sum128 { background-color: #D2691E; } .sum256 { background-color: #8B4513; } .sum512 { background-color: #A0522D; } .sum1024 { background-color: #A52A2A; } .sum2048 { background-color: #800000; } .sum4096 { background-color: #4682B4; } .sum8192 { background-color: #006400; } .sum16384 { background-color: #FF4500; }
/* Write JavaScript here */ function $(id) { return document.getElementById(id);} var board = { cols: ['a', 'b', 'c', 'd'], map: [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ], finished: false, getInMap: function(x, y) { return this.map[x][y]; }, getInPage: function(x, y) { return this.cols[x]+(y+1); }, update: function() { var el, mp; var h = 0; for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { el = $(this.getInPage(j, i)); mp = this.getInMap(j, i); if (h < mp) h = mp; el.className = "sum" + mp; el.innerHTML = (mp > 0) ? mp : " "; } } if ((h == 2048) && (!this.finished)) if (!confirm("Parabéns! Você conseguiu! Continuar jogando?")) this.reset(); else this.finished = true; }, zeros: function() { var arr = []; for (var i = 0; i < 4; i++) for (var j = 0; j < 4; j++) if (this.getInMap(j, i) === 0) arr.push(this.getInPage(j, i)); return arr; }, drop: function() { var a = this.zeros(); var i = Math.floor(Math.random() * 1000) % a.length; var x = this.cols.indexOf(a[i][0]); var y = a[i][1]-1; this.map[x][y] = 2; if ((this.zeros().length === 0) && (!this.testMoves())) { alert("Game Over! Iniciando outra partida."); return false; } return true; }, gravity: function(direc, testMode) /*[r-l]*/{ var apply = ! testMode; var i, j, lc, col, n, temp; var change = false; var reverse = (direc >= 2); var vertical = (direc % 2); for (i = 0; i < 4; i++) { col = []; temp = 0; for (lc = 0; lc < 4; lc++) { j = (reverse) ? 3 - lc : lc; n = (vertical) ? this.map[i][j] : this.map[j][i]; if (temp > 0) { if (n > 0) { if (temp != n) { col.push(temp); temp = n; } else { col.push(n * 2); temp = 0; } } } else { if (n > 0) { temp = n; } } } if (temp > 0) { col.push(temp); } for (lc = 0; lc < 4; lc++) { j = (reverse) ? 3 - lc : lc; temp = (col.length > lc) ? col[lc] : 0; if (vertical) { if (temp !== this.map[i][j]) change = true; if (apply) this.map[i][j] = temp; } else { if (temp !== this.map[j][i]) change = true; if (apply) this.map[j][i] = temp; } } } return change; }, testMoves: function() { var r = false; for (var i = 0; i < 4; i++) if (this.gravity(i, true)) r = true; return r; }, reset: function() { this.map = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ]; this.finished = false; this.drop(); this.drop(); this.update(); } }; board.reset(); window.addEventListener("keydown", function(e) { if ((e.keyCode >= 37) && (e.keyCode <= 40)) { if (board.gravity(e.keyCode - 37)) { if (board.drop()) board.update(); else board.reset(); } } }, false);