Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.35 KB | None | 0 0
  1. function lo(sx,sy)
  2. % lo(sx,sy) - play lights out
  3. % sx,sy: board size
  4.     L=zeros(sx,sy);
  5.    
  6.     % initialize
  7.     init=rand(sx,sy)>=0.5;
  8.     for x = 1:sx
  9.        for y = 1:sy
  10.            if init(x,y)
  11.                L=ltoggle(L,x,y);
  12.            end
  13.        end
  14.     end
  15.    
  16.     while any(any(L))
  17.         % pintar
  18.         lplot(L);
  19.        
  20.         % esperar clic
  21.         [cx cy] = ginput(1);
  22.         cx = floor(cx);
  23.         cy = floor(cy);
  24.        
  25.         % ejecutar paso
  26.         L=ltoggle(L, cx, cy);
  27.     end
  28.     lplot(L);
  29.     text((sx+1)/2,(sy+1)/2,'YOU WIN','Color','green','FontSize',40,'HorizontalAlignment','center');
  30.     disp('Enhorabuena');
  31. end
  32.  
  33. function lplot(L)
  34.     px = [0 0 1 1];
  35.     py = [0 1 1 0];
  36.     [sx sy] = size(L);
  37.     clf;
  38.     axis([1 sx+1 1 sy+1]);
  39.     hold on;
  40.     for x = 1:sx
  41.        for y = 1:sy
  42.            c = 'kb';
  43.            fill(px+x, py+y, c(1+L(x,y)));
  44.        end
  45.     end
  46. end
  47.  
  48. function L=ltoggle(L,cx,cy)
  49.     [sx sy] = size(L);
  50.     L(cx,cy) = ~L(cx,cy);
  51.    
  52.     if(cx-1 > 0); L(cx-1,cy) = ~L(cx-1,cy);
  53.     else L(sx,cy) = ~L(sx,cy); end;
  54.    
  55.     if(cx+1 <= sx);L(cx+1,cy) = ~L(cx+1,cy);
  56.     else L(1,cy) = ~L(1,cy); end;
  57.    
  58.     if(cy-1 > 0);L(cx,cy-1) = ~L(cx,cy-1);
  59.     else L(cx,sy) = ~L(cx,sy); end;
  60.    
  61.     if(cy+1 <= sy);L(cx,cy+1) = ~L(cx,cy+1);
  62.     else L(cx,1) = ~L(cx,1);end;
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement