/*
 * Stratgey: Density
 * Description: attempt to maintain a high density of pieces
 * Note: this one could use a lot of work
 * Author: Erik_Kay@next.com
 */

#import "DensityComp.h"

@implementation DensityComp

+ (const char *)strategyName
{
    return "Density";
}

- (int)scoreBoard:(Board *)b forPlayer:(square_state)type
{
    int minx = 0,miny = 0,maxx = 1,maxy = 1,x,y,sc;
    int cols,rows, width, height;
    float dens;
    cols = [b cols];
    rows = [b rows];
    for (y = 0; y < rows; y++) {
	for (x = 0; x < cols; x++) {
	    if (b->board[x + y * cols] == type) {
	    	miny = y;
		break;
	    }
	}
	if (x < cols)
	    break;
    }
    for (y = rows - 1; y >= 0; y--) {
	for (x = 0; x < cols; x++) {
	    if (b->board[x + y * cols] == type) {
	    	maxy = y;
		break;
	    }
	}
	if (x < cols)
	    break;
    }
    for (x = 0; x < cols; x++) {
	for (y = 0; y < rows; y++) {
	    if (b->board[x + y * cols] == type) {
	    	minx = x;
		break;
	    }
	}
	if (x < cols)
	    break;
    }
    for (x = cols - 1; x >= 0; x++) {
	for (y = 0; y < rows; y++) {
	    if (b->board[x + y * cols] == type) {
	    	maxx = x;
		break;
	    }
	}
	if (x < cols)
	    break;
    }
    dprintf (("(%d,%d)x(%d,%d) : ",minx,miny,maxx,maxy));
    width = 1 + maxx - minx;
    height = 1 + maxy - miny;
    dens = ((float)[b numberOfPiece:type] / (float)((width * height)));
    sc =  dens * MIN(width,height) * MIN(cols,rows) + 
    	([b numberOfPiece:type] - [b numberOfPiece:OTHER_PLAYER(type)]);
    dprintf (("score: %d (%d,%d) to (%d,%d)\n",sc,
    	b->currentMove.from.col, b->currentMove.from.row,
	b->currentMove.to.col, b->currentMove.to.row));
    return sc;
}

@end
