diff --git a/board.c b/board.c index cfe6944..fa75580 100644 --- a/board.c +++ b/board.c @@ -300,6 +300,9 @@ board_place ( /* Set value */ board_set (board, x, y, value); + + /* Update board complexity */ + board_refresh_complexity (board); return true; } @@ -307,3 +310,28 @@ board_place ( } else ERROR("Invalid parameters to function board_place()"); } + + +void +board_refresh_complexity (struct board *board) +{ + board->complexity = 10; + for (board_pos y = 0; y < 9; ++y) + for (board_pos x = 0; x < 9; ++x) + if (! board_has_value (board, x, y)) + { + struct board_element *elem = BOARD_ELEM (board, x, y); + if (elem->complexity < board->complexity) + { + board->complexity = elem->complexity; + + /* Short-circuit function on comlpexity=1, since it can't go lower */ + if (board->complexity == 1) + return; + } + } + + /* If there are no complex board elements, board is solved */ + if (board->complexity == 10) + board->complexity = 0; +} diff --git a/board.h b/board.h index a4b8820..f20f857 100644 --- a/board.h +++ b/board.h @@ -184,3 +184,11 @@ board_place ( board_pos y, element_value value ); + + +/** + * Recomputes board complexity by searching all elements on board for the + * lowest complexity + */ +void +board_refresh_complexity (struct board *board);