Code covered by the BSD License  

Highlights from
BattleShip (R)

image thumbnail

BattleShip (R)

by

 

23 Oct 2008 (Updated )

Learn and have fun with an old classic.

bsconnect(hObject,eventdata,locb,atb)
function bsconnect(hObject,eventdata,locb,atb)
%  	Author: Isaac Noh
%   Copyright 2008-2009 The MathWorks, Inc.
%   Version: 1.1


prompt = {'Connect to:' 'Are you "Player 1 (Y or N)?"'};
dlgtitle = 'Connection Dialog';
answer = inputdlg(prompt,dlgtitle,1);
set(locb.bhandle,'Enable','on');

switch strcmpi(answer{2},'y')
    case true
        % Player 1 attacks first
        % Create UDP connection for Player 1
        u1 = udp(answer{1}, 'RemotePort', 8866, 'LocalPort', 8844, 'TimeOut', 600);
        u1.DatagramReceivedFcn = {@readPort,locb,atb,gcbf};
        
        set(locb.bhandle,'UserData','1');
        
    case false
        % Player 2 defends first
        % Create UDP connection for Player 2
        u1 = udp(answer{1}, 'RemotePort', 8844, 'LocalPort', 8866, 'TimeOut', 600);
        u1.DatagramReceivedFcn = {@readPort,locb,atb,gcbf};
        
        set(locb.bhandle,'UserData','2');
        
end

% Save the object to GUIDATA
guidata(gcbo,u1);

% Open connection
fopen(u1);

Contact us