add keys 'a' and 's' for showing board options
This commit is contained in:
		
							parent
							
								
									98c245fb9e
								
							
						
					
					
						commit
						775398f456
					
				| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user