add undo
This commit is contained in:
		
							parent
							
								
									ba7db7a04e
								
							
						
					
					
						commit
						8e279b4800
					
				| @ -527,6 +527,15 @@ function sHelper(n) | |||||||
|     solve(board) |     solve(board) | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
|  | function checkBoardsEqual(b1, b2) | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         for j = 1, 9 do | ||||||
|  |             if b1[i][j] ~= b2[i][j] then return false end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     return true | ||||||
|  | end | ||||||
|  | 
 | ||||||
| -- b = loadBoard("b.txt")[1] | -- b = loadBoard("b.txt")[1] | ||||||
| -- applyFillSinglesRepeatedly(b) | -- applyFillSinglesRepeatedly(b) | ||||||
| -- s = buildSearchSpace(b) | -- s = buildSearchSpace(b) | ||||||
| @ -535,8 +544,11 @@ end | |||||||
| 
 | 
 | ||||||
| return { | return { | ||||||
|     loadBoard = loadBoard, |     loadBoard = loadBoard, | ||||||
|     calCellValidOptions = calCellValidOptions, |  | ||||||
|     cloneBoard = cloneBoard, |     cloneBoard = cloneBoard, | ||||||
|     checkBoard = checkBoard, |  | ||||||
|     createEmptyBoard = createEmptyBoard, |     createEmptyBoard = createEmptyBoard, | ||||||
|  |     calCellValidOptions = calCellValidOptions, | ||||||
|  |     checkBoard = checkBoard, | ||||||
|  |     applyFillSinglesRepeatedly = applyFillSinglesRepeatedly, | ||||||
|  |     showBoard = showBoard, | ||||||
|  |     checkBoardsEqual = checkBoardsEqual,  | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,47 @@ | |||||||
| SudokuCanvas = {} | SudokuCanvas = {} | ||||||
| SudokuCanvas.__index = SudokuCanvas | SudokuCanvas.__index = SudokuCanvas | ||||||
| 
 | 
 | ||||||
|  | function SudokuCanvas:pushHistory() | ||||||
|  |     if #self.history <= 1 then | ||||||
|  |         table.insert(self.history, sudoku.cloneBoard(self.board[2])) | ||||||
|  |         -- showHistory(self) | ||||||
|  |         -- print("pushHistory: ", #self.history) | ||||||
|  |     elseif not sudoku.checkBoardsEqual(self.board[2], self.history[#self.history]) then | ||||||
|  |         table.insert(self.history, sudoku.cloneBoard(self.board[2])) | ||||||
|  |         -- showHistory(self) | ||||||
|  |         -- print("pushHistory: ", #self.history) | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function showHistory(self) | ||||||
|  |     for i, v in ipairs(self.history) do | ||||||
|  |         print(string.format("-- [%03d] ----------", i)) | ||||||
|  |         sudoku.showBoard(v) | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function undo(self) | ||||||
|  |     if #self.history > 1 then | ||||||
|  |         showHistory(self) | ||||||
|  |         table.remove(self.history) | ||||||
|  |         self.board[2] = sudoku.cloneBoard(self.history[#self.history]) | ||||||
|  |         self.board[3] = sudoku.cloneBoard(self.board[2]) | ||||||
|  | 
 | ||||||
|  |         -- self.board[2] = sudoku.cloneBoard(self.history[#self.history]) | ||||||
|  |         -- self.history[#self.history] = nil | ||||||
|  | 
 | ||||||
|  |         self:checkBoard(true) | ||||||
|  |         print"-------------------------------------------" | ||||||
|  |         showHistory(self) | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function applyAllSingles(self) | ||||||
|  |     sudoku.applyFillSinglesRepeatedly(self.board[2]) | ||||||
|  |     self.board[3] = sudoku.cloneBoard(self.board[2]) | ||||||
|  |     self:checkBoard() | ||||||
|  | end | ||||||
|  | 
 | ||||||
| function showValidOptionsForCurrentCell(self, x, y) | function showValidOptionsForCurrentCell(self, x, y) | ||||||
|     local s = sudoku.calCellValidOptions(self.board[2], y, x) |     local s = sudoku.calCellValidOptions(self.board[2], y, x) | ||||||
|     for num = 1, 9 do |     for num = 1, 9 do | ||||||
| @ -211,6 +252,7 @@ function SudokuCanvas:new(x, y, size, options) | |||||||
|         colors = options.colors, |         colors = options.colors, | ||||||
|         enabled = true, |         enabled = true, | ||||||
|         focused = true, |         focused = true, | ||||||
|  |         history = {}, | ||||||
|     } |     } | ||||||
|     o.grid = buildGrid(x, y, cellSize) |     o.grid = buildGrid(x, y, cellSize) | ||||||
|     o.smallNumbers = calSmallNumbersCoordinates(o.grid) |     o.smallNumbers = calSmallNumbersCoordinates(o.grid) | ||||||
| @ -228,7 +270,8 @@ end | |||||||
| function SudokuCanvas:focus() | function SudokuCanvas:focus() | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| function SudokuCanvas:checkBoard() | function SudokuCanvas:checkBoard(hist) | ||||||
|  |     if not hist then self:pushHistory() end | ||||||
|     local ok, isComplete, conflictList = sudoku.checkBoard(self.board[2]) |     local ok, isComplete, conflictList = sudoku.checkBoard(self.board[2]) | ||||||
|     self.board[4] = sudoku.createEmptyBoard() |     self.board[4] = sudoku.createEmptyBoard() | ||||||
|     for _, c in ipairs(conflictList) do |     for _, c in ipairs(conflictList) do | ||||||
| @ -342,7 +385,7 @@ function SudokuCanvas:keypressed(key) | |||||||
|                     self.board[2][y][x] = num |                     self.board[2][y][x] = num | ||||||
|                     self.board[3][y][x] = num |                     self.board[3][y][x] = num | ||||||
|                 end |                 end | ||||||
|                 self:checkBoard() |                 -- self:checkBoard() | ||||||
|             else |             else | ||||||
|                 self.smallNumbers[x][y][num].enabled = not self.smallNumbers[x][y][num].enabled |                 self.smallNumbers[x][y][num].enabled = not self.smallNumbers[x][y][num].enabled | ||||||
|             end |             end | ||||||
| @ -382,9 +425,12 @@ function SudokuCanvas:keypressed(key) | |||||||
|             mode = "big" |             mode = "big" | ||||||
|         end |         end | ||||||
|     elseif key == "s" then |     elseif key == "s" then | ||||||
|         showValidOptionsForCurrentCell(self, x, y) |         -- showValidOptionsForCurrentCell(self, x, y) | ||||||
|     elseif key == "a" then |  | ||||||
|         showValidOptionsForAllCells(self) |         showValidOptionsForAllCells(self) | ||||||
|  |     elseif key == "a" then | ||||||
|  |         applyAllSingles(self) | ||||||
|  |     elseif key == "u" then | ||||||
|  |         undo(self) | ||||||
|     end |     end | ||||||
|     self.cursor.x = x |     self.cursor.x = x | ||||||
|     self.cursor.y = y |     self.cursor.y = y | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user