I have an issue with my checkers game that im not sure of the best way to solve. Because of the for loop it will always favour taking the 1 1 square up and 1 square to the left in the case of white (1 square down and 1 to the right if black's turn). I have managed to code it so that if the "wrong choice" was taken at the start of the chain it can start from the beginning and i mark the original piece taken as an empty square. however that doesn't work if either a the original piece taken was correct and it came off the "path" else where or b it the original piece was wrong and it made the wrong turn elsewhere on the path.
this is what i have so far
Code:
bool take_piece(char board[][ROW_MAX + 1][MESSAGE], bool is_king, int piece_coordinate_x, int piece_coordinate_y,
int move_coordinate_x, int move_coordinate_y, int current_player)
{
int x_coordinate, y_coordinate, multiplyer_x, multiplyer_y = current_player ? -1 : 1;
char temp_board[COLUMN_MAX + 1][ROW_MAX + 1][MESSAGE];
char *p_player_letter = current_player ? "b" : "w", *p_piece_letter = current_player ? "w" : "b";
char *p_king_letter = current_player ? "W" : "B";
bool found_piece_to_take = false;
for (y_coordinate = ROW_MIN; y_coordinate <= ROW_MAX; y_coordinate++)
{
for (x_coordinate = COLUMN_MIN; x_coordinate <= COLUMN_MAX; x_coordinate++)
{
strcpy(temp_board[x_coordinate][y_coordinate], board[x_coordinate][y_coordinate]);
//printf("x is %d y is %d board is %s temp is %s\n", x_coordinate,y_coordinate, board[x_coordinate][y_coordinate], temp_board[x_coordinate][y_coordinate]);
}
}
x_coordinate = piece_coordinate_x;
y_coordinate = piece_coordinate_y;
while (1)
{
for (multiplyer_x = -1; multiplyer_x <= 1; multiplyer_x += 2)
{
if ((strcmp(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], p_piece_letter) == 0) ||
(strcmp(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], p_king_letter) == 0))
{
strcpy(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], EMPTY_SQUARE);// mark it empty so not found next time round
if (off_board((x_coordinate + (2 * multiplyer_x)), (y_coordinate + (2 * multiplyer_y))) == false)
{
if (valid_square(board, (x_coordinate + (2 * multiplyer_x)), (y_coordinate + (2 * multiplyer_y))) == true)
{
strcpy(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], "t");
x_coordinate += 2 * multiplyer_x;
y_coordinate += 2 * multiplyer_y;
found_piece_to_take = true;
break;
}
}
}
}
if (x_coordinate == move_coordinate_x && y_coordinate == move_coordinate_y)
{
break;
}
if (multiplyer_x >= 3)
{
if (found_piece_to_take == false)
{
break;
}
else
{
for (y_coordinate = ROW_MAX; y_coordinate >= ROW_MIN; y_coordinate--)
{
for (x_coordinate = COLUMN_MIN; x_coordinate <= COLUMN_MAX; x_coordinate++)
{
if (strcmp(temp_board[x_coordinate][y_coordinate], "t") == 0)
{
strcpy(temp_board[x_coordinate][y_coordinate], EMPTY_SQUARE);
found_piece_to_take = false;
printf("\"x is %d y is %d board is %s temp is %s\"\n", x_coordinate,y_coordinate, board[x_coordinate][y_coordinate], temp_board[x_coordinate][y_coordinate]);
}
}
}
x_coordinate = piece_coordinate_x;
y_coordinate = piece_coordinate_y;
}
}
}
if (found_piece_to_take == true)
{
for (y_coordinate = ROW_MAX; y_coordinate >= ROW_MIN; y_coordinate--)
{
for (x_coordinate = COLUMN_MIN; x_coordinate <= COLUMN_MAX; x_coordinate++)
{
if (strcmp(temp_board[x_coordinate][y_coordinate], "t") == 0)
{
update_board_array(board, EMPTY_SQUARE, x_coordinate, y_coordinate);
}
}
}
update_board_array(board, EMPTY_SQUARE, piece_coordinate_x, piece_coordinate_y);
update_board_array(board, p_player_letter, move_coordinate_x, move_coordinate_y);
return true;
}
return false;
}
the way i see it is at the start of each single jump in the chain have two choices left and right. maximum amount of legal jumps on a board 8 x 8 is 3 so a total maximum of 8 choices (2^3). so either i can have some sort of array that logs the co-ordinates of the jumps and somehow backtracks through them. or somehow automatically calculating every route possible from point a to b in increments of 2 to both x and y co-ordinates stores them somehow then checks them off against the board to see if pieces are there to take.
i think the latter is the better idea because when i come to deal with kings the possibilities increase dramatically so to try and predict how big the array would have to be is near impossible (i suppose the maximum number of pieces is 12 so 2^12)
any advice greatly appreciated
coop