%-- 15.11.08 17:47 --% function SudokuFree() start global reihenzellen global spaltenzellen global hauptzellen global Zelle function start() %<--------Graphische Oberfläche-----------> figure('name', 'Soduko löser', 'Color', [1 1 1], 'Units', 'pixels','Position', [300 300 378 458]) spaceX = 0; spaceY = 0; for i = 1:9 if i == 4 || i == 7 spaceY = spaceY + 10; end spaceX = 0; for a = 1:9 if a == 4 || a == 7 spaceX = spaceX + 10; end uicontrol('Style','edit','position', [50+(a-1)*30+spaceX, 77+(i-1)*30+spaceY,21,21]); end end uicontrol('Style','push','String','Soduko lösen','position', [70 ,32 ,80, 26],'Callback',@pushbutton1_Callback); uicontrol('Style','push','String','Zurücksetzen','position', [220 ,32 ,80, 26],'Callback', @pushbutton2_Callback); uicontrol('Style','text','BackgroundColor', [1 1 1],'FontSize', 25,'String','Sodukolöser','position', [59, 380, 257, 51]); uicontrol('Style','text','BackgroundColor', [1 1 1],'String','von Isabelle Wetzstein','position', [250, 8, 113, 16]); %<--------Graphische Oberfläche-----------> %<---------Variablen zum Lösen------------> reihenzellen = cell(81,8); spaltenzellen = cell(81,8); hauptzellen = cell(81,8); for reihe = 1:9 for spalte = 1:9 position = (reihe-1) * 9 + spalte; i = 1; for s = 1:9 if (s ~= spalte) reihenzellen{position,i} = (reihe-1) * 9 + (s-1)+1; i = i+1; end end i = 1; for r = 1:9 if (r ~= reihe) spaltenzellen{position,i} = 9 * (r-1) + spalte; i = i+1; end end hauptreihe = floor((reihe-1) / 3); hauptspalte = floor((spalte-1) / 3); i = 1; for hr = 0:2 for hs = 0:2 index = (((hauptreihe * 3) + hr) * 9) + (hauptspalte * 3) + hs+1; if (index ~= position) hauptzellen{position,i} = index; i = i+1; end end end end end end function s = loese(position, Zelle) s = false; if position > 81 h=findobj('style','edit'); i = 0; for k=h' i = i + 1; set(k,'string',Zelle(i)) end disp ('Erfolgreich gelöst!'); s = true; return; end if (Zelle(position) == 0) for i = 1:9 if (teste(position, i, Zelle)) Zelle(position) = i; if (loese(position + 1, Zelle)) s = true; return; else Zelle(position) = 0; end end end s = false; else loese(position + 1, Zelle); return; end end function y = teste(position, value, Zelle) for i = 1:8 if (Zelle(reihenzellen{position,i}) == value) y = false; return; end if (Zelle(spaltenzellen{position,i}) == value) y = false; return; end if (Zelle(hauptzellen{position,i}) == value) y = false; return; end end y = true; end function pushbutton1_Callback(hObject, eventdata, handles) h=findobj('style','edit'); i = 0; Fehler = 0; for k=h' i = i + 1; z = str2num(get(k,'string')); if z < 10 Zelle(i) = z; else Zelle(i) = 0; end end for position = 1:81 if Zelle(position) ~= 0 if (~teste(position, Zelle(position), Zelle)) msgbox('Das Sudoku konnte nicht gelöst werden, weil es Fehler enthält!!'); ausgabe = ''; Fehler = 1; break; end end end if Fehler ~= 1 loese(1, Zelle) end end function pushbutton2_Callback(hObject, eventdata, handles) h=findobj('style','edit'); for k=h' set(k,'string',''); end end end