function readPort(u2,DatagramReceived,locb,atb,hfig)
% Author: Isaac Noh
% Copyright 2008-2009 The MathWorks, Inc.
% Version: 1.1
persistent numhits;
if isempty(numhits)
numhits = 0;
end
data = fscanf(u2);
% Display ship locations at end of game
if length(data(1:end-1)) > 8
loc = cellstr(reshape(data(1:end-1),17,[]));
for i = 1:17
row = (11 - (double(loc{i}(1)) - 64))/10;
col = str2num(loc{i}(2:end))/10;
ax = atb.axhandle;
H = zeros(2,2,3);
H(:,:,2) = 0.6;
surface(zeros(2),H,...
'XData',[(col - 0.1) col],...
'YData',[(row - 0.1) row],...
'Parent',ax);
hUI = findobj(hfig,'UserData',loc{i});
%set(hUI,'BackgroundColor',[0 0.6 0]);
end
return;
end
target = regexprep(data,'\s','');
switch target
%%% BEGIN Ready or Not Ready %%%
case 'ready1'
switch get(locb.bhandle,'Enable')
case 'on'
fprintf(u2,'notready');
case 'off'
fprintf(u2,'ready2');
end
case 'ready2'
switch get(locb.bhandle,'Enable')
case 'on'
set(atb.bhandle,'Enable','inactive');
otherwise
set(atb.bhandle,'Enable','on');
msgbox('Player 2 is ready.');
end
case 'notready'
set(atb.bhandle,'Enable','inactive');
%%% END Ready or Not Ready %%%
%%% BEGIN Speak %%%
case 'sunkbs'
speak('You sunk my battleship!')
%%% END Speak
%%% BEGIN Hit or Miss %%%
case 'true'
hM = findobj(hfig,'Label','Sound On');
val = get(hM,'Checked');
if strcmp(val,'on')
load bshipSounds y_h Fs_h bits_h;
sound(y_h,Fs_h,bits_h);
hUI = findobj(hfig,'Style','radiobutton',...
'Value',1);
pause(3);
else
hUI = findobj(hfig,'Style','radiobutton',...
'Value',1);
end
set(hUI,'BackgroundColor','r','Enable','off','Value',0);
ud = get(hUI,'UserData');
row = (11 - (double(ud(1)) - 64))/10;
col = str2num(ud(2:end))/10;
H = zeros(2,2,3);
H(:,:,1) = 1;
surface(zeros(2),H,...
'XData',[(col - 0.1) col],...
'YData',[(row - 0.1) row],...
'Parent',atb.axhandle);
case 'false'
hM = findobj(hfig,'Label','Sound On');
val = get(hM,'Checked');
if strcmp(val,'on')
load bshipSounds y_m Fs_m bits_m;
sound(y_m,Fs_m,bits_m);
hUI = findobj(hfig,'Style','radiobutton',...
'Value',1);
pause(3);
else
hUI = findobj(hfig,'Style','radiobutton',...
'Value',1);
end
set(hUI,'BackgroundColor','w','Enable','off','Value',0);
ud = get(hUI,'UserData');
row = (11 - (double(ud(1)) - 64))/10;
col = str2num(ud(2:end))/10;
H = ones(2,2,3);
surface(zeros(2),H,...
'XData',[(col - 0.1) col],...
'YData',[(row - 0.1) row],...
'Parent',atb.axhandle);
%%% END Hit or Miss %%%
%%% BEGIN Game Over %%%
case 'win'
msgbox('You win! Game Over.');
loc = char(locb.getlocation());
fprintf(u2,loc);
%%% END Game Over %%%
otherwise
loc = locb.getlocation();
row = (11 - (double(target(1)) - 64));
col = str2num(target(2:end));
gp = locb.grid(row,col);
if any(strcmp(loc,target))
gp.sethit(true);
numhits = numhits + 1;
fprintf(u2,'true');
if numhits == 17
gameOver(u2);
elseif numhits > 17
errordlg('Too many hits');
end
else
gp.sethit(false);
fprintf(u2,'false');
end
set(atb.bhandle,'Enable','on');
end