/*
 *  Copyright 2002-2009 Mikhail Kryshen <mikhail@kryshen.net>
 *
 *  This file is part of BMTron.
 *
 *  BMTron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  BMTron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with BMTron.  If not, see <http://www.gnu.org/licenses/>.
 */

package kryshen.bmtron;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;

/**
 * Very simpe computer player algorithm.
 */
public class ComputerPlayer1 extends ThreadedPlayer {
    private boolean firstMove = true;
    
    ComputerPlayer1(Field field, String name, Color color, int team) {
        super(field, name, color, team);
    }

    synchronized void start() {
        firstMove = true;
        super.start();
    }

    protected void move() {
        if (firstMove) {
            firstMove = false;
            setDirection(randomMove());
            return;
        }
        
        Point l = getLocation();
        int dir = field.UP;
        
        int lr = findLargestRect(l.x, l.y - 1);
        int lr2 = findLargestRect(l.x - 1, l.y);
        if (lr2 > lr + Game.nextRandom(-2, 1)) {
            dir = field.LEFT;
            lr = lr2;
        }
        lr2 = findLargestRect(l.x, l.y + 1);
        if (lr2 > lr + Game.nextRandom(-2, 1)) {
            dir = field.DOWN;
            lr = lr2;
        }
        lr2 = findLargestRect(l.x + 1, l.y);
        if (lr2 > lr + Game.nextRandom(-2, 1)) {
            dir = field.RIGHT;
            lr = lr2;
        }
        
        setDirection(dir);
    }
        
    private int findLargestRect(int x, int y) {
        if (!field.isFree(x, y)) return 0;
        
        Rectangle r = new Rectangle(x, y, 1, 1);
        
        boolean loop = true;
        while (loop) {
            loop = false;
            boolean f = true;
            for (int i = r.x; i <= r.width + r.x - 1; i++) {
                f = f & field.isFree(i, r.y - 1);
                if (!f) break;
            }
            loop = loop | f;
            if (f) { r.y--; r.height++; }
            f = true;
            for (int i = r.x; i <= r.width + r.x - 1; i++) {
                f = f & field.isFree(i, r.y + r.height);
                if (!f) break;
            }
            loop = loop | f;
            if (f) r.height++;
            f = true;
            for (int i = r.y; i <= r.height + r.y - 1; i++) {
                f = f & field.isFree(r.x - 1, i);
                if (!f) break;
            }
            loop = loop | f;
            if (f) {r.x--; r.width++; }
            f = true;
            for (int i = r.y; i <= r.height + r.y - 1; i++) {
                f = f & field.isFree(r.x + r.width, i);
                if (!f) break;
            }
            loop = loop | f;
            if (f) r.width++;
        }
        //System.err.println(r.width * r.height);
        
        //return r.width * r.height;
        return r.width * r.height * 2 - r.width - r.height + 1;
    }
}

