From: John L. <jr...@us...> - 2006-06-06 05:14:38
|
Update of /cvsroot/wxlua/wxLua/samples In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv8826/wxLua/samples Modified Files: wxluasudoku.wx.lua Added Files: coroutine.wx.lua Log Message: add coroutine.wx.lua sample and fix coroutines after breaking it from require code Index: wxluasudoku.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/wxluasudoku.wx.lua,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** wxluasudoku.wx.lua 5 Jun 2006 05:14:24 -0000 1.54 --- wxluasudoku.wx.lua 6 Jun 2006 05:14:32 -0000 1.55 *************** *** 1328,1332 **** -- solution method iterating from low to high numbers and then from high to low. -- This should always find if there are at least two solutions. ! -- returns nil if no solution or [soln table, # guesses] for forward iteration function sudoku.IsUniquePuzzle(sudokuTable) --- 1328,1333 ---- -- solution method iterating from low to high numbers and then from high to low. -- This should always find if there are at least two solutions. ! -- returns nil if no solution or [s1, s2] if at least two solutions, else ! -- just the single unique solution function sudoku.IsUniquePuzzle(sudokuTable) *************** *** 1337,1343 **** if not s2 then return nil end ! if not sudoku.IsSamePuzzle(s1, s2) then return nil end ! return s1, g1 end --- 1338,1344 ---- if not s2 then return nil end ! if not sudoku.IsSamePuzzle(s1, s2) then return s1, s2 end ! return s1 end *************** *** 1711,1715 **** solution.<br> <br> ! If you want to start with a completely empty grid using <i>File->New</i>. If on the other hand, you want to start with a completely filled grid of random values, use <i>File->Generate</i> with the number of cell values --- 1712,1716 ---- solution.<br> <br> ! If you want to start with a completely empty grid use <i>File->New</i>. If on the other hand, you want to start with a completely filled grid of random values, use <i>File->Generate</i> with the number of cell values *************** *** 2171,2175 **** sudokuTables_pos = 0, -- Current position in the tables ! sudokuSolnTable = nil, -- solution to the current puzzle possNakedTable = nil, --- 2172,2177 ---- sudokuTables_pos = 0, -- Current position in the tables ! sudokuSolnTable = nil, -- solution to the current puzzle ! nonunique_init_puzzle = nil, -- nil for don't know, for true/false once checked possNakedTable = nil, *************** *** 3434,3438 **** function sudokuGUI.VerifyUniquePuzzle(sudokuTable) ! local ret = wx.wxOK local solve_progress = 0 --- 3436,3453 ---- function sudokuGUI.VerifyUniquePuzzle(sudokuTable) ! ! sudokuTable = sudoku.CalcInvalidCells(sudokuTable) ! local invalid_count = TableCount(sudokuTable.invalid) ! ! if invalid_count > 0 then ! local ret = wx.wxMessageBox( ! string.format("The initial values of the puzzle are invalid.\n".. ! "There are %d cells with duplicate values.\n".. ! "Please select Create and fix them before trying to solve.\n", invalid_count), ! "wxLuaSudoku - Invalid puzzle", ! wx.wxOK + wx.wxICON_ERROR, ! sudokuGUI.frame ) ! return ! end local solve_progress = 0 *************** *** 3456,3459 **** --- 3471,3475 ---- end + local ret = wx.wxOK while ret == wx.wxOK do solve_progress = 0 *************** *** 3468,3483 **** wx.wxPD_AUTO_HIDE+wx.wxPD_CAN_ABORT+wx.wxPD_ELAPSED_TIME) ! local s1, g = sudoku.IsUniquePuzzle(sudokuTable) progressDialog:Destroy() ! if s1 then return s1 elseif solve_ok == false then ! ret = wx.wxMessageBox("The puzzle was not fully verified. ".. "Press 'Ok' to restart checking or 'Cancel' to quit.", "wxLuaSudoku - Unfinished check", wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, sudokuGUI.frame) ! else wx.wxMessageBox("The puzzle does not have a unique solution.\n".. "Use 'Create' to fix the problem, showing mistakes will be disabled.", --- 3484,3499 ---- wx.wxPD_AUTO_HIDE+wx.wxPD_CAN_ABORT+wx.wxPD_ELAPSED_TIME) ! local s1, s2 = sudoku.IsUniquePuzzle(sudokuTable) progressDialog:Destroy() ! if s1 and (s2 == nil) then return s1 elseif solve_ok == false then ! ret = wx.wxMessageBox("The puzzle was not fully verified and therefore may not have a unique solution or a solution at all.\n".. "Press 'Ok' to restart checking or 'Cancel' to quit.", "wxLuaSudoku - Unfinished check", wx.wxOK + wx.wxCANCEL + wx.wxICON_ERROR, sudokuGUI.frame) ! elseif s1 and s2 then wx.wxMessageBox("The puzzle does not have a unique solution.\n".. "Use 'Create' to fix the problem, showing mistakes will be disabled.", *************** *** 3488,3491 **** --- 3504,3516 ---- sudokuGUI.CheckMenuItem(sudokuGUI.ID_SHOW_MISTAKES, false) return nil + else + wx.wxMessageBox("The puzzle does not have a solution.\n".. + "Use 'Create' to fix the problem, showing mistakes will be disabled.", + "wxLuaSudoku - Unsolvable puzzle", + wx.wxOK + wx.wxICON_ERROR, + sudokuGUI.frame) + + sudokuGUI.CheckMenuItem(sudokuGUI.ID_SHOW_MISTAKES, false) + return nil end end *************** *** 3497,3501 **** function sudokuGUI.SolveScanning() local s = TableCopy(sudokuGUI.GetCurrentTable()) ! local invalid = not TableIsEmpty(s.invalid) if invalid then --- 3522,3526 ---- function sudokuGUI.SolveScanning() local s = TableCopy(sudokuGUI.GetCurrentTable()) ! --[[ local invalid = not TableIsEmpty(s.invalid) if invalid then *************** *** 3529,3533 **** end end ! local s, count, changed_cells = sudoku.SolveScan(s) local changed_count = 0 --- 3554,3558 ---- end end ! ]] local s, count, changed_cells = sudoku.SolveScan(s) local changed_count = 0 *************** *** 3555,3559 **** s = TableCopy(sudokuGUI.GetCurrentTable()) end ! local invalid = not TableIsEmpty(s.invalid) if invalid then --- 3580,3584 ---- s = TableCopy(sudokuGUI.GetCurrentTable()) end ! --[[ local invalid = not TableIsEmpty(s.invalid) if invalid then *************** *** 3586,3590 **** end end ! local progressDialog = wx.wxProgressDialog("wxLuaSudoku - Solving...", string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[1], 0, 0), --- 3611,3615 ---- end end ! ]] local progressDialog = wx.wxProgressDialog("wxLuaSudoku - Solving...", string.format("%s\nIteration # %d, current cell %d ", sudokuGUI.sayings[1], 0, 0), --- NEW FILE: coroutine.wx.lua --- -------------------------------------------------------------------------=--- -- Name: coroutine.wx.lua -- Purpose: Tests coroutines and wxIDLE_EVENTS -- Author: Leandro Motta Barros -- Created: 2006 -- Copyright: -- Licence: wxWidgets licence -------------------------------------------------------------------------=--- ------------------------------------------------------------------------------- -- ProgressWindow ------------------------------------------------------------------------------- ProgressWindow = { -- The 'wxDialog' being encapsulated by this class. dialog = nil, -- The label telling what's going on ("Processing thing 7/88...") label = nil, } -- The constructor. 'parent' is the parent window. 'workCoroutine' is the -- coroutine used to process whatever is desired. 'caption' is the caption to be -- used for this window. 'initialLabel' is the string that will be initially -- shown in the window. function ProgressWindow:new (parent, workCoroutine, caption, initialLabel) -- simple little sanity test to ensure that we can call binding functions local r = wx.wxRect(1,2,3,4) r:SetX(1) local o = { } setmetatable (o, self) self.__index = self -- Check parameters assert (type (workCoroutine) == "thread") assert (type (caption) == "string") assert (type (initialLabel) == "string") -- Create dialog o.dialog = wx.wxDialog(parent, wx.wxID_ANY, caption, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxDEFAULT_DIALOG_STYLE) o.label = wx.wxTextCtrl (o.dialog, wx.wxID_ANY, intialLabel, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTE_MULTILINE + wx.wxTE_READONLY) -- Handle idle events: run the coroutine's next "step" o.dialog:Connect (wx.wxEVT_IDLE, function (event) if coroutine.status (workCoroutine) ~= "dead" then local s, msg = coroutine.resume (workCoroutine) if not msg then o.dialog:Close() else o:setStatus (msg) end event:RequestMore() event:Skip() end end) -- Voilà return o end -- Sets the ProgressWindow's "status". For now, this means "change the text" -- being displayed in the window (usually something like "Doing this thing"). function ProgressWindow:setStatus (label) self.label:AppendText ("\n"..label) end ------------------------------------------------------------------------------- -- The main frame ------------------------------------------------------------------------------- local ID_THE_BUTTON = wx.wxID_HIGHEST + 100 frame = wx.wxFrame (wx.wxNull, wx.wxID_ANY, "wxLua Idle Events and Coroutines") -- ---------------------------------------------------------------------------- -- create a simple file menu local fileMenu = wx.wxMenu() fileMenu:Append(wx.wxID_EXIT, "E&xit", "Quit the program") -- create a simple help menu local helpMenu = wx.wxMenu() helpMenu:Append(wx.wxID_ABOUT, "&About", "About the wxLua Coroutine Sample") -- create a menu bar and append the file and help menus local menuBar = wx.wxMenuBar() menuBar:Append(fileMenu, "&File") menuBar:Append(helpMenu, "&Help") -- attach the menu bar into the frame frame:SetMenuBar(menuBar) -- connect the selection event of the exit menu item to an -- event handler that closes the window frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) frame:Close(true) end ) -- connect the selection event of the about menu item frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) wx.wxMessageBox('This is the "About" dialog of the Coroutine wxLua sample.', "About wxLua", wx.wxOK + wx.wxICON_INFORMATION, frame) end ) -- ---------------------------------------------------------------------------- panel = wx.wxPanel(frame, wx.wxID_ANY) button = wx.wxButton (panel, ID_THE_BUTTON, "Perform some long operation") frame:Connect (ID_THE_BUTTON, wx.wxEVT_COMMAND_BUTTON_CLICKED, function (event) local function workFunc() for i = 1, 10 do coroutine.yield ("Performing step "..tostring(i).."/10") wx.wxSleep(1) end end local workCoroutine = coroutine.create (workFunc) local wndProgress = ProgressWindow:new (frame, workCoroutine, "Performing some long operation", "Performing step 1/many") wndProgress.dialog:ShowModal (true) wndProgress.dialog:Destroy() end) -- Show the main frame frame:Show (true) |