I wonder if any of you can suggest a better and faster way to do the "valuable square" checking routine that i do. what i do is, i pass through all the 225 squares on the board,and check if a certain square is "valuable" . a "valuable" square is a square that is no more than two steps away from an existing token on the board..computer's or opponent's. And it is also has to be empty.
I am using a single dimension array in size 225 for the board representation, and this is how it looks like:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17...
and 13 more rows like this
My checkValuableSquare(int square) method is as follows: I simply pass thourgh all possible 8 directions away from the square, and if i stumble upon a player i return true (e.g 1).
int isValuableSquare(int square){
if(board[square] != 0){ // 0 is an empty square, 1 for computer player and -1 for human opponent.
return 0; // if square != 0 that means that it is not playable so i return 0;
}
else{
int point;
point = square;
//check left
while(!isBorder(point,LEFT) && point > square-2){
point--;
if(board[point] != 0){
return 1;
}
}
point = square;
//check right
while(!isBorder(point,RIGHT) && point < square+2){
point++;
if(board[point] != 0){
return 1;
}
}
point = square;
//check top
while(!isBorder(point,TOP) && point > square-30){
point-=15;
if(board[point] != 0){
return 1;
}
}
point = square;
//check bottom
while(!isBorder(point,BOTTOM) && point < square+30){
point+=15;
if(board[point] != 0){
return 1;
}
}
point = square;
//check up left
while(!isBorder(point,TOP) && !isBorder(point,LEFT) && point > square-32){
point-=16;
if(board[point] != 0){
return 1;
}
}
point = square;
//check up right
while(!isBorder(point,TOP) && !isBorder(point,RIGHT) && point > square-28){
point-=14;
if(board[point] != 0){
return 1;
}
}
point = square;
//check down right
while(!isBorder(point,BOTTOM) && !isBorder(point,RIGHT) && point < square+32){
point+=16;
if(board[point] != 0){
return 1;
}
}
point = square;
//check down left
while(!isBorder(point,BOTTOM) && !isBorder(point,LEFT) && point < square+28){
point+=14;
if(board[point] != 0){
return 1;
}
}
return 0;
}
}
I have another function that tells me if a certain square is a border square. this is the isBorder() function: it takes the square and an enum Direction {TOP,BOTTOM,LEFT,RIGHT} as parameters.
int isBorder(int square,Direction direction){
switch(direction){
case TOP:
return square/15 == 0;
case BOTTOM:
return square/15 == 14;
case LEFT:
return square%15 == 0;
case RIGHT:
return square%15 == 14;
}
}
I hope that this code is not too messy..I would like to get better suggestions and corrections for this method. thx!