Auto tetris

HTML

<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>

CSS

.description {
    float: left;
    font-size: 12px;
    text-style: italic;
    margin: 10px;
    width: 150px;
}
.results {
    font-size: 18px;
}
#auto-tetris {
    float: left;
    width: 200px;
    height: 400px;
    position: relative;
    border: 1px solid #333;
}
.fig {
    position: absolute;
}
.fig.top {
    top: 0;
    left: 50%;
    background: #333;
}
.fig-1-1 {
    width: 80px;
    height: 20px;
}
.fig-1-1.top {
    margin-left: -40px;
}
.fig-1-2 {
    width: 20px;
    height: 80px;
}
.fig-1-2.top {
    margin-left: -10px;
}
.fig-2-1, .fig-2-2 {
    width: 40px;
    height: 40px;
}
.fig-2-1.top, .fig-2-2.top {
    margin-left: -20px;
}

JavaScript

// 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) {
    var arr = cols.slice(0);
    var order=[];
    var start = 0;
    var end = 9;
    for(i=start;i<=end;i++)
        {
            order[i]=i;
        }
    // Сортировка колоно, по количеству свободных клеток
    for(i=start;i<end;i++)
        {
            for(j=i+1;j<=end;j++)
                {
                    if(arr[i]>arr[j])
                    {
                        buf = arr[i];
                        arr[i]=arr[j];
                        arr[j] = buf;
                        
                        buf = order[i];
                        order[i]=order[j];
                        order[j] = buf;
                    }                    
                }
        }
    // Поиск подходящего интервала на минимальной высоте
    for(i=start;i<=end;i++)
        {
            buf = w-1;
            offset = 1;
            while(buf > 0 && (offset + order[i])<=end && cols[order[i]+offset]<=cols[order[i]])
            {
                offset++;
                buf--;
            }
            if(buf == 0)
            {
                return order[i]+1;
            }
        }
    return order[0];
}

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 =...