/*
 * Stratgey: Wagman
 * Description: Try to maximize number of protected pieces
 *		(pieces with a mimimal number of open adjacent sqaures)
 *		Also try to keep pieces in lower left hand corner of
 *		the playing board (why???)
 * Author: Mat Hostetter (rewritten by Erik_Kay@next.com)
 */

#import "WagmanComp.h"
#include <math.h>

@implementation WagmanComp

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

- (int)scoreBoard:(Board *)b forPlayer:(square_state)type
{
    int x1,y1,x,y, res = 0, pieceval, radius, penalty;
    int cols, rows;
    square_state s1;
    const int radial_weight = 3;
    
    cols = [b cols];
    rows = [b rows];
    radius = cols * cols + rows * rows / 4;
    for (x1 = 0; x1 < cols; x1++)
	for (y1 = 0; y1 < rows; y1++) {
	    s1 = b->board[x1 + y1 * cols];
	    if (s1 < SQUARE_ONE)
		continue;
	    pieceval = 10 + 
	    	radial_weight * (((x1-cols/2)^2) + ((y1-rows/2)^2)) / radius;
	    res += ((s1 == type) ? pieceval : -pieceval);
	
	    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;
	    res -= ((s1 == type) ? penalty : -penalty);
	}
    
    return res;
}

@end
