/*
 * Stratgey: Protect
 * Description: greedy + try to maximize number of protected pieces
 *		(pieces with a mimimal number of open adjacent sqaures)
 * Author: Erik_Kay@next.com (ideas taken from Wagman by Mat Hostetter)
 */

#import "ProtectComp.h"

@implementation ProtectComp

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


-(int)scoreBoard:(Board *)b forPlayer:(square_state)player
{
    int x1,y1,x,y, score = 0, penalty;
    int cols, rows;
    square_state piece;
    
    score = 10 * ([b numberOfPiece:player] - 
    		[b numberOfPiece:OTHER_PLAYER(player)]);
    cols = [b cols];
    rows = [b rows];
    for (x1 = 0; x1 < cols; x1++)
	for (y1 = 0; y1 < rows; y1++) {
	    piece = b->board[x1 + y1 * cols];
	    // we only care about squares that have player pieces in them
	    if ((piece != SQUARE_ONE) && (piece != SQUARE_TWO))
		continue;
	    penalty = 0;
	    for (x = MAX(x1 - 1, 0); x <= MIN(x1 + 1, cols - 1); x++)
		for (y = MAX(y1 - 1, 0); y <= MIN(y1 + 1, rows - 1); y++) 
		    if (b->board[x + y * cols] == SQUARE_EMPTY) 
			penalty++;
	
	    if (penalty > 5) 
	    	penalty = 5;
	    if (penalty == 0)
	    	penalty = -10;
	    score -= ((piece == player) ? penalty : -penalty);
	}
    return score;
}

@end
