Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lo(sx,sy)
- % lo(sx,sy) - play lights out
- % sx,sy: board size
- L=zeros(sx,sy);
- % initialize
- init=rand(sx,sy)>=0.5;
- for x = 1:sx
- for y = 1:sy
- if init(x,y)
- L=ltoggle(L,x,y);
- end
- end
- end
- while any(any(L))
- % pintar
- lplot(L);
- % esperar clic
- [cx cy] = ginput(1);
- cx = floor(cx);
- cy = floor(cy);
- % ejecutar paso
- L=ltoggle(L, cx, cy);
- end
- lplot(L);
- text((sx+1)/2,(sy+1)/2,'YOU WIN','Color','green','FontSize',40,'HorizontalAlignment','center');
- disp('Enhorabuena');
- end
- function lplot(L)
- px = [0 0 1 1];
- py = [0 1 1 0];
- [sx sy] = size(L);
- clf;
- axis([1 sx+1 1 sy+1]);
- hold on;
- for x = 1:sx
- for y = 1:sy
- c = 'kb';
- fill(px+x, py+y, c(1+L(x,y)));
- end
- end
- end
- function L=ltoggle(L,cx,cy)
- [sx sy] = size(L);
- L(cx,cy) = ~L(cx,cy);
- if(cx-1 > 0); L(cx-1,cy) = ~L(cx-1,cy);
- else L(sx,cy) = ~L(sx,cy); end;
- if(cx+1 <= sx);L(cx+1,cy) = ~L(cx+1,cy);
- else L(1,cy) = ~L(1,cy); end;
- if(cy-1 > 0);L(cx,cy-1) = ~L(cx,cy-1);
- else L(cx,sy) = ~L(cx,sy); end;
- if(cy+1 <= sy);L(cx,cy+1) = ~L(cx,cy+1);
- else L(cx,1) = ~L(cx,1);end;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement