function fillBoard() % % Checks to see if there are any places where the board has only % one valid location, and if so it fills in that location with % the assigned value. % % Author: Greg Coombe % Date: Nov 29, 2005 global B; global Valid; global RegionTable; % Let's keep track of how many elements we fill in count = 0; % Loop over every cell, and check whether only one valid value exists % If so, then fill in this value for i=1:9, for j=1:9, % if there isn't an assigned value here yet if ( B(i,j) == 0 ) % Create a T/F vector for valid values tf_vec = Valid(i,j,:) == 1; % If there is only one true value if ( sum( tf_vec ) == 1 ) % % Possibility #1: There are no other valid % choices for this cell % % get the index of the true value tmp = 1:9; value = tmp(tf_vec); % do some paranoia checks; this should be a scalar value if ( all( size(value) ~= 1)) error('Error in logic; multiple valid values!!') end %if % looks good, assign the value. B(i,j) = value; Valid(i,j,:) = zeros(1,9); count = count+1; else % % Possibility #2: There are no other valid % places in this row, column, or region for % this value % for v=1:9 if ( Valid(i,j,v) ) rsum = sum(Valid(i,:,v)); csum = sum(Valid(:,j,v)); % look up our region neighbors in table region_i = RegionTable(i,:); region_j = RegionTable(j,:); val_reg = Valid(region_i, region_j, v); rgsum = sum(sum(val_reg)); %if ( rsum == 1 || csum == 1 || rgsum == 1) if ( rsum == 1 || csum == 1 || rgsum == 1) % Assign the value B(i,j) = v; computeValid; return; end end end end end %if computeValid; end %for end %for %disp(['Filled in ' num2str(count) ' values.']);