<div id="auto-tetris"></div>
<div class="description"><span class="results">Squares left: <span id="left">200</span></span><br/><br/>Test UI for tetris algorithm. Just 2 figures: 1x4 and 2x2. Just implement function getColumnNumberForLeftFigureSquare(w, h) that finds the best place for current figure (column number for left square). And wait for the results. Size of tetris: 10 x 20.<br/><br/><a href="http://habrahabr.ru/post/230229/">Post your code here</a></div>
// Function should return integer from 1 to 10. It is a number of column in which will be left square of figure.
function getColumnNumberForLeftFigureSquare(w, h) {
// You can use 3 vars: w, h (size of figure), cols
return getRandomInt(1, 6);
}
var interval,
cols = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // contains filled height for each column (10 columns)
left = 200,
speed = 300; // Score
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function getRandomColor() {
var hex = Math.floor(Math.random() * 0xFFFFFF);
return "#" + ("000000" + hex.toString(16)).substr(-6);
}
function createRandomFigure() {
var t = document.getElementById('auto-tetris'),
l = document.getElementById('left'),
f = getRandomInt(1, 3), // 1 - 1x4 or 2 - 2x2
o = getRandomInt(1, 3), // 1 - horizontal or 2 - vertical
c = 'top fig fig-' + f + '-' + o,
d = document.createElement('div'),
h = 2,
w = 2;
if (f == 1) {
if (o == 1) {
w = 4; h = 1;
} else {
w = 1; h = 4;
}
}
d.className = c;
t.appendChild(d);
setTimeout(function() {
var r = checkResults();
if (r) {
var cn = getColumnNumberForLeftFigureSquare(w, h),
newH = 0,
end = cn + w - 1,
wi = 0,
maxH = 0;
if (end > 10 || cn <= 0) {
t.removeChild(d);
clearInterval(interval);
alert('Incorrect algorithm. Figure can not be inserted here.');
return;
}
for (wi = 1; wi <= w; wi++) {
if (cols[cn + wi - 2] >= maxH) {
maxH = cols[cn + wi - 2];
}
}
newH = maxH + h;
if (newH > 20) {
t.removeChild(d);
clearInterval(interval);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.