From 775398f4568cf76b35d4fdbf207861c7dc4aaf90 Mon Sep 17 00:00:00 2001 From: Reza Behzadan Date: Fri, 29 May 2020 10:19:18 +0430 Subject: [PATCH] add keys 'a' and 's' for showing board options --- sudoku/sudoku.lua | 37 +++++++++++++++++++++++++++++++++++++ ui/sudokucanvas.lua | 22 ++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/sudoku/sudoku.lua b/sudoku/sudoku.lua index e2077a6..053e584 100644 --- a/sudoku/sudoku.lua +++ b/sudoku/sudoku.lua @@ -78,6 +78,43 @@ function sudoku.checkBoard(board) return ok, isComplete, conflictList end +function sudoku.calCellValidOptions(board, x, y) + if board[x][y] ~= 0 then return {} end + local u = {} + local v = {} + for i = 1, 9 do + if board[x][i] ~= 0 then table.insert(u, board[x][i]) end + if board[i][y] ~= 0 then table.insert(u, board[i][y]) end + end + local dx = math.floor((x - 1) / 3) * 3 + local dy = math.floor((y - 1) / 3) * 3 + for i = 1, 3 do + for j = 1, 3 do + local a = board[i+dx][j+dy] + if a ~= 0 then table.insert(u, a) end + end + end + for i = 1, 9 do + local found = false + for _, elm in ipairs(u) do + if elm == i then found = true; break end + end + if not found then table.insert(v, i) end + end + return v +end + +function sudoku.buildSearchSpave(board) + local s = {} + for i = 1, 9 do + s[i] = {} + for j = 1, 9 do + s[i][j] = sudoku.calCellValidOptions(board, i, j) + end + end + return s +end + sudoku.cloneBoard = utils.cloneBoard sudoku.createEmptyBoard = utils.createEmptyBoard sudoku.loadBoard = utils.loadBoard diff --git a/ui/sudokucanvas.lua b/ui/sudokucanvas.lua index 26e5c33..db78c37 100644 --- a/ui/sudokucanvas.lua +++ b/ui/sudokucanvas.lua @@ -1,6 +1,24 @@ SudokuCanvas = {} SudokuCanvas.__index = SudokuCanvas +function showValidOptionsForCurrentCell(self, x, y) + local s = sudoku.calCellValidOptions(self.board[2], y, x) + for num = 1, 9 do + self.smallNumbers[x][y][num].enabled = false + end + for _, num in ipairs(s) do + self.smallNumbers[x][y][num].enabled = true + end +end + +function showValidOptionsForAllCells(self) + for x = 1, 9 do + for y = 1, 9 do + showValidOptionsForCurrentCell(self, x, y) + end + end +end + local function selectSmallNumbersFont(cellSize) local fntSize = 65 local margin = 0 @@ -363,6 +381,10 @@ function SudokuCanvas:keypressed(key) self.board[2][y][x] = self.board[3][y][x] mode = "big" end + elseif key == "s" then + showValidOptionsForCurrentCell(self, x, y) + elseif key == "a" then + showValidOptionsForAllCells(self) end self.cursor.x = x self.cursor.y = y