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.

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

% Create the Location Board
obj = creategrid;
obj = createbutton(obj);
location = {};

obj.getlocation = @getlocation;

    function obj = creategrid

        vert = 0.465;
        hor = 0.1;

        ax = axes('XGrid','on',...
            'YGrid','on',...
            'GridLineStyle','-',...
            'XLimMode','manual',...
            'YLimMode','manual',...
            'XTick',[0:.1:1],...
            'YTick',[0:.1:1],...
            'XTickLabel','',...
            'YTickLabel','',...
            'Units','normalized',...
            'Position',[0.14 0.065 0.6 0.4],...
            'Color',[0 0 .7]);
        obj.axhandle = ax;

        for i = 1:10
            for j = 1:10
                obj.grid(i,j) = locgridpt(ax);
                obj.grid(i,j).setlocation([char(11-i+64) num2str(j)]);
                uicontrol('Style','text',...
                    'Units','normalized',...
                    'Position',[hor vert+(0.0075-0.04*j) 0.03 0.02],...
                    'String',char(j+64),...
                    'BackgroundColor',[0.8 0.8 0.8]);
            end
            uicontrol('Style','text',...
                'Units','normalized',...
                'Position',[hor-0.005+(0.06*i) vert+0.0075 0.03 0.02],...
                'String',i,...
                'BackgroundColor',[0.8 0.8 0.8]);
        end
    end

    function obj = createbutton(obj)
        obj.bhandle = uicontrol('Style','pushbutton',...
            'Units','normalized',...
            'Position',[0.8 0.45 0.1 0.025],...
            'String','Set',...
            'Enable','off',...
            'Callback', @setcb);
    end

    function setcb(hObject, eventdata)
        h = findobj(gcbf,'Type','surface');
        loc = genLocation(h);
        location = loc;

        % Check to make sure the pieces don't overlap
        count = length(unique(loc));
        if count ~= 17
            warndlg('Ships must not overlap.');
            return
        else
            set(h,'ButtonDownFcn','');
            set(hObject,'Enable','off');

            % Determine which player you are
            player = get(hObject,'UserData');
            pready = ['ready' player];

            hb = findobj(gcbf,'String','Fire');
            u1 = guidata(hObject);

            fprintf(u1,pready);

        end
    end

    function out = getlocation
        out = location;
    end
end

Contact us