function ClassBot()
{
    this.m = [[],[],[],[],[],[],[],[]];
    this.find = false;
    this.max_length = 0;
    this.max = {y1 : 0, x1 : 0, y2 : 0, x2 : 0, l : 0};
}

ClassBot.prototype.ifMix = function()
{
    this.max.y1 = 0;
    this.max.x1 = 0;
    this.max.y2 = 0;
    this.max.x2 = 0;
    this.max.l = 0;

    while(this.max.l == 0)
    {
        for(var i=0; i<game.grid.length_h; i++)
            for(var j=0; j<game.grid.length_w; j++)
                game.bot.Find(i, j);

        if(this.max.l == 0)
            game.grid.Mix();
    }
};

ClassBot.prototype.Start = function()
{
    /*this.max.y1 = 0;
    this.max.x1 = 0;
    this.max.y2 = 0;
    this.max.x2 = 0;
    this.max.l = 0;

    var a = true;
    while(this.max.l == 0)
    {
        for(var i=0; i<game.grid.length_h; i++)
            for(var j=0; j<game.grid.length_w; j++)
                game.bot.Find(i, j);

        if(this.max.l == 0)
        {
            game.grid.Mix();
            a = false;
        }
        else
        {
            if(a) game.grid.Swap(this.max.y1, this.max.x1, this.max.y2, this.max.x2);
        }
    }   */

    game.grid.Swap(this.max.y1, this.max.x1, this.max.y2, this.max.x2);
};

ClassBot.prototype.Find = function(i, j)
{
    if(IsClampH(i) && IsClampW(j) && IsClampH(i+1))
        this.FindOne(i, j, i+1, j);
    if(IsClampH(i) && IsClampW(j) && IsClampH(i-1))
        this.FindOne(i, j, i-1, j);
    if(IsClampH(i) && IsClampW(j) && IsClampW(j+1))
        this.FindOne(i, j, i, j+1);
    if(IsClampH(i) && IsClampW(j) && IsClampW(j-1))
        this.FindOne(i, j, i, j-1);
};

ClassBot.prototype.FindOne = function(y1, x1, y2, x2)
{
    if(game.grid.elements[y1][x1].ID == 0 && game.grid.elements[y2][x2].ID == 0 && game.grid.grid[y1][x1] != 0 && game.grid.grid[y2][x2] != 0)
    {
        for(var i=0; i<game.grid.length_h; i++)
            for(var j=0; j<game.grid.length_w; j++)
            {
                if(game.grid.elements[i][j].ID == 0 && game.grid.elements[i][j].bonusID != 2)
                    this.m[i][j] = game.grid.elements[i][j].value;
                else
                    this.m[i][j] = 0;
            }

        this.m[y1][x1] += this.m[y2][x2];
        this.m[y2][x2] = this.m[y1][x1] - this.m[y2][x2];
        this.m[y1][x1] -= this.m[y2][x2];

        this.find = false;
        this.max_length = 0;
        for(i=0; i<game.grid.length_h; i++)
        {
            j=0;
            while(j<game.grid.length_w-1)
                j+=this.getMatchW(i, j);
        }

        for(j=0; j<game.grid.length_w; j++)
        {
            i=0;
            while(i<game.grid.length_h-1)
                i+=this.getMatchH(i, j);
        }

        if(this.find && this.max_length >= this.max.l)
        {
            this.max.y1 = y1;
            this.max.x1 = x1;
            this.max.y2 = y2;
            this.max.x2 = x2;
            this.max.l = this.max_length;
        }
    }
};

ClassBot.prototype.getMatchW = function(wi, hi)
{
    var a = 1;
    for(var k=1; hi+k<game.grid.length_w; k++)
        if(this.m[wi][hi] != 0 && this.m[wi][hi] == this.m[wi][hi+k])
        {
            a++;
        }
        else
        {
            if(a >= config.NUMBER_PETS)
            {
                this.find = true;
                this.max_length += a;
            }
            return a;
        }
    if(a >= config.NUMBER_PETS)
    {
        this.find = true;
        this.max_length += a;
    }
    return a;
};

ClassBot.prototype.getMatchH = function(wi, hi)
{
    var a = 1;
    for(var k=1; wi+k<game.grid.length_h; k++)
    {
        if(this.m[wi][hi] != 0 && this.m[wi][hi] == this.m[wi+k][hi])
        {
            a++;
        }
        else
        {
            if(a >= config.NUMBER_PETS)
            {
                this.find = true;
                this.max_length += a;
            }
            return a;
        }
    }
    if(a >= config.NUMBER_PETS)
    {
        this.find = true;
        this.max_length += a;
    }
    return a;
};