From: John L. <jr...@us...> - 2006-05-31 06:06:31
|
Update of /cvsroot/wxlua/wxLua/samples In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv18622/wxLua/samples Modified Files: wxluasudoku.wx.lua Log Message: check to see if soln is possible and cleanup Index: wxluasudoku.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/wxluasudoku.wx.lua,v retrieving revision 1.49 retrieving revision 1.50 diff -C2 -d -r1.49 -r1.50 *** wxluasudoku.wx.lua 30 May 2006 22:56:29 -0000 1.49 --- wxluasudoku.wx.lua 31 May 2006 06:06:21 -0000 1.50 *************** *** 655,658 **** --- 655,681 ---- end + -- For row, col, block check to see that there is at least one possible value + -- for any empty cells, returns true/false + function sudoku.HasPossibleForEmptyCellsAll(row, col, sudokuTable) + local cell + local block_cell = sudoku.BlockToCell(sudoku.RowColToBlock(row, col)) + + for rcb = 1, 9 do + cell = sudoku.RowColToCell(rcb, col) + if (not sudoku.HasCellValue(cell, sudokuTable)) and + TableIsEmpty(sudokuTable.possible[cell]) then return false end + + cell = sudoku.RowColToCell(row, rcb) + if (not sudoku.HasCellValue(cell, sudokuTable)) and + TableIsEmpty(sudokuTable.possible[cell]) then return false end + + cell = rcb - 1 + block_cell + sudoku.LinearBlockCells[rcb] + if (not sudoku.HasCellValue(cell, sudokuTable)) and + TableIsEmpty(sudokuTable.possible[cell]) then return false end + end + + return true + end + -- Get the count of all possible values for rows, cols, and blocks -- returns 3 tables row_possible[row#][value] = #times possible value occurs in row *************** *** 1253,1256 **** --- 1276,1282 ---- guesses[cell] = guesses[cell] + 1 guesses.current = guesses.current + 1 + + if sudoku.HasPossibleForEmptyCellsAll(row, col, s1) then + s1, g = sudoku.DoSolveBruteForce(sudokuTable, backwards, s1, guesses, cell+1) -- if s1 then success! we're all done *************** *** 1260,1263 **** --- 1286,1290 ---- return s2, g end + end end end *************** *** 2898,2901 **** --- 2925,2929 ---- end elseif sudokuGUI.IsCheckedMenuItem(sudokuGUI.ID_SHOW_MISTAKES) then + sudokuGUI.sudokuSolnTable = sudokuGUI.SolveBruteForce(sudokuGUI.sudokuTables[1]) *************** *** 2926,2930 **** end ! sudokuGUI.UpdateGUI() end --- 2954,2958 ---- end ! sudokuGUI.UpdateTable() end *************** *** 2945,2961 **** local msg_idx = 1 - local progressDialog = wx.wxProgressDialog("wxLuaSudoku - Generating...", string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[1], 0, 0), ! 100, sudokuGUI.frame, wx.wxPD_AUTO_HIDE+wx.wxPD_CAN_ABORT+wx.wxPD_ELAPSED_TIME) - -- define handler function here so it'll work w/o gui function sudoku.GeneratePuzzleHook(count, cell) if solve_completed == false then return false end -- canceled solve_progress = solve_progress + 1 ! if (solve_progress >= 1000) then solve_progress = 0 end if solve_progress%10 ~= 0 then return true end if (msg_idx < sudokuGUI.sayings_n) and (os.time() - last_time > 4) then --- 2973,2987 ---- local msg_idx = 1 local progressDialog = wx.wxProgressDialog("wxLuaSudoku - Generating...", string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[1], 0, 0), ! 10000, sudokuGUI.frame, wx.wxPD_AUTO_HIDE+wx.wxPD_CAN_ABORT+wx.wxPD_ELAPSED_TIME) -- define handler function here so it'll work w/o gui function sudoku.GeneratePuzzleHook(count, cell) if solve_completed == false then return false end -- canceled solve_progress = solve_progress + 1 ! if solve_progress >= 10000 then solve_progress = 0 end if solve_progress%10 ~= 0 then return true end if (msg_idx < sudokuGUI.sayings_n) and (os.time() - last_time > 4) then *************** *** 2964,2993 **** end local msg = string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[msg_idx], count, cell) ! solve_completed = progressDialog:Update(solve_progress/10, msg) return solve_completed end local s, count = sudoku.GeneratePuzzle() - progressDialog:Destroy() - if not s then return end ! local solve_progress = 0 ! local start_time = os.time() ! local solve_completed = true ! ! local progressDialog = wx.wxProgressDialog("wxLuaSudoku - Ensuring Uniqueness...", ! string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[1], 0, 0), ! 81 - sudokuGUI.difficulty, ! sudokuGUI.frame, ! wx.wxPD_AUTO_HIDE+wx.wxPD_CAN_ABORT+wx.wxPD_ELAPSED_TIME) ! ! msg_idx = 1 ! last_time = os.time() ! local diff_count = 0 ! local diff_i = 0 ! local diff_cell = 0 ! -- define handler function here so it'll work w/o gui --- 2990,3002 ---- end local msg = string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[msg_idx], count, cell) ! solve_completed = progressDialog:Update(solve_progress, msg) return solve_completed end local s, count = sudoku.GeneratePuzzle() progressDialog:Destroy() if not s then return end ! -- have complete puzzle, now remove cells -- define handler function here so it'll work w/o gui *************** *** 3010,3028 **** function sudoku.SolveBruteForceHook(guesses, cell) solve_progress = solve_progress + 1 ! if (solve_progress >= 1000) then solve_progress = 0 end if solve_progress%10 ~= 0 then return true end return sudoku.GeneratePuzzleDifficultyHook(diff_count, diff_i, diff_cell) end ! sudokuGUI.sudokuSolnTable = TableCopy(s) ! local s1 = sudoku.GeneratePuzzleDifficulty(sudokuGUI.difficulty, TableCopy(s)) ! progressDialog:Destroy() ! if s1 then ! sudokuGUI.sudokuTables_pos = 1 ! sudokuGUI.sudokuTables = {} ! table.insert(sudokuGUI.sudokuTables, s1) ! sudokuGUI.UpdateTable() -- resets possible and refreshes too end end --- 3019,3065 ---- function sudoku.SolveBruteForceHook(guesses, cell) solve_progress = solve_progress + 1 ! if (solve_progress >= 10000) then solve_progress = 0 end if solve_progress%10 ~= 0 then return true end return sudoku.GeneratePuzzleDifficultyHook(diff_count, diff_i, diff_cell) end + local ret = wx.wxOK ! while ret == wx.wxOK do ! msg_idx = 1 ! last_time = os.time() ! local diff_count = 0 ! local diff_i = 0 ! local diff_cell = 0 ! solve_progress = 0 ! start_time = os.time() ! solve_completed = true ! ! progressDialog = wx.wxProgressDialog("wxLuaSudoku - Ensuring Uniqueness...", ! string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[1], 0, 0), ! 81 - sudokuGUI.difficulty + 1, ! sudokuGUI.frame, ! wx.wxPD_AUTO_HIDE+wx.wxPD_CAN_ABORT+wx.wxPD_ELAPSED_TIME) ! ! local s1 = sudoku.GeneratePuzzleDifficulty(sudokuGUI.difficulty, TableCopy(s)) ! progressDialog:Destroy() ! ! if s1 then ! sudokuGUI.sudokuSolnTable = TableCopy(s) ! ! sudokuGUI.sudokuTables_pos = 1 ! sudokuGUI.sudokuTables = {} ! table.insert(sudokuGUI.sudokuTables, s1) ! sudokuGUI.UpdateTable() -- resets possible and refreshes too ! break ! else ! ret = wx.wxMessageBox( ! "The puzzle was not fully generated.".. ! "Press Ok to continue generating or Cancel to quit.", ! "wxLuaSudoku - Unfinished generation", ! wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, ! sudokuGUI.frame ) ! end end end *************** *** 3052,3057 **** -- make them correct the puzzle local ret = wx.wxMessageBox( ! "The puzzle you've opened has invalid values or cannot be solved.\n".. ! "Press Ok to correct them using Create before continuing.", "wxLuaSudoku - Invalid puzzle", wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, --- 3089,3094 ---- -- make them correct the puzzle local ret = wx.wxMessageBox( ! "The puzzle you've opened has invalid values.\n".. ! "Press Ok to correct them using 'Create' before continuing.", "wxLuaSudoku - Invalid puzzle", wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, *************** *** 3062,3079 **** end elseif sudokuGUI.IsCheckedMenuItem(sudokuGUI.ID_SHOW_MISTAKES) then ! sudokuGUI.sudokuSolnTable = sudokuGUI.SolveBruteForce(sudokuGUI.sudokuTables[1]) ! if not sudokuGUI.sudokuSolnTable then ! -- try to make them correct the puzzle ! local ret = wx.wxMessageBox( ! "The initial puzzle you've opened cannot be or was not solved \n".. ! "and therefore mistakes cannot be marked.\n".. ! "If you press cancel then showing mistakes will be disabled.", ! "wxLuaSudoku - Invalid puzzle", ! wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, ! sudokuGUI.frame ) ! if ret == wx.wxCANCEL then ! sudokuGUI.CheckMenuItem(sudokuGUI.ID_SHOW_MISTAKES, false) end end --- 3099,3122 ---- end elseif sudokuGUI.IsCheckedMenuItem(sudokuGUI.ID_SHOW_MISTAKES) then ! local ret = wx.wxOK ! while ret == wx.wxOK do ! sudokuGUI.sudokuSolnTable = sudokuGUI.SolveBruteForce(sudokuGUI.sudokuTables[1]) ! if not sudokuGUI.sudokuSolnTable then ! -- try to make them correct the puzzle ! local ret = wx.wxMessageBox( ! "The initial puzzle you've opened cannot be or was not solved \n".. ! "and therefore mistakes cannot be marked.\n".. ! "If you press cancel then showing mistakes will be disabled.", ! "wxLuaSudoku - Invalid puzzle", ! wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, ! sudokuGUI.frame ) ! if ret == wx.wxCANCEL then ! sudokuGUI.CheckMenuItem(sudokuGUI.ID_SHOW_MISTAKES, false) ! break ! end ! else ! break end end *************** *** 3090,3094 **** function sudokuGUI.SaveAsPuzzle() ! local fileDialog = wx.wxFileDialog(sudokuGUI.frame, "Save file", sudokuGUI.filePath, sudokuGUI.fileName, "wxLuaSudoku files (*.sudoku)|*.sudoku|All files (*)|*", --- 3133,3137 ---- function sudokuGUI.SaveAsPuzzle() ! local fileDialog = wx.wxFileDialog(sudokuGUI.frame, "Save puzzle", sudokuGUI.filePath, sudokuGUI.fileName, "wxLuaSudoku files (*.sudoku)|*.sudoku|All files (*)|*", |