import sudoku logic codes
This commit is contained in:
		
							parent
							
								
									2954a4d0b3
								
							
						
					
					
						commit
						bb078d798b
					
				
							
								
								
									
										38
									
								
								boards.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								boards.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | |||||||
|  | board = {{}, {}} | ||||||
|  | 
 | ||||||
|  | board[1].problem = { | ||||||
|  |     {5, 3, 0, 0, 7, 0, 0, 0, 0}, | ||||||
|  |     {6, 0, 0, 1, 9, 5, 0, 0, 0}, | ||||||
|  |     {0, 9, 8, 0, 0, 0, 0, 6, 0}, | ||||||
|  |     {8, 0, 0, 0, 6, 0, 0, 0, 3}, | ||||||
|  |     {4, 0, 0, 8, 0, 3, 0, 0, 1}, | ||||||
|  |     {7, 0, 0, 0, 2, 0, 0, 0, 6}, | ||||||
|  |     {0, 6, 0, 0, 0, 0, 2, 8, 0}, | ||||||
|  |     {0, 0, 0, 4, 1, 9, 0, 0, 5}, | ||||||
|  |     {0, 0, 0, 0, 8, 0, 0, 7, 9}, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | board[1].solution = { | ||||||
|  |     {5, 3, 4, 6, 7, 8, 9, 1, 2}, | ||||||
|  |     {6, 7, 2, 1, 9, 5, 3, 4, 8}, | ||||||
|  |     {1, 9, 8, 3, 4, 2, 5, 6, 7}, | ||||||
|  |     {8, 5, 9, 7, 6, 1, 4, 2, 3}, | ||||||
|  |     {4, 2, 6, 8, 5, 3, 7, 9, 1}, | ||||||
|  |     {7, 1, 3, 9, 2, 4, 8, 5, 6}, | ||||||
|  |     {9, 6, 1, 5, 3, 7, 2, 8, 4}, | ||||||
|  |     {2, 8, 7, 4, 1, 9, 6, 3, 5}, | ||||||
|  |     {3, 4, 5, 2, 8, 6, 1, 7, 9}, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | board[2].problem = { | ||||||
|  |     {8, 0, 0, 0, 5, 0, 0, 0, 7}, | ||||||
|  |     {0, 6, 1, 0, 0, 0, 0, 0, 0}, | ||||||
|  |     {3, 7, 0, 0, 0, 0, 0, 6, 8}, | ||||||
|  |     {0, 0, 0, 6, 0, 0, 8, 0, 0}, | ||||||
|  |     {0, 1, 0, 0, 9, 0, 0, 2, 0}, | ||||||
|  |     {0, 3, 0, 2, 0, 0, 0, 9, 0}, | ||||||
|  |     {0, 8, 0, 0, 0, 7, 0, 0, 0}, | ||||||
|  |     {0, 0, 0, 0, 0, 0, 4, 0, 1}, | ||||||
|  |     {0, 0, 5, 0, 0, 0, 9, 8, 3}, | ||||||
|  | } | ||||||
							
								
								
									
										116
									
								
								check.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								check.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,116 @@ | |||||||
|  | local function checkCT(ct) | ||||||
|  |     local r = {} | ||||||
|  |     for i = 1, 8 do | ||||||
|  |         for j = i+1, 9 do | ||||||
|  |             if ct[i] == ct[j] and ct[i] ~= 0 then | ||||||
|  |                 table.insert(r, {i, j}) | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     if #r > 0 then return false, r end | ||||||
|  |     return true | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | local function findThisTable(this, that) | ||||||
|  |     for i = 1, #that do | ||||||
|  |         if #that[i] == #this then | ||||||
|  |             local flag = true | ||||||
|  |             for j = 1, #this do | ||||||
|  |                 if this[j] ~= that[i][j] then flag = false; break end | ||||||
|  |             end | ||||||
|  |             if flag then return true, i end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     return false | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function check(board) | ||||||
|  |     local rr = {} | ||||||
|  |     for x = 1, 9 do | ||||||
|  |         local ct = {} | ||||||
|  |         for y = 1, 9 do | ||||||
|  |             ct[y] = board[x][y] | ||||||
|  |         end | ||||||
|  |         local ok, r = checkCT(ct) | ||||||
|  |         if not ok then | ||||||
|  |             for _, p in ipairs(r) do | ||||||
|  |                 local t = {x, p[1], x, p[2], board[x][p[1]]} | ||||||
|  |                 if not findThisTable(t, rr) then | ||||||
|  |                     table.insert(rr, t) | ||||||
|  |                 end | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     for y = 1, 9 do | ||||||
|  |         local ct = {} | ||||||
|  |         for x = 1, 9 do | ||||||
|  |             ct[x] = board[x][y] | ||||||
|  |         end | ||||||
|  |         local ok, r = checkCT(ct) | ||||||
|  |         if not ok then | ||||||
|  |             for _, p in ipairs(r) do | ||||||
|  |                 local t = {p[1], y, p[2], y, board[p[1]][y]} | ||||||
|  |                 if not findThisTable(t, rr) then | ||||||
|  |                     table.insert(rr, t) | ||||||
|  |                 end | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     for i = 0, 2 do | ||||||
|  |         for j = 0, 2 do | ||||||
|  |             local ct = {} | ||||||
|  |             for x = 1, 3 do | ||||||
|  |                 for y = 1, 3 do | ||||||
|  |                     ct[(x-1)*3+y] = board[x+i*3][y+j*3] | ||||||
|  |                 end | ||||||
|  |             end | ||||||
|  |             local ok, r = checkCT(ct) | ||||||
|  |             if not ok then | ||||||
|  |                 for _, p in ipairs(r) do | ||||||
|  |                     local x1 = ((p[1] - 1) // 3 + 1) + i * 3 | ||||||
|  |                     local y1 = ((p[1] - 1) % 3 + 1) + j * 3 | ||||||
|  |                     local x2 = ((p[2] - 1) // 3 + 1) + i * 3 | ||||||
|  |                     local y2 = ((p[2] - 1) % 3 + 1) + j * 3 | ||||||
|  |                     local t = {x1, y1, x2, y2, board[x1][y1]} | ||||||
|  |                     if not findThisTable(t, rr) then | ||||||
|  |                         table.insert(rr, t) | ||||||
|  |                     end | ||||||
|  |                 end | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     if #rr > 0 then | ||||||
|  |         return false, rr | ||||||
|  |     end | ||||||
|  |     return true | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function checkSolved(board) | ||||||
|  |     local r = {} | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         for j = 1, 9 do | ||||||
|  |             if board[i][j] < 1 or board[i][j] > 9 then | ||||||
|  |                 table.insert(r, {i, j, board[i][j]}) | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     local ok, rr = check(board) | ||||||
|  |     if #r > 0 or (rr and #rr > 0) then | ||||||
|  |         return false, r, rr | ||||||
|  |     end | ||||||
|  |     return true | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function pcheck(board) | ||||||
|  |     local ok, r, rr = checkSolved(board) | ||||||
|  |     if ok then | ||||||
|  |         print("Board is solved!") | ||||||
|  |         return  | ||||||
|  |     end | ||||||
|  |     for _, p in ipairs(r) do | ||||||
|  |         print(S("(%d, %d) = %d", unpack(p))) | ||||||
|  |     end | ||||||
|  |     for _, p in ipairs(rr) do | ||||||
|  |         print(S("(%d, %d) = (%d, %d) = %d", unpack(p))) | ||||||
|  |     end | ||||||
|  | end | ||||||
							
								
								
									
										44
									
								
								sudoku.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								sudoku.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,44 @@ | |||||||
|  | require "utils" | ||||||
|  | 
 | ||||||
|  | function calValidOptions(b, x, y) | ||||||
|  |     if b[x][y] ~= 0 then return {} end | ||||||
|  |     local elm | ||||||
|  |     local u = {} | ||||||
|  |     local v = {} | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         elm = b[x][i] | ||||||
|  |         if elm ~= 0 and find(u, elm) == 0 then | ||||||
|  |             table.insert(u, elm) | ||||||
|  |         end | ||||||
|  |         elm = b[i][y] | ||||||
|  |         if elm ~= 0 and find(u, elm) == 0 then | ||||||
|  |             table.insert(u, elm) | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     local xc = ((x - 1) // 3) * 3 | ||||||
|  |     local yc = ((y - 1) // 3) * 3 | ||||||
|  |     for i = 1, 3 do | ||||||
|  |         for j = 1, 3 do | ||||||
|  |             elm = b[i+xc][j+yc] | ||||||
|  |             if elm ~= 0 and find(u, elm) == 0 then | ||||||
|  |                 table.insert(u, elm) | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         if find(u, i) == 0 then table.insert(v, i) end | ||||||
|  |     end | ||||||
|  |     return v | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function buildSearchSpace(b) | ||||||
|  |     local s = {} | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         s[i] = {} | ||||||
|  |         for j = 1, 9 do | ||||||
|  |             s[i][j] = calValidOptions(b, i, j) | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     return s | ||||||
|  | end | ||||||
|  | 
 | ||||||
							
								
								
									
										28
									
								
								utils.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								utils.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,28 @@ | |||||||
|  | function find(array, element) | ||||||
|  |     local index = 0 | ||||||
|  |     for i = 1, #array do | ||||||
|  |         if array[i] == element then | ||||||
|  |             index = i | ||||||
|  |             break | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     return index | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function showBoard(board) | ||||||
|  |     for row = 1, 9 do | ||||||
|  |         print(table.concat(board[row], " ")) | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function cloneBoard(b) | ||||||
|  |     local c = {} | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         c[i] = {} | ||||||
|  |         for j = 1, 9 do | ||||||
|  |             c[i][j] = b[i][j] | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     return c | ||||||
|  | end | ||||||
|  | 
 | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user