% Plot and visualize the eight-queen problem % Roger Jang, Oct 96 side = 8; eliminate = (3*(side-1)+1)*ones(side); fontSize = 15; gridColor = 'm'; % Clear the current axis cla reset; textH = zeros(side); % Place the text in the correct locations for i=1:side, % Index over number of columns for j=1:side, numberString=num2str(eliminate(i,j)); textH(i,j)=text(j-.5,side-i+.5,numberString, ... 'HorizontalAlignment','center', ... 'Color','y', ... 'FontWeight','bold', ... 'FontSize',fontSize); end; end; % Set grids set(gca,'Box','on', ... 'Visible','on', ... 'xLim',[0 side], ... 'xGrid','on', ... 'xTickLabels',[], ... 'xTick',0:side, ... 'yGrid','on', ... 'yLim',[0 side], ... 'yTickLabels',[], ... 'yTick',0:side, ... 'Aspect',[1, NaN], ... 'GridLineStyle',':', ... 'LineWidth',3, ... 'XColor',gridColor, ... 'YColor',gridColor); title('Eliminate Matrix'); set(textH, 'erase', 'xor'); xlabel('Choose the next Queen''s position by clicking on the board.'); big_number = 100; AxisH = gca; FigH = gcf; statusH = get(gca, 'xlabel'); set(statusH, 'erase', 'xor'); move_n = 0; % The following is for animation % action when button is first pushed down action1 = ['[p,q]=find(textH==gco);', ... 'if p~=[] & eliminate(p,q)~=big_number,', ... 'set(textH(p,q),''string'',''Q'',''color'',''w'',''fontsize'',20);', ... 'update8q;', ... 'move_n = move_n + 1;', ... 'if move_n==side,', ... 'set(statusH, ''string'', ''Congratulations! You made it! But can you do it again?'');', ... 'elseif sum(sum(abs(eliminate-big_number)))==0,', ... 'set(statusH, ''string'', ''Sorry that you could''''t made it. Try one more time!'');', ... 'end;', ... 'end;']; % actions after the mouse is pushed down % (This action cannot be an empty string.) action2 = ['fprintf('''');']; % action when button is released action3 = []; % temporary storage for the recall in the down_action set(AxisH,'UserData',action2); % set action when the mouse is pushed down down_action=[ ... 'set(FigH,''WindowButtonMotionFcn'',get(AxisH,''UserData''));' ... action1]; set(FigH,'WindowButtonDownFcn',down_action); % set action when the mouse is released up_action=[ ... 'set(FigH,''WindowButtonMotionFcn'','' '');', action3]; set(FigH,'WindowButtonUpFcn',up_action); % Make everything interruptible set(findobj(FigH,'Interrupt','no'), 'Interrupt','yes');