include Win32Lib.ew sequence Lin, Col, Box, Bot,z Lin = { {6, 0, 0, 0, 0, 4, 0, 9, 0}, {4, 0, 0, 0, 0, 7, 0, 0, 0}, {0, 0, 3, 0, 0, 8, 0, 5, 0}, {0, 6, 1, 4, 0, 0, 0, 0, 0}, {9, 0, 0, 0, 0, 0, 0, 0, 3}, {0, 0, 0, 0, 0, 2, 1, 8, 0}, {0, 7, 0, 2, 0, 0, 9, 0, 0}, {0, 0, 0, 7, 0, 0, 0, 0, 1}, {0, 3, 0, 6, 0, 0, 0, 0, 4} } Col = Lin Box = Lin Bot = Lin constant A={0,0,0, 3,3,3, 6,6,6}, B={1,1,1, 2,2,2, 3,3,3}, C={0,3,6, 0,3,6, 0,3,6}, D={1,2,3, 1,2,3, 1,2,3}, Win = create( Window, "SUDOKU", 0, Default, Default, 300+10+3, 350+15, {WS_DLGFRAME, WS_SYSMENU, WS_MINIMIZEBOX}), Res = create( Button, "Solve", Win, 180, 265+15, 60, 28, 0 ), Lmp = create( Button, "Clear", Win, 60, 265+15, 60, 28, 0 ), Sta = create (StatusBar,"", Win, 0, 0, 0, 0, 0) integer aa,bb,Listo,cambia aa=0 bb=0 Listo=0 cambia=0 for i=1 to 9 do aa=0 if i=4 or i=7 then bb+=5 end if for j=1 to 9 do if j=4 or j=7 then aa+=6 end if if Lin[i][j] then z= { Lin[i][j]+'0'} else z="" end if Bot[i][j] = create( EditText, z, Win, 12+30*(j-1)+aa, 10+28*(i-1)+bb, 30, 28, 0 ) Col[i][j] = Lin[j][i] Box[i][j] = Lin[ A[i]+B[j] ][ C[i]+D[j] ] end for end for constant BOTONES=Bot[1]&Bot[2]&Bot[3]&Bot[4]&Bot[5]&Bot[6]&Bot[7]&Bot[8]&Bot[9] procedure SUDOKU(integer l, integer c)-------------------------- if c>9 then l+=1 c=1 if l>9 then Listo=1 cambia=1 for i=1 to 9 do for j=1 to 9 do setText(Bot[i][j], { Lin[i][j]+'0' } ) end for end for cambia=0 return end if end if if Lin[l][c] then SUDOKU(l,c+1) if Listo then return end if else for N=1 to 9 do if find( N, Lin[l] )=0 and find( N, Col[c] )=0 and find( N, Box[A[l]+B[c]] )=0 then Lin[l][c] = N Col[c][l] = N Box[A[l]+B[c]][C[l]+D[c]] = N SUDOKU(l,c+1) if Listo then return end if Lin[l][c] = 0 Col[c][l] = 0 Box[A[l]+B[c]][C[l]+D[c]] = 0 end if end for end if end procedure procedure RESUELVE(integer self, integer event, sequence params)------------ atom T integer x for i=1 to 9 do for N=1 to 9 do x=find( N, Lin[i] ) if x>0 and find( N, Lin[i][x+1..$] )>0 then setText(Sta,sprintf("ERROR: Dos '%d' en la Linea %d %d %d %d %d %d %d %d %d %d ",{N,i}&Lin[i] )) return end if x=find( N, Col[i] ) if x and find( N, Col[i][x+1..$] ) then setText(Sta,sprintf("ERROR: Dos '%d' en la Columna %d %d %d %d %d %d %d %d %d %d ",{N,i}&Col[i] )) return end if x=find( N, Box[i] ) if x and find( N, Box[i][x+1..$] ) then setText(Sta,sprintf("ERROR: Dos '%d' en la Región %d %d %d %d %d %d %d %d %d %d ",{N,i}&Box[i] )) return end if end for end for T=time() SUDOKU(1,1) if Listo then setText(Sta,sprintf("%f segundos",time()-T)) else setText(Sta,"No tiene Solución") end if Listo=0 end procedure setHandler( Res, w32HClick, routine_id("RESUELVE")) procedure LIMPIA(integer self, integer event, sequence params)--------------- Listo=0 cambia=1 for i=1 to 9 do for j=1 to 9 do Lin[i][j]=0 setText(Bot[i][j], "" ) end for end for cambia=0 Col = Lin Box = Lin setText(Sta,"") end procedure setHandler(Lmp, w32HClick, routine_id("LIMPIA")) procedure CAMBIA(integer self, integer event, sequence params)-------------- integer N,i,j,s if cambia then return end if s=find(self,BOTONES) i= floor((s-1)/9)+1 j=remainder( s-1 ,9)+1 N = find(getText(Bot[i][j]),{"1","2","3","4","5","6","7","8","9"}) Lin[i][j]= N Col[j][i] = N Box[A[i]+B[j]][C[i]+D[j]] = N if N=0 then cambia=1 setText( self, "" ) cambia=0 end if end procedure setHandler( BOTONES, w32HChange, routine_id("CAMBIA")) procedure OnWin(integer self, integer event, sequence params)---------------- setPenColor( Win, 0 ) drawLine( Win, 12, 12+28*3, 22+30*9, 12+28*3 ) drawLine( Win, 12, 12+28*6+5, 22+30*9, 12+28*6+5 ) drawLine( Win, 12+30*3+3, 11, 12+30*3+3, 12+30*8+17 ) drawLine( Win, 12+30*6+3+6, 11, 12+30*6+3+6, 12+30*8+17 ) end procedure setHandler( Win, w32HPaint, routine_id("OnWin")) WinMain( Win,Normal )