2007-02-23 18:10:32 +01:00
|
|
|
#include "state.h"
|
2007-02-23 23:28:29 +01:00
|
|
|
#include <stdio.h>
|
2007-02-23 18:10:32 +01:00
|
|
|
|
|
|
|
int State::size = 19;
|
|
|
|
|
|
|
|
State::State( void )
|
|
|
|
{
|
2007-02-23 23:28:29 +01:00
|
|
|
board = new type[size * size];
|
|
|
|
}
|
|
|
|
|
|
|
|
State::~State( void )
|
|
|
|
{
|
|
|
|
delete[] board;
|
|
|
|
}
|
|
|
|
|
|
|
|
void State::printState( void )
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
for (j = 0; j < size; j++) { //for all points:
|
|
|
|
switch (board[i*size + j])
|
|
|
|
{
|
|
|
|
case BLACK:
|
|
|
|
printf("B ");
|
|
|
|
break;
|
|
|
|
case WHITE:
|
|
|
|
printf("W ");
|
|
|
|
break;
|
|
|
|
case BOARD:
|
|
|
|
printf("- ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
2007-02-23 18:10:32 +01:00
|
|
|
}
|
|
|
|
|
2007-02-23 20:14:48 +01:00
|
|
|
State State::generateState (SDL_Surface *boardImg, Settings settings)
|
2007-02-23 18:10:32 +01:00
|
|
|
{
|
2007-02-23 20:14:48 +01:00
|
|
|
State state;
|
|
|
|
int i, j, x, y, r, sum;
|
|
|
|
point p;
|
2007-02-23 23:28:29 +01:00
|
|
|
r = 10; //what kind of magic number is this?
|
2007-02-23 20:14:48 +01:00
|
|
|
for (i = 0; i < size; i++) {
|
2007-02-23 23:28:29 +01:00
|
|
|
for (j = 0; j < size; j++) { //for all points:
|
|
|
|
p = positionPoint(i, j, settings); //retrieve the estimated point
|
|
|
|
sum = 0;
|
|
|
|
|
|
|
|
//TODO: this loop should be placed into its own function!
|
2007-02-23 20:14:48 +01:00
|
|
|
for (y = p.y-r; y < p.y+r; y++) {
|
2007-02-23 23:28:29 +01:00
|
|
|
for (x = p.x-r; x < p.x+r; x++) { //for -20 to 20 around the point
|
2007-02-23 20:14:48 +01:00
|
|
|
uint32_t px = PX(boardImg, x, y);
|
2007-02-23 23:28:29 +01:00
|
|
|
sum += (1024 -PXR(px) - PXG(px) + PXB(px)); //sum up the colors
|
2007-02-23 20:14:48 +01:00
|
|
|
}
|
|
|
|
}
|
2007-02-23 23:28:29 +01:00
|
|
|
sum/=400;
|
|
|
|
//printf("%d\t", sum);
|
2007-02-23 20:14:48 +01:00
|
|
|
type t = BOARD;
|
|
|
|
if (sum < settings.getBlackTreshold()) t = BLACK;
|
|
|
|
if (sum > settings.getWhiteTreshold()) t = WHITE;
|
|
|
|
PX_(state.board, i, j, size, size) = t;
|
|
|
|
}
|
2007-02-23 23:28:29 +01:00
|
|
|
//printf("\n");
|
2007-02-23 20:14:48 +01:00
|
|
|
}
|
2007-02-23 23:28:29 +01:00
|
|
|
//printf("\n");
|
2007-02-23 20:14:48 +01:00
|
|
|
return state;
|
|
|
|
}
|
2007-02-23 18:10:32 +01:00
|
|
|
|
2007-02-23 20:14:48 +01:00
|
|
|
point State::positionPoint(int i, int j, Settings s)
|
|
|
|
{
|
|
|
|
point ul = s.getUL(), ur = s.getUR(), bl = s.getBL(), br = s.getBR();
|
|
|
|
point topp, botp, pp;
|
|
|
|
topp.x = ul.x + (i*(ur.x-ul.x))/size;
|
|
|
|
topp.y = ul.y + (i*(ur.y-ul.y))/size;
|
|
|
|
botp.x = bl.x + (i*(br.x-bl.x))/size;
|
|
|
|
botp.y = bl.y + (i*(br.y-bl.y))/size;
|
|
|
|
pp.x = topp.x + (j*(botp.x-topp.x))/size;
|
|
|
|
pp.y = topp.y + (j*(botp.y-topp.y))/size;
|
|
|
|
return pp;
|
2007-02-23 18:10:32 +01:00
|
|
|
}
|
2007-02-23 20:14:48 +01:00
|
|
|
|