----------------------------- -- Levak ©2011 -------------- -- http://levak.free.fr/ ---- -- levak92@gmail.com -------- ----------------------------- ------ Sudoku management Sudoku = class(Screen) function Sudoku:paint(gc) PaintGrid(gc, selectedCase.X + (selectedCase.Y - 1) * 9) PaintTimer(gc) gc:setFont("sansserif", "r", fsmall) gc:drawString("Grid "..tostring(GridSeed), x, y, "bottom") if win then gc:setFont("sansserif", "b", fnormal) gc:drawString("You won !", platform.window:width() - 7.5*normal, 6.5*normal, "middle") end -- paint anotation zone if (not paused) and timer.getMilliSecCounter() - oldTimer >= 1000 then oldTimer = timer.getMilliSecCounter() timer.start(1) end if displayIfGridOk then local str = "" if SudokuSolver.new(DisplayGrid):isValid(false) then str = "Grid could be OK" else str = "Grid contains an error !" end gc:setFont("sansserif", "r", fnormal) gc:drawString(str, x, y + iter * 9, "top") end if paused and (not win) then local str = "Paused" gc:setFont("sansserif", "b", xxlarge) gc:setColorRGB(255, 255, 255) gc:setAlpha(127) gc:fillRect(0, 0, platform.window:width(), platform.window:height()) gc:setAlpha(255) gc:fillRect((platform.window:width() - gc:getStringWidth(str) - 10)/2, (platform.window:height() - gc:getStringHeight(str) - 10)/2, gc:getStringWidth(str) + 10, gc:getStringHeight(str) + 10) gc:setColorRGB(0, 0, 0) gc:drawRect((platform.window:width() - gc:getStringWidth(str) - 10)/2, (platform.window:height() - gc:getStringHeight(str) - 10)/2, gc:getStringWidth(str) + 10, gc:getStringHeight(str) + 10) gc:drawString( str, (platform.window:width() - gc:getStringWidth(str))/2, platform.window:height()/2, "middle") end end function Sudoku:charIn(ch) if OriginalGrid[selectedCase.X + (selectedCase.Y - 1) * 9] == 0 then if ch >= "0" and ch <= "9" then paused = false win = false if ch == "0" then -- erase Case PlayGrid[selectedCase.X + (selectedCase.Y - 1) * 9] = 0 else -- add number to case PlayGrid[selectedCase.X + (selectedCase.Y - 1) * 9] = tonumber(ch) if not table.contains(PlayGrid, 0) then -- on a rempli la grille if SudokuSolver.new(PlayGrid):isValid(true) then paused = true win = true end end end end end if ch == "s" then if DisplayGrid == PlayGrid then DisplayGrid = SolutionGrid else DisplayGrid = PlayGrid end elseif ch == "p" then paused = not paused elseif ch == "c" then displayIfGridOk = not displayIfGridOk end platform.window:invalidate() end function Sudoku:contextMenu() initGUI() PushScreen(ContextMenu) end function Sudoku:arrowKey(key) if key == "up" then selectedCase.Y = selectedCase.Y - 1 if selectedCase.Y < 1 then selectedCase.Y = 9 end elseif key == "down" then selectedCase.Y = selectedCase.Y + 1 if selectedCase.Y > 9 then selectedCase.Y = 1 end end if key == "left" then selectedCase.X = selectedCase.X - 1 if selectedCase.X < 1 then selectedCase.X = 9 end elseif key == "right" then selectedCase.X = selectedCase.X + 1 if selectedCase.X > 9 then selectedCase.X = 1 end end platform.window:invalidate() end function Sudoku:enterKey() -- add anotation platform.window:invalidate() end function Sudoku:help() -- display Help PushScreen(Help) end function Sudoku:mouseDown(mx, my) -- calculate mouse position and determine selectedCase.X and selectedCase.Y if mx >= x and my >= y and mx <= x + dim * 2 and my <= y + dim * 2 then local nX = math.floor((mx - x) / iter) + 1 local nY = math.floor((my - y) / iter) + 1 if OriginalGrid[nX + (nY-1) * 9] == 0 then selectedCase = {X = nX, Y = nY} platform.window:invalidate() end end end -- non used events function Sudoku:escapeKey() end function Sudoku:mouseUp() end PushScreen(Sudoku())