From 8332bbe7b59f9a83e074760c886592da907d012d Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 10 Mar 2019 22:03:03 +1300 Subject: Correct box (3x3) terminology --- solve.c | 64 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/solve.c b/solve.c index d9bc01d..a96ddab 100644 --- a/solve.c +++ b/solve.c @@ -51,13 +51,13 @@ int solve_row_col(struct cell (*b)[9][9]) /* * FIXME rename - cell != cell * For all unsolved cells on the board, solve them if they are the only place - * left in their local 3x3 that a value missing from that 3x3 could fit + * left in their local 3x3 (box) that a value missing from that box could fit */ int not_to_val_cell(struct cell (*b)[9][9]) { int change_count = 0; - int cx = 0; - int cy = 0; + int bx = 0; + int by = 0; int x = 0; int y = 0; int n = 0; @@ -66,9 +66,9 @@ int not_to_val_cell(struct cell (*b)[9][9]) struct cell *c; /* refactor this crap */ - for (cy = 0; cy < 9; cy += 3) + for (by = 0; by < 9; by += 3) { - for (cx = 0; cx < 9; cx += 3) + for (bx = 0; bx < 9; bx += 3) { /* for each val that has to be in cell */ for (n = 1; n <= 9; n++) @@ -77,7 +77,7 @@ int not_to_val_cell(struct cell (*b)[9][9]) { for (x = 0; x < 3; x++) { - c = &(*b)[cx+x][cy+y]; + c = &(*b)[bx+x][by+y]; if (c->val == n) { potentials = 0; @@ -97,17 +97,17 @@ int not_to_val_cell(struct cell (*b)[9][9]) } if (potentials == 1) { - DEBUG_LOG("CELL: (%d,%d) must be %d\n", cx + okx, cy + oky, n); + DEBUG_LOG("CELL: (%d,%d) must be %d\n", bx + okx, by + oky, n); printf("%c%c must be %d because it can't be anything else\n", - NAME_CELL(cx+okx, cy+oky), n); - (*b)[cx+okx][cy+oky].val = n; + NAME_CELL(bx+okx, by+oky), n); + (*b)[bx+okx][by+oky].val = n; potentials = 0; change_count++; } else if(potentials) { - DEBUG_LOG("CELL: (%d,%d) could be one of %d\n", cx + okx, cy + oky, potentials); + DEBUG_LOG("CELL: (%d,%d) could be one of %d\n", bx + okx, by + oky, potentials); int k = 0; for (k = 0; k < 9; k++) { - if (!(*b)[cx+okx][cy+oky].not[k]) { + if (!(*b)[bx+okx][by+oky].not[k]) { DEBUG_LOG(" %d", k+1); } } @@ -122,7 +122,7 @@ int not_to_val_cell(struct cell (*b)[9][9]) /* For the numbers 1-9 not filled into a cell, fill in those which have only * one place they can go */ -int cell_fill_certainties(struct cell (*b)[9][9], int cx, int cy) +int cell_fill_certainties(struct cell (*b)[9][9], int bx, int by) { int change_count = 0; int will_take_count = 0; @@ -134,15 +134,15 @@ int cell_fill_certainties(struct cell (*b)[9][9], int cx, int cy) int x,y; /* Translate cell-based index to tile-based */ - cx *= 3; - cy *= 3; + bx *= 3; + by *= 3; /* First: walk the cell and find which values it has */ - for (x = cx; x < cx + 3; x++) { - for (y = cy; y < cy + 3; y++) { + for (x = bx; x < bx + 3; x++) { + for (y = by; y < by + 3; y++) { val = (*b)[x][y].val; if (val) { - DEBUG_LOG("CELL_CERT: Cell (%d,%d) has %d\n", cx/3, cy/3, val); + DEBUG_LOG("CELL_CERT: Cell (%d,%d) has %d\n", bx/3, by/3, val); cell_has[val - 1] = 1; } } @@ -160,8 +160,8 @@ int cell_fill_certainties(struct cell (*b)[9][9], int cx, int cy) DEBUG_LOG("Try to solve for %d\n", val + 1); /* Walk the cell and find which tile(s) will take it */ - for (x = cx; x < cx + 3; x++) { - for (y = cy; y < cy + 3; y++) { + for (x = bx; x < bx + 3; x++) { + for (y = by; y < by + 3; y++) { if ((*b)[x][y].val) { continue; } @@ -175,7 +175,7 @@ int cell_fill_certainties(struct cell (*b)[9][9], int cx, int cy) DEBUG_LOG("will_take_count is %d\n", will_take_count); /* Don't make indeterminate choices here*/ if (will_take_count == 1) { - printf("%c%c must be %d because it is the only place in its 3x3 this will fit\n", + printf("%c%c must be %d because it is the only place in its boxthis will fit\n", NAME_CELL(will_take_x, will_take_y), val + 1); (*b)[will_take_x][will_take_y].val = val + 1; @@ -278,27 +278,27 @@ int board_cols_are_solved(struct cell (*b)[9][9]) } /* - * Check if one of the nine 3x3 blocks is correctly solved on a board + * Check if one of the nine 3x3 boxes is correctly solved on a board * Returns 0 for a correctly solved 3x3 */ -int board_3_is_solved(struct cell (*b)[9][9], int cx, int cy) +int board_box_is_solved(struct cell (*b)[9][9], int bx, int by) { int col_has[9] = {0}; int val = 0; int x = 0; int y = 0; - cx *= 3; - cy *= 3; + bx *= 3; + by *= 3; - for (x = cx; x < cx + 3; x++) { - for (y = cy; y < cy + 3; y++) { + for (x = bx; x < bx + 3; x++) { + for (y = by; y < by + 3; y++) { val = (*b)[x][y].val; if (val == 0) { return -1; } if (col_has[val - 1]) { - DEBUG_LOG("Invalid board: 3x3 %d duplicate %d\n", x, val); + DEBUG_LOG("Invalid board: box %d,%d duplicate %d\n", bx/3, by/3 val); return -1; } col_has[val - 1] = 1; @@ -309,17 +309,17 @@ int board_3_is_solved(struct cell (*b)[9][9], int cx, int cy) } /* - * Check if all of the nine 3x3 blocks are correctly solved on a board - * Returns 0 for correctly solved 3x3s + * Check if all of the nine 3x3 boxes are correctly solved on a board + * Returns 0 for correctly solved boxes */ -int board_3s_are_solved(struct cell (*b)[9][9]) +int board_boxes_are_solved(struct cell (*b)[9][9]) { int ret = 0; int x = 0; int y = 0; for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { - ret = board_3_is_solved(b, x, y); + ret = board_box_is_solved(b, x, y); if (ret) return ret; } @@ -346,7 +346,7 @@ int board_is_solved(struct cell (*b)[9][9]) if (ret) return ret; - ret = board_3s_are_solved(b); + ret = board_boxes_are_solved(b); return ret; } -- cgit v1.1