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.

battleship
function battleship
%  	Author: Isaac Noh
%   Copyright 2008-2009 The MathWorks, Inc.
%   Version: 1.1

clear readPort

figure('Units','normalized',...
    'Position',[0.25 0.1 0.5 0.8],...
    'Menubar','none',...
    'Name','Battleship',...
    'NumberTitle','off',...
    'Resize','off',...
    'CloseRequestFcn', 'bsclose')

atb = atboard;

locb = locboard;

f = uimenu('Label','Connection');
uimenu(f,'Label','Connect','Callback',{@bsconnect,locb,atb});

f = uimenu('Label','Talk');
uimenu(f,'Label','You sunk my battleship!','Callback',@sunk);

f = uimenu('Label','Sound');
uimenu(f,'Label','Sound On','Checked','on','Callback',@soundOff);

pt = [0.1 0.2];
destroyer = [0.1 0.3];
sub = [0.1 0.3];
battleship = [0.1 0.4];
carrier = [0.1 0.5];

k = round(rand(1));
if k == 1
    orientation = 'horizontal';
else
    orientation = 'vertical';
end
pt = rot90(pt,2*k);
s(1) = ship([rand(1) rand(1) pt],'pt',orientation, locb.axhandle);

k = round(rand(1));
if k == 1
    orientation = 'horizontal';
else
    orientation = 'vertical';
end
destroyer = rot90(destroyer,2*k);
s(2) = ship([rand(1) rand(1) destroyer],'destroyer',orientation, locb.axhandle);

k = round(rand(1));
if k == 1
    orientation = 'horizontal';
else
    orientation = 'vertical';
end
sub = rot90(sub,2*k);
s(3) = ship([rand(1) rand(1) sub],'sub',orientation, locb.axhandle);

k = round(rand(1));
if k == 1
    orientation = 'horizontal';
else
    orientation = 'vertical';
end
battleship = rot90(battleship,2*k);
s(4) = ship([rand(1) rand(1) battleship],'battleship',orientation, locb.axhandle);

k = round(rand(1));
if k == 1
    orientation = 'horizontal';
else
    orientation = 'vertical';
end
carrier = rot90(carrier,2*k);
s(5) = ship([rand(1) rand(1) carrier],'carrier',orientation, locb.axhandle);

Contact us