image thumbnail
from MATLAB Picross by Husam Aldahiyat
MATLAB version of the popular game.

picross(varargin)
function picross(varargin)
%{
MATLAB Picross

Picross is a fun game where you have to cross out the tiles based on the outer numbers.
The numbers are placed in the order the tiles need to be crossed out in.

Example:
2,2 means there are two crossed out tiles, then a space of one or more tiles, then another two tiles.

In the end, and after all correct tiles are crossed out, you get to see a picture, hence the word picross!

Clicking on a tile one time "marks" it with the gray colour.
Clicking on a gray tile reveals that tile. If it is indeed a crossed out tile it will turn black.
However, if you had revealed a wrong tile, it will turn red.

Get five red tiles to lose the game.

There are only three levels to choose from (three drawings) but you can very easily add your own designs
by adding them as cases in the choose function. Just go there and add your 21 by 21 boolean map and name
and it will be automatically created when you choose the number of that case. If I see that a number of
people like the game then I could add more designs in the future.

Draw Mode allows you to freely make designs and get their corresponding maps.

To launch, simply type 'picross' in your command window.
Alternatively, you can choose run from this editor window, or press 'F5'.

Husam Aldahiyat, 2008, Jordan.
numandina@gmail.com
%}

global h S S2 h2 h3         %I opted to use globals instead of Tags and other methods

figure('visible','on','Menubar','none',...     %Main figure
'name','Picross','Numbertitle','off',...
'color',[.5 .2 .8],'units','normalized',...
'resize','off','position',[.1 .05 .8 .9*1.05],'renderer','zbuffer');

axes('Units','normalized','Position', [.05 .06 .9 .93],'Visible','on',...   	%Our axes
'DrawMode','fast','NextPlot','replace','XLim',[1,5],'YLim',[1,15*1.05]...
,'color',[.5 .2 .8],'xcolor',[.5 .2 .8],'ycolor',[.5 .2 .8]);

%Buttons
uicontrol('style','pushbutton','units','normalized','position',[.1 .05 .075 .05],'string','Start','callback',@st);
hd=uicontrol('style','pushbutton','units','normalized','position',[.1 .35 .075 .05],'string','Draw Mode','callback',@dm);
uicontrol('style','text','units','normalized','position',[.1 .15 .075 .025],'string','Level: ');
he=uicontrol('style','edit','units','normalized','position',[.1 .12 .075 .025],'string','');

    function st(varargin) %If Start button is pressed           
        cla         
        init(1)        
        drawgame(choose(str2double(get(he,'string')))); %Get the level number, its map, and draw the board.
    end

    function dm(varargin) %Draw Mode button
        if strcmp(get(hd,'string'),'Draw Mode')
            map=zeros(21,21);
            cla         
            init(0)                
            set(hd,'string','Finished')
        else
            set(hd,'string','Draw Mode')
            disp(map)
            workspace
            init(1)
        end
    end

map=zeros(21,21);
init(1)
h=zeros(21,21);

    function map=choose(n) %Function that has the different designs.
        switch n
            case 1 %Level 1: Skull                
           map=[1     1     1     1     1     1     1     1     0     0     0     0     0     1     1     1     1     1     1     1     1
                1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
                1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
                1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1
                1     1     1     1     0     0     0     0     0     1     0     1     0     0     0     0     0     1     1     1     1
                1     1     1     1     0     0     0     0     0     0     1     0     0     0     0     0     0     1     1     1     1
                1     1     1     0     0     0     0     1     1     0     1     0     1     1     0     0     0     0     1     1     1
                1     1     0     0     0     0     1     1     1     0     0     0     1     1     1     0     0     0     0     1     1
                1     1     0     0     0     1     1     1     0     0     1     0     0     1     1     1     0     0     0     1     1
                1     1     0     0     0     1     1     1     0     0     1     0     0     1     1     1     0     0     0     1     1
                1     1     0     0     0     0     0     0     0     1     1     1     0     0     0     0     0     0     0     1     1
                1     1     1     0     0     0     0     0     0     1     1     1     0     0     0     0     0     0     1     1     1
                1     1     1     1     0     0     1     0     0     0     0     0     0     0     1     0     0     1     1     1     1
                1     1     1     1     1     1     1     0     0     0     1     0     0     0     1     1     1     1     1     1     1
                1     1     1     1     1     1     1     0     0     0     0     0     0     0     1     1     1     1     1     1     1
                1     1     1     1     1     1     0     0     0     1     0     1     0     0     0     1     1     1     1     1     1
                1     1     1     1     1     1     0     0     1     1     1     1     1     0     0     1     1     1     1     1     1
                1     1     1     1     1     1     0     1     1     1     0     1     1     1     0     1     1     1     1     1     1
                1     1     1     1     1     1     1     0     1     0     1     0     1     0     1     1     1     1     1     1     1
                1     1     1     1     1     1     1     0     0     0     0     0     0     0     1     1     1     1     1     1     1
                1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1];
            k='Skull';
            case 2 %Level 2: Bearded Man
           map=[0     0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0
                0     0     0     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     0     0     0
                0     0     1     1     0     0     0     0     0     0     0     0     0     1     1     0     0     1     0     0     0
                0     1     1     0     0     1     1     0     0     0     0     0     1     0     0     1     0     1     1     0     0
                0     1     0     0     1     0     0     1     0     0     0     0     1     1     0     1     0     0     1     0     0
                1     1     0     0     1     1     0     1     0     0     1     0     0     1     1     1     0     0     1     1     1
                1     0     0     0     1     1     1     0     0     1     1     0     0     0     0     0     0     0     0     0     1
                1     0     0     0     0     0     0     0     1     1     0     0     0     0     0     0     0     0     0     0     1
                1     1     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     1     1
                0     1     1     1     0     0     0     0     1     0     0     0     0     0     0     0     0     0     1     1     0
                0     1     1     1     0     0     0     0     1     1     1     0     0     0     0     1     1     1     0     0     0
                0     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     0     0     0
                0     1     1     1     1     1     0     1     1     1     1     1     0     1     1     1     1     1     0     0     0
                0     1     1     1     1     1     1     1     0     0     0     1     1     1     1     1     1     1     0     0     0
                0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0
                0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0
                0     0     0     0     0     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0
                0     0     0     0     0     0     0     1     1     1     1     1     0     0     0     0     0     0     0     0     0
                0     0     0     0     0     0     0     0     1     1     1     0     0     0     0     0     0     0     0     0     0
                0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0
                0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0];
            k='Bearded Man';                        
            case 3 %Level 3: Eagle
           map=[1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0
                1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0
                1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     0
                1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     1     0     0     1     1     0
                1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     1     0     1     1     1     0
                1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     1     1     1     1     0     0           
                1     1     1     1     1     1     1     1     1     1     0     0     0     0     1     1     1     1     0     0     0
                1     1     1     1     1     1     1     1     1     1     1     0     0     1     1     1     1     1     0     0     0
                0     1     1     1     1     1     1     1     1     1     1     0     0     1     1     1     1     0     0     0     0
                0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0
                0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0
                0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     0     1     0     0     0
                0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0
                0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     0     1     1     0     0     0
                0     0     0     0     1     1     1     1     1     1     1     1     1     1     1     0     0     1     0     0     0
                0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0
                0     0     0     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0
                0     0     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0
                0     1     1     1     1     1     1     1     0     1     1     0     1     1     1     1     0     0     0     0     0
                1     1     1     1     1     1     0     0     0     1     1     0     1     0     0     1     0     0     0     0     0
                1     1     1     1     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0];
            k='Eagle';
        end        
        text('position',[1.15 14.5],'fontweight','bold','string',k,'fontsize',33)
    end

    function drawgame(map) %This function gets the map, then creates its picross board accordingly
        S=zeros(21,21);
        for i11=1:21
            a=map(i11,:);
            s=0;
            k=1;
            for j1=1:21
                if a(j1)
                    s=s+1;
                else
                    if s
                        S(i11,k)=s;
                        k=k+1; 
                    end
                    s=0;                   
                end
            end
            if s
                S(i11,k)=s;                
            end
        end
        se1=0;
        se2=0;
        a1=0;
        i1=1-.115:.115:.115*20+1;
        for i3=1:21  
            a1=a1+1;
            a2=1;
            a=fliplr(S(i3,:));    
            se2=se2+.5;
            for j3=1:21
                if a(j3) || ~sum(a)  
                    no=a(j3);
                    box2(i1(i3)-se1,.5*20+2-se2-.5,no,a1,a2,0);
                    se1=se1+.115;  
                    a2=a2+1;
                    if ~sum(a)
                        break 
                    end
                end
            end
            se1=0+i1(i3)-(1-.115*2);
        end
        S2=zeros(21,21);
        for i9=1:21
            a=map(:,i9);
            s=0;
            k=1;
            for j9=1:21
                if a(j9)
                    s=s+1;
                else
                    if s
                        S2(i9,k)=s;
                        k=k+1; 
                    end
                    s=0;
                end
            end
            if s
                S2(i9,k)=s;                
            end
        end
        se1=0;
        a1=0;
        i1=1:.5:.5*20+1 ;
        for i8=21:-1:1
            a=fliplr((S2(i8,:)));
            se1=se1+.115;
            se2=0+i1(i8)+(1-.5*2);
            a2=1;
            a1=a1+1;
            for j8=1:21
                if a(j8) || ~sum(a)  
                    no=a(j8);
                    box2(.115*20+2.5-se1-.115-1-.115*2-.115/3-.115/53,i1(i8)-se2+11.5,no,a1,a2,1);
                    se2=se2-.5;            
                    if ~sum(a)
                        break 
                    end
                    a2=a2+1;
                end
            end
        end
    end

    function init(l) %Creates the inital white boxes
        clear h h2 h3 S S2
        a=0;
        for i=1:.115:.115*20+1
            a=a+1;
            b=0;
            for j=1:.5:.5*20+1
                b=b+1;           
                box(i,j,a,b,l)
            end
        end
    end

    function cc(varargin)   %Function of clicking a patch in draw mode
        p=get(gca,'currentpoint');
        p=p(1,1:2);     
        if sum((get(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor')))-2.4<1e-1        
            set(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor','w')
            map(22-ceil((p(2)-2)/.5)-2,ceil((p(1)-2.5)/.115))=0;
        else            
            set(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor','k')
            map(22-ceil((p(2)-2)/.5)-2,ceil((p(1)-2.5)/.115))=1;
        end
%         disp(map)
    end

    function bb(varargin)   %Function of clicking a patch in solve mode
        if isempty(get(he,'string')) || ~isempty(findobj('position',[3 9]))
            return 
        end
        map=choose(str2double(get(he,'string')));
        p=get(gca,'currentpoint');
        p=p(1,1:2);     

        if sum((get(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor')))-2.4<1e-1        
            if map(22-ceil((p(2)-2)/.5)-2,ceil((p(1)-2.5)/.115))            
                set(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor','k')
            else
                set(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor','r')
            end
        elseif sum((get(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor')))-3<1e-1
            set(h(ceil((p(1)-2.5)/.115),ceil((p(2)-2)/.5)+2),'facecolor',[.8 .8 .8])
        end
              
        if  numel(findobj('facecolor','k'))==sum(sum(choose(str2double(get(he,'string')))))
            set(findobj('facecolor','r'),'facecolor','w','edgecolor','w')
            set(findobj('facecolor',[.8 .8 .8]),'facecolor','w','edgecolor','w')
            for g=1:21
                for k=1:21
                    set(h(g,k),'edgecolor','w') 
                end
            end
            set(findobj('facecolor','k'),'facecolor','k','edgecolor','k')
        end
        if numel(findobj('facecolor','r'))>4
            text('position',[3 9],'string','YOU LOSE!','fontsize',50,'color','r')
        end
        hud=fliplr(h);
        for k=1:21
            k1=numel(findobj(hud(:,k),'facecolor','k'));
            k2=sum(map(k,:));
            if abs(k1-k2)<1e-1
                pl=h3(k,:);               
                for ij=1:length(pl(pl>0))
                    set(h3(k,ij),'facecolor','m')
                end
            end
        end
        hud=(flipud(h));
        for k=1:21
            k1=numel(findobj(hud(22-k,:),'facecolor','k'));
            k2=sum(map(:,k));
            if abs(k1-k2)<1e-1
                pl=h2(22-k,:);                
                for ij=1:length(pl(pl>0))
                    set(h2(22-k,ij),'facecolor','m')
                end
            end
        end
    end

    function box(x,y,a,b,l) %Used for drawing the white boxes
        if l        %solve mode
            h(a,b)=patch([0 0 .115 .115]+x+1.5,[0 .5 .5 0]+y,'w','buttondownfcn',@bb);
        else        %draw mode
            h(a,b)=patch([0 0 .115 .115]+x+1.5,[0 .5 .5 0]+y,'w','buttondownfcn',@cc);
        end
    end

    function box2(x,y,no,a,b,w) %For drawing the outer boxes
        text('position',[0+x+1.535,-1+y+1.25],'string',num2str(no))       
        if w %Horizontal
            h2(a,b)=patch([0 0 .115 .115]+x+1.5,[0 .5 .5 0]+y,'c','linewidth',2,'FaceVertexAlphaData',0.5,'FaceAlpha','flat');
        else %Vertical
            h3(a,b)=patch([0 0 .115 .115]+x+1.5,[0 .5 .5 0]+y,'c','linewidth',2,'FaceVertexAlphaData',0.5,'FaceAlpha','flat');
        end
    end
end

Contact us at files@mathworks.com