function lucky7(action)
global n t_fin c_cycle t_cycle
if nargin < 1, 
	action = 'initialise';
	n=7;
end;

if strcmp(action,'initialise'),
	p=(n-1)/2+2;
	% centres des cercles
	t1=[pi/p:2*pi/p:2*pi];
	t2=[pi+3*pi/p:2*pi/p:3*pi-pi/p];
	dx=2*cos(pi/p);
	cx=[cos(t1) cos(t2)+dx];
	cy=[sin(t1) sin(t2)];
	
	% traits de fond
	t1=pi/p:pi/400:2*pi-pi/p;
	t2=pi+pi/p:pi/400:3*pi-pi/p;
	x1=cos(t1);
	y1=sin(t1);
	x2=cos(t2);
	y2=sin(t2);
	plot(x1,y1,'k',x2+dx,y2,'k',...
	0.95*x1,0.95*y1,'k',0.95*x2+dx,0.95*y2,'k',...
	1.05*x1,1.05*y1,'k',1.05*x2+dx,1.05*y2,'k',...
	[cos(pi/p) cos(2*pi-pi/p)],[sin(pi/p) sin(2*pi-pi/p)],'k',...
	0.92*[cos(pi/p) cos(2*pi-pi/p)],0.92*[sin(pi/p) sin(2*pi-pi/p)],'k',...
	1.08*[cos(pi/p) cos(2*pi-pi/p)],1.08*[sin(pi/p) sin(2*pi-pi/p)],'k') 
	hold on 
	
	% représentation des cercles
	plot(cx,cy,'.r','markersize',200)
	ind=[2:n+1 1];
	[val,tirage]=sort(rand(1,n));
	% tirage=1:n;
	for i=1:n
		h(i)=text(cx(ind(i))-0.05,cy(ind(i)),num2str(tirage(i)),...
		'Fontsize',36,...
		'FontName','Times',...
		'color','y',...
		'userdata',i,...
		'buttondownfcn','lucky7(''clic'')');
	end

	% case vide
	h(n+1)=text(cx(ind(n+1))-0.05,cy(ind(n+1)),'0',...
	'Fontsize',36,...
	'FontName','Times',...
	'color','r',...
	'userdata',n+1,...
	'buttondownfcn','lucky7(''clic'')');
	
	axis off
	axis equal
	hold off
	set(gca,'userdata',h)
	set(gcf,'userdata',tirage,...
	'doublebuffer','on',...
	'Name','Lucky7',...
	'NumberTitle','off')
	t_fin=title(' ');
	
	% ajout des boutons
	uicontrol('string','Mélange','callback','lucky7(''initialise'')')
	uicontrol('string','Triche','Position',[100 20 60 20],...
	'callback','lucky7(''triche'')')
	uicontrol('string','<','Position',[180 20 20 20],...
	'callback','lucky7(''diminue'')')
	uicontrol('style','text','string',num2str(n),...
	'Position',[210 18 20 20],'tag','n')
	uicontrol('string','>','Position',[240 20 20 20],...
	'callback','lucky7(''augmente'')')
	uicontrol('string','Reset','Position',[380 20 60 20],...
	'callback','lucky7(''reset'')')
	c_cycle=uicontrol('style','check','value',0,...
	'userdata',0,'callback','lucky7(''clic'')',...
	'Position',[265 20 20 20]);
	uicontrol('style','text','string','Cycles',...
	'Position',[280 16 50 20])
	t_cycle=uicontrol('style','text','string','',...
	'Position',[20 40 400 20],...
	'HorizontalAlignment','left');
	figure(gcf)
end

if strcmp(action,'clic')
	h=get(gca,'userdata');
	no=get(gco,'userdata');
	avant=get(gcf,'userdata');
	n=length(avant);
	apres=avant;
	n1=1;
	n2=(n-1)/2;
	n3=n2+1;
	n4=n2+2;
	n5=n;
	n6=n+1;
	switch no
	case n1
		apres(n3)=avant(n1);
		apres(1:n3-1)=avant(2:n3);
	case n2
		apres(n1)=avant(n3);
		apres(2:n3)=avant(1:n3-1);
	case n3
		apres(1)=avant(n5);
		apres(2:n5)=avant(1:n5-1);
	case n4
		apres(n3)=avant(n5);
		apres(n4:n5)=avant(n3:n5-1);
	case n5
		apres(n5)=avant(n3);
		apres(n3:n5-1)=avant(n4:n5);
	case n6
		apres(n5)=avant(n1);
		apres(1:n5-1)=avant(2:n5);
	end

	set(gcf,'userdata',apres)
	for i=1:n
		set(h(i),'string',apres(i))
	end

	% test de fin
	if apres==(1:n)
		set(t_fin,'string','BRAVO')
		% set(h,'ButtonDownFcn','')
	end

	% affichage des cycles
	avant=apres;
	affich=get(c_cycle,'value');
	if affich
		avant=get(gcf,'userdata');
		liste=[];
		t_cycles=[];
		for deb=1:n
			if ~ismember(deb,liste)
				t_cycles=[t_cycles,'(',num2str(deb)];
				fin=find(avant==deb);
				while fin~=deb
					t_cycles=[t_cycles,' ',num2str(fin)];
					liste=[liste fin];
					fin=find(avant==fin);
				end
				t_cycles=[t_cycles,')'];
			end
		end
		set(t_cycle,'string',t_cycles)
	else
		set(t_cycle,'string','')
	end
end

if strcmp(action,'augmente')
	avant=get(gcf,'userdata');
	n=min([length(avant)+2,11]);
	lucky7('initialise')
end

if strcmp(action,'diminue')
	avant=get(gcf,'userdata');
	n=max([length(avant)-2,3]);
	lucky7('initialise')
end

if strcmp(action,'reset')
	h=get(gca,'userdata');
	for i=1:n
		set(h(i),'string',i)
	end
	set(gcf,'userdata',1:n)
end

if strcmp(action,'triche')
	h=get(gca,'userdata');
	avant=get(gcf,'userdata');
	apres=avant;
	apres(1)=avant(2);
	apres(2)=avant(1);
	set(gcf,'userdata',apres)
	for i=1:n
		set(h(i),'string',apres(i))
	end
end


