bool is_solved(int n, int gridArray[n][n]) { /* Determines of sudoku puzzle has been solved * Returns true if it has and false otherwise */ //Check rows for(int i=0; i<n;i++) { int content_of_rows[n]; for(int j=0; j<n; j++) { content_of_rows[j] = gridArray[i][j]; } bool has_duplicates = check_for_duplicates(content_of_rows,n); if (has_duplicates) { return false; } else if (has_zeros(content_of_rows,n)) { return false; } } //Check columns for(int i=0; i<n;i++) { int content_of_columns[n]; for(int j=0; j<n;j++) { content_of_columns[j] = gridArray[j][i]; } bool has_duplicates = check_for_duplicates(content_of_columns, n); if (has_duplicates) { return false; } else if (has_zeros(content_of_columns,n)) { return false; } } //Check subsquares int m = sqrt(n); for(int i=0; i<n; i += m){ for(int j=0; j<n; j += m) { for(int x=i; x< i+m; x++) { for(int y=j; y<j+m;y++){ if(x != i && y != j && gridArray[x][y] == gridArray[i][j]){ return false; } else if (gridArray[x][y] == 0) { return false; } } } } } return true; }
static unsigned int try_to_flush_duplicates(const char *new_key, unsigned int buf_len) { unsigned int key_size, new_record_size, ret = 0, can_rollback = 0; struct record record, previous_record; char sector_buff[STORAGE_SIZE]; struct iter_state is; memcpy(sector_buff, STORAGE_ADDRESS, STORAGE_SIZE); if(check_for_duplicates(sector_buff) || key_exists(sector_buff, new_key, §or_buff[STORAGE_SIZE], 0, NULL) || check_for_empty_records(sector_buff)) { fs_erase(); record_iter_init(&is, sector_buff, STORAGE_SIZE); while(record_iter_next(&is, &record, NULL)) { if(is_empty(&record)) continue; if(!key_exists((char *)STORAGE_ADDRESS, record.key, STORAGE_ADDRESS + STORAGE_SIZE, 1, NULL)) { struct record rec; if(!key_exists(sector_buff, record.key, §or_buff[STORAGE_SIZE], 0, &rec)) continue; if(strcmp(new_key, record.key) == 0) { // If we are about to write this key we don't keep the old value. previous_record = rec; // This holds the old record in case we need it back (for instance if new record is too long) can_rollback = 1; } else fs_write(record.key, rec.value, rec.value_len); } } ret = 1; } key_size = strlen(new_key) + 1; new_record_size = key_size + buf_len + sizeof(new_record_size); if(can_rollback && new_record_size > get_free_space()) { fs_write(new_key, previous_record.value, previous_record.value_len); } return ret; }