function Breaking_Bar(action)
%----------------------------------------------------------------------
%INTRODUCTION:
%Assume you have a chocolate bar (M x N) consisting of number of squares
%arranged in a rectangular pattern. Your task is to split the bar into small
%squares (always breaking along the lines between the squares) with a minimum
%number of breaks. How many will it take?
%ANSWER: "M+N-1" (Unique Solution, hence NO Optimum Solution)
%----------------------------------------------------------------------
global fig M N BrBa msgdisp barbreak BrBa_store game_hand break_hand;
clc;
%----------------------------------------------------------------------
if nargin < 1,
M=3+mod(fix(10*rand(1,1)),2);
N=3+mod(fix(10*rand(1,1)),2);
BrBa=ones(2*M-1,2*N-1);
for ii=1:2*M-1
for jj=1:2*N-1
%Chocolate Pieces
if (mod(ii,2)==1 && mod(jj,2)==1), BrBa(ii,jj)=2; end
%Intersection Points or Junction Points
if (mod(ii,2)==0 && mod(jj,2)==0), BrBa(ii,jj)=1; end
end
end
msgdisp=1;
barbreak=0;
action='Start';
end
%--------------------------------------------------------------------------
if strcmp(action,'Start')
fig=figure( ...
'Name','Breaking Chocolate Bar', 'NumberTitle','off', ...
'Visible','off', 'BackingStore','off','units','normalized');
%----------------------------------------------------------------------
% Handling the handles
game_hand=uicontrol('units','normalized','position',[.3 .04 .07 .05],...
'string','Game','callback','Breaking_Bar(''MathModel'')',...
'interruptible','on','BackgroundColor',[1 0 0]);
uicontrol('units','normalized','position',[.4 .04 .07 .05],...
'string','Help','callback','Breaking_Bar(''Help'')',...
'interruptible','on','BackgroundColor',[1 0.5 0.2]);
uicontrol('units','normalized','position',[.5 .04 .07 .05],...
'string','Reset','callback','Breaking_Bar(''Reset'')',...
'interruptible','on','BackgroundColor',[1 0.5 0.2]);
uicontrol('units','normalized','position',[.6 .04 .07 .05],...
'string','Exit','callback','delete(gcf)',...
'interruptible','on','BackgroundColor',[1 0.5 0.2]);
%drawing Chocolate Pieces
for ii=1:N,
for jj=1:M,
chocolate(ii-0.5,jj-0.5);
end
end
rectangle('Position', [0 0 N M],'linewidth',0.25,'edgecolor',[0.7 0.7 0.7]);
text(0.5*N-0.5,M+0.2,'Breaks:','color',[0.8 0.3 0.3],'fontsize',10,'fontweight','bold');
break_hand=text(0.5*N+0.25,M+0.2,sprintf('%d',barbreak),'color',[0.8 0.1 0.1],'fontsize',10,'fontweight','bold');
figure(fig);
if msgdisp==1,
msg={'click "Game" button to start the Game...'};
msg_handle=msgbox(msg,'Breaking Chocolate Bar!','help');
if ~waitforbuttonpress, close(msg_handle); end
end
end
%--------------------------------------------------------------------------
if strcmp(action,'Reset')
closereq;
Breaking_Bar();
end
%--------------------------------------------------------------------------
if strcmp(action,'MathModel')
set(game_hand,'BackgroundColor',[1 0.5 0.2]);
while (barbreak~=M*N-1),
% while (length(find(BrBa==0))~=(M-1)+(N-1)),
%(M-1)+(N-1)+(M-1)*(N-1)
validpt=[0 0];
set(fig,'Currentpoint',[0 0]);
while (validpt(1)==0 && validpt(2)==0),
validpt = get(fig,'Currentpoint');
row=validpt(1); col=validpt(2);
pause(1);
end
x=row*(N+1); y=col*(M+1);
r=round(x); c=round(y);
if abs(x-r) < abs(y-c), Opt=1; end
if abs(x-r) > abs(y-c), Opt=2; end
if abs(x-r) == abs(y-c), Opt=1+mod(fix(10*rand(1,1)),2); end
if x < r, r=r-1; end
if y < c, c=c-1; end
row=c; col=r;
if Opt==1, row=2*row; col=2*col-1; end
if Opt==2, row=2*row-1; col=2*col; end
valid=0;
if (row>0 && row<2*M && col>0 && col<2*N), valid=1; end
for ii=1:barbreak,
if (BrBa_store(ii,1)==row && BrBa_store(ii,2)==col),
valid=0;
end
end
if valid==0,
[valid,row,col]=heuristic(BrBa_store,barbreak,M,N,row,col);
if mod(row,2)==0, Opt=1; end
if mod(col,2)==0, Opt=2; end
end
if valid==0,
continue; %invalid step
else
BrBa_bkp=BrBa;
BrBa=Update_Bar(Opt,row,col,BrBa);
if BrBa_bkp==BrBa, continue; end
BrBa_bkp=BrBa;
barbreak=barbreak+1;
BrBa_store(barbreak,:)=[row col];
%Updating Math Model Matrix
for ii=1:2*M-1
for jj=1:2*N-1
%Chocolate Pieces
if (mod(ii,2)==1 && mod(jj,2)==1), BrBa(ii,jj)=2; end
%Intersection Points or Junction Points
if (mod(ii,2)==0 && mod(jj,2)==0), BrBa(ii,jj)=1; end
end
end
Update_Board(M,N,BrBa);
set(break_hand,'string',barbreak);
BrBa=BrBa_bkp;
end
end
pause(2);
[namastedata namastemap]=imread('namaste.jpg');
msg={'Good Try...!'};
msg_handle=msgbox(msg,'Breaking Chocolate Bar','custom',namastedata,namastemap);
pause(3);
close(msg_handle);
closereq;
end
%--------------------------------------------------------------------------
if strcmp(action,'Help')
msg={'DESCRIPTION of GAME: ',...
' ',...
'1. Use the intersection lines to break the chocolate bar. ',...
'2. At any instant only single solid piece is allowed to break. ',...
'3. The objective is to minimize breaks to obtain individual pieces.'};
msgbox(msg,'Breaking Bar','help');
end
%--------------------------------------------------------------------------