before ui / game logic separation
This commit is contained in:
		
							parent
							
								
									0624b43b82
								
							
						
					
					
						commit
						3abedc2ed8
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -46,3 +46,5 @@ luac.out | ||||
| 
 | ||||
| 
 | ||||
| # End of https://www.gitignore.io/api/lua | ||||
| 
 | ||||
| ui/fonts/ | ||||
|  | ||||
							
								
								
									
										25
									
								
								boards/easy/01.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								boards/easy/01.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | ||||
| return { | ||||
|     problem = { | ||||
|         {5, 3, 0, 0, 7, 0, 0, 0, 0}, | ||||
|         {6, 0, 0, 1, 9, 5, 0, 0, 0}, | ||||
|         {0, 9, 8, 0, 0, 0, 0, 6, 0}, | ||||
|         {8, 0, 0, 0, 6, 0, 0, 0, 3}, | ||||
|         {4, 0, 0, 8, 0, 3, 0, 0, 1}, | ||||
|         {7, 0, 0, 0, 2, 0, 0, 0, 6}, | ||||
|         {0, 6, 0, 0, 0, 0, 2, 8, 0}, | ||||
|         {0, 0, 0, 4, 1, 9, 0, 0, 5}, | ||||
|         {0, 0, 0, 0, 8, 0, 0, 7, 9}, | ||||
|     } | ||||
| 
 | ||||
|     solution = { | ||||
|         {5, 3, 4, 6, 7, 8, 9, 1, 2}, | ||||
|         {6, 7, 2, 1, 9, 5, 3, 4, 8}, | ||||
|         {1, 9, 8, 3, 4, 2, 5, 6, 7}, | ||||
|         {8, 5, 9, 7, 6, 1, 4, 2, 3}, | ||||
|         {4, 2, 6, 8, 5, 3, 7, 9, 1}, | ||||
|         {7, 1, 3, 9, 2, 4, 8, 5, 6}, | ||||
|         {9, 6, 1, 5, 3, 7, 2, 8, 4}, | ||||
|         {2, 8, 7, 4, 1, 9, 6, 3, 5}, | ||||
|         {3, 4, 5, 2, 8, 6, 1, 7, 9}, | ||||
|     } | ||||
| } | ||||
							
								
								
									
										19
									
								
								boards/easy/01.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								boards/easy/01.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | ||||
| 5 3 0 0 7 0 0 0 0 | ||||
| 6 0 0 1 9 5 0 0 0 | ||||
| 0 9 8 0 0 0 0 6 0 | ||||
| 8 0 0 0 6 0 0 0 3 | ||||
| 4 0 0 8 0 3 0 0 1 | ||||
| 7 0 0 0 2 0 0 0 6 | ||||
| 0 6 0 0 0 0 2 8 0 | ||||
| 0 0 0 4 1 9 0 0 5 | ||||
| 0 0 0 0 8 0 0 7 9 | ||||
| 
 | ||||
| 5 3 4 6 7 8 9 1 2 | ||||
| 6 7 2 1 9 5 3 4 8 | ||||
| 1 9 8 3 4 2 5 6 7 | ||||
| 8 5 9 7 6 1 4 2 3 | ||||
| 4 2 6 8 5 3 7 9 1 | ||||
| 7 1 3 9 2 4 8 5 6 | ||||
| 9 6 1 5 3 7 2 8 4 | ||||
| 2 8 7 4 1 9 6 3 5 | ||||
| 3 4 5 2 8 6 1 7 9 | ||||
							
								
								
									
										9
									
								
								boards/easy/02.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								boards/easy/02.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | ||||
| 4 0 0 0 0 2 3 0 6 | ||||
| 0 0 6 0 0 0 0 8 0 | ||||
| 0 9 2 1 0 0 0 0 0 | ||||
| 0 0 0 0 1 4 6 9 0 | ||||
| 8 0 0 2 0 7 0 0 1 | ||||
| 0 6 5 3 9 0 0 0 0 | ||||
| 0 0 0 0 0 9 8 4 0 | ||||
| 0 8 0 0 0 0 7 0 0 | ||||
| 9 0 7 8 0 0 0 0 3 | ||||
							
								
								
									
										16
									
								
								boards/veryhard/01.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								boards/veryhard/01.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| return { | ||||
|     problem = { | ||||
|         {8, 0, 0, 0, 5, 0, 0, 0, 7}, | ||||
|         {0, 6, 1, 0, 0, 0, 0, 0, 0}, | ||||
|         {3, 7, 0, 0, 0, 0, 0, 6, 8}, | ||||
|         {0, 0, 0, 6, 0, 0, 8, 0, 0}, | ||||
|         {0, 1, 0, 0, 9, 0, 0, 2, 0}, | ||||
|         {0, 3, 0, 2, 0, 0, 0, 9, 0}, | ||||
|         {0, 8, 0, 0, 0, 7, 0, 0, 0}, | ||||
|         {0, 0, 0, 0, 0, 0, 4, 0, 1}, | ||||
|         {0, 0, 5, 0, 0, 0, 9, 8, 3}, | ||||
|     } | ||||
| 
 | ||||
|     solution = { | ||||
|     } | ||||
| } | ||||
							
								
								
									
										9
									
								
								boards/veryhard/01.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								boards/veryhard/01.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | ||||
| 8 0 0 0 5 0 0 0 7 | ||||
| 0 6 1 0 0 0 0 0 0 | ||||
| 3 7 0 0 0 0 0 6 8 | ||||
| 0 0 0 6 0 0 8 0 0 | ||||
| 0 1 0 0 9 0 0 2 0 | ||||
| 0 3 0 2 0 0 0 9 0 | ||||
| 0 8 0 0 0 7 0 0 0 | ||||
| 0 0 0 0 0 0 4 0 1 | ||||
| 0 0 5 0 0 0 9 8 3 | ||||
							
								
								
									
										11
									
								
								main.lua
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								main.lua
									
									
									
									
									
								
							| @ -5,7 +5,7 @@ require "ui.init" | ||||
| require "ui.draw" | ||||
| require "ui.keyboard" | ||||
| require "ui.mouse" | ||||
| 
 | ||||
| require "sudoku.utils" | ||||
| 
 | ||||
| function love.load() | ||||
|     love.window.setTitle("Sudoku!") | ||||
| @ -13,21 +13,20 @@ function love.load() | ||||
|     love.mouse.setGrabbed(false) | ||||
|     love.keyboard.setKeyRepeat(true) | ||||
|     math.randomseed(os.time()) | ||||
|     initSmallNumbers() | ||||
|     initGrid() | ||||
|     messages[1] = "Game Info" | ||||
|     board[1] = loadBoard(board.fn) | ||||
|     board[2] = cloneBoard(board[1]) | ||||
|     initUI() | ||||
| end | ||||
| 
 | ||||
| function love.update(dt) | ||||
| end | ||||
| 
 | ||||
| function love.draw() | ||||
|     -- love.graphics.setBackgroundColor(colors.backGround) | ||||
|     setBackgroundColor(colors.backGround) | ||||
|     drawGrid() | ||||
|     drawSmallNumbers() | ||||
|     drawBigNumbers() | ||||
|     drawCursor() | ||||
|     drawMessages() | ||||
|     love.graphics.setFont(fonts.bigNum) | ||||
|     love.graphics.print("5", grid[5][5].x1+25, grid[5][5].y1+10) | ||||
| end | ||||
|  | ||||
| @ -1,38 +0,0 @@ | ||||
| board = {{}, {}} | ||||
| 
 | ||||
| board[1].problem = { | ||||
|     {5, 3, 0, 0, 7, 0, 0, 0, 0}, | ||||
|     {6, 0, 0, 1, 9, 5, 0, 0, 0}, | ||||
|     {0, 9, 8, 0, 0, 0, 0, 6, 0}, | ||||
|     {8, 0, 0, 0, 6, 0, 0, 0, 3}, | ||||
|     {4, 0, 0, 8, 0, 3, 0, 0, 1}, | ||||
|     {7, 0, 0, 0, 2, 0, 0, 0, 6}, | ||||
|     {0, 6, 0, 0, 0, 0, 2, 8, 0}, | ||||
|     {0, 0, 0, 4, 1, 9, 0, 0, 5}, | ||||
|     {0, 0, 0, 0, 8, 0, 0, 7, 9}, | ||||
| } | ||||
| 
 | ||||
| board[1].solution = { | ||||
|     {5, 3, 4, 6, 7, 8, 9, 1, 2}, | ||||
|     {6, 7, 2, 1, 9, 5, 3, 4, 8}, | ||||
|     {1, 9, 8, 3, 4, 2, 5, 6, 7}, | ||||
|     {8, 5, 9, 7, 6, 1, 4, 2, 3}, | ||||
|     {4, 2, 6, 8, 5, 3, 7, 9, 1}, | ||||
|     {7, 1, 3, 9, 2, 4, 8, 5, 6}, | ||||
|     {9, 6, 1, 5, 3, 7, 2, 8, 4}, | ||||
|     {2, 8, 7, 4, 1, 9, 6, 3, 5}, | ||||
|     {3, 4, 5, 2, 8, 6, 1, 7, 9}, | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| board[2].problem = { | ||||
|     {8, 0, 0, 0, 5, 0, 0, 0, 7}, | ||||
|     {0, 6, 1, 0, 0, 0, 0, 0, 0}, | ||||
|     {3, 7, 0, 0, 0, 0, 0, 6, 8}, | ||||
|     {0, 0, 0, 6, 0, 0, 8, 0, 0}, | ||||
|     {0, 1, 0, 0, 9, 0, 0, 2, 0}, | ||||
|     {0, 3, 0, 2, 0, 0, 0, 9, 0}, | ||||
|     {0, 8, 0, 0, 0, 7, 0, 0, 0}, | ||||
|     {0, 0, 0, 0, 0, 0, 4, 0, 1}, | ||||
|     {0, 0, 5, 0, 0, 0, 9, 8, 3}, | ||||
| } | ||||
| @ -26,3 +26,24 @@ function cloneBoard(b) | ||||
|     return c | ||||
| end | ||||
| 
 | ||||
| function loadBoard(fn) | ||||
|     local problem = {} | ||||
|     local solution = {} | ||||
|     local board = problem | ||||
|     local r = 0 | ||||
|     for line in io.lines(fn) do | ||||
|         if line:gsub("%s+", "") == "" then | ||||
|             board = solution | ||||
|             r = 0 | ||||
|         else | ||||
|             r = r + 1 | ||||
|             local c = 0 | ||||
|             board[r] = {} | ||||
|             for item in line:gmatch("%w+") do | ||||
|                 c = c + 1 | ||||
|                 board[r][c] = tonumber(item) | ||||
|             end | ||||
|         end | ||||
|     end | ||||
|     return problem, solution | ||||
| end | ||||
|  | ||||
| @ -8,17 +8,24 @@ cell.height = 90 | ||||
| cell.mx = 10             -- margin x | ||||
| cell.my = 5              -- margin y | ||||
| 
 | ||||
| fonts.bigNum = love.graphics.newFont(64) | ||||
| -- fonts.bigNumbersOriginal = love.graphics.newFont("ui/fonts/Helvetica.ttf", 64) | ||||
| fonts.bigNumbersOriginal = love.graphics.newFont(64) | ||||
| fonts.bigNumbersPlayer = love.graphics.newFont("ui/fonts/Armadillo.ttf", 64) | ||||
| fonts.smallNum = love.graphics.newFont(24) | ||||
| fonts.info = love.graphics.newFont(24) | ||||
| 
 | ||||
| colors.smallNumbersDisabled = "EEEEEE" | ||||
| colors.smallNumbersEnabled = "0000FF" | ||||
| colors.boardCursor = "0000FF" | ||||
| colors.smallNumbersEnabled = "00FF00" | ||||
| colors.bigNumbersOriginal = "000000" | ||||
| colors.bigNumbersPlayer = "0000FF" | ||||
| colors.cursorActiveSmall = "00FF00" | ||||
| colors.cursorActiveBig = "0000FF" | ||||
| colors.cursorDisabled = "FF0000" | ||||
| colors.backGround = "FFFFFF" | ||||
| colors.messagesText = "000000" | ||||
| colors.boardThinLines = "808080" | ||||
| colors.boardThikLines = "000000" | ||||
| 
 | ||||
| cursor.x = 1 | ||||
| cursor.y = 1 | ||||
| cursor.editMode = false | ||||
| 
 | ||||
| board.fn = "boards/veryhard/01.txt" | ||||
|  | ||||
							
								
								
									
										44
									
								
								ui/draw.lua
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								ui/draw.lua
									
									
									
									
									
								
							| @ -21,23 +21,51 @@ function drawSmallNumbers() | ||||
|     for x = 1, 9 do | ||||
|         for y = 1, 9 do | ||||
|             for i = 1, 9 do | ||||
|                 local x1 = smallNumbers[x][y][i].x | ||||
|                 local y1 = smallNumbers[x][y][i].y | ||||
|                 if smallNumbersVal[x][y][i] then | ||||
|                     setColor(colors.smallNumbersEnabled) | ||||
|                 else | ||||
|                     setColor(colors.smallNumbersDisabled) | ||||
|                 if board[#board][y][x] == 0 then | ||||
|                     local x1 = smallNumbers[x][y][i].x | ||||
|                     local y1 = smallNumbers[x][y][i].y | ||||
|                     if smallNumbersVal[x][y][i] then | ||||
|                         setColor(colors.smallNumbersEnabled) | ||||
|                     else | ||||
|                         setColor(colors.smallNumbersDisabled) | ||||
|                     end | ||||
|                     love.graphics.print(tostring(i), x1, y1) | ||||
|                 end | ||||
|                 love.graphics.print(tostring(i), x1, y1) | ||||
|             end | ||||
|         end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function drawBigNumbers() | ||||
|     for x = 1, 9 do | ||||
|         for y = 1, 9 do | ||||
|             if board[1][x][y] ~= 0 then | ||||
|                 local c = tostring(board[1][x][y]) | ||||
|                 love.graphics.setFont(fonts.bigNumbersOriginal) | ||||
|                 setColor(colors.bigNumbersOriginal) | ||||
|                 love.graphics.print(c, grid[y][x].x1+25, grid[y][x].y1+10) | ||||
|             elseif board[2][x][y] ~= 0 then | ||||
|                 local c = tostring(board[2][x][y]) | ||||
|                 love.graphics.setFont(fonts.bigNumbersPlayer) | ||||
|                 setColor(colors.bigNumbersPlayer) | ||||
|                 love.graphics.print(c, grid[y][x].x1+33, grid[y][x].y1+15) | ||||
|             end | ||||
|         end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function drawCursor() | ||||
|     setColor(colors.boardCursor) | ||||
|     local x = grid[cursor.x][cursor.y].x1 | ||||
|     local y = grid[cursor.x][cursor.y].y1 | ||||
|     if board[1][cursor.y][cursor.x] == 0 then | ||||
|         if cursor.editMode then | ||||
|             setColor(colors.cursorActiveSmall) | ||||
|         else | ||||
|             setColor(colors.cursorActiveBig) | ||||
|         end | ||||
|     else | ||||
|         setColor(colors.cursorDisabled) | ||||
|     end | ||||
|     love.graphics.setLineWidth(3) | ||||
|     love.graphics.rectangle("line", x+3, y+3, cell.width-6, cell.height-6, 10, 10) | ||||
| end | ||||
|  | ||||
| @ -7,3 +7,5 @@ cursor = {} | ||||
| 
 | ||||
| smallNumbers = {} | ||||
| smallNumbersVal = {} | ||||
| 
 | ||||
| board = {} | ||||
|  | ||||
							
								
								
									
										17
									
								
								ui/init.lua
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								ui/init.lua
									
									
									
									
									
								
							| @ -21,7 +21,6 @@ function initSmallNumbers() | ||||
|             end | ||||
|         end | ||||
|     end | ||||
|     smallNumbersVal[8][5][4] = true | ||||
| end | ||||
| 
 | ||||
| function initGrid() | ||||
| @ -69,3 +68,19 @@ function initGrid() | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function findFirstEmptyCell() | ||||
|     for x = 1, 9 do | ||||
|         for y = 1, 9 do | ||||
|             if board[1][y][x] == 0 then return y, x end | ||||
|         end | ||||
|     end | ||||
|     return -1, -1 | ||||
| end | ||||
| 
 | ||||
| function initUI() | ||||
|     initSmallNumbers() | ||||
|     initGrid() | ||||
|     cursor.x, cursor.y = findFirstEmptyCell() | ||||
| end | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										259
									
								
								ui/keyboard.lua
									
									
									
									
									
								
							
							
						
						
									
										259
									
								
								ui/keyboard.lua
									
									
									
									
									
								
							| @ -1,40 +1,253 @@ | ||||
| function love.keypressed(key) | ||||
| local function iterBoardLine(start, step) | ||||
|     local step = step or 1 | ||||
|     local i = start | ||||
|     local n = 0 | ||||
|     return function() | ||||
|         i = i + step | ||||
|         n = n + 1 | ||||
|         if n > 9 then return end | ||||
|         if i > 9 then i = 1 end | ||||
|         if i < 1 then i = 9 end | ||||
|         return i | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function love.keypressed2(key, scancode) | ||||
|     if key == "escape" or key == "q" then | ||||
|         love.event.quit() | ||||
|     elseif key == "right" or key == "l" then | ||||
|         cursor.x = cursor.x + 1 | ||||
|         if cursor.x > 9 then cursor.x = 1 end | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             for x in iterBoardLine(cursor.x) do | ||||
|                 if board[1][cursor.y][x] == 0 then | ||||
|                     cursor.x = x | ||||
|                     break | ||||
|                 end | ||||
|             end | ||||
|         else | ||||
|             cursor.x = cursor.x + 1 | ||||
|             if cursor.x > 9 then cursor.x = 1 end | ||||
|         end | ||||
|     elseif key == "left" or key == "h" then | ||||
|         cursor.x = cursor.x - 1 | ||||
|         if cursor.x < 1 then cursor.x = 9 end | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             for x in iterBoardLine(cursor.x, -1) do | ||||
|                 if board[1][cursor.y][x] == 0 then | ||||
|                     cursor.x = x | ||||
|                     break | ||||
|                 end | ||||
|             end | ||||
|         else | ||||
|             cursor.x = cursor.x - 1 | ||||
|             if cursor.x < 1 then cursor.x = 9 end | ||||
|         end | ||||
|     elseif key == "down" or key == "j" then | ||||
|         cursor.y = cursor.y + 1 | ||||
|         if cursor.y > 9 then cursor.y = 1 end | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             for y in iterBoardLine(cursor.y) do | ||||
|                 if board[1][y][cursor.x] == 0 then | ||||
|                     cursor.y = y | ||||
|                     break | ||||
|                 end | ||||
|             end | ||||
|         else | ||||
|             cursor.y = cursor.y + 1 | ||||
|             if cursor.y > 9 then cursor.y = 1 end | ||||
|         end | ||||
|     elseif key == "up" or key == "k" then | ||||
|         cursor.y = cursor.y - 1 | ||||
|         if cursor.y < 1 then cursor.y = 9 end | ||||
|     -- elseif key == "n" then | ||||
|     --     cursor.x = cursor.x - 1 | ||||
|     --     if cursor.x < 1 then cursor.x = 9 end | ||||
|     --     cursor.y = cursor.y + 1 | ||||
|     --     if cursor.y > 9 then cursor.y = 1 end | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             for y in iterBoardLine(cursor.y, -1) do | ||||
|                 if board[1][y][cursor.x] == 0 then | ||||
|                     cursor.y = y | ||||
|                     break | ||||
|                 end | ||||
|             end | ||||
|         else | ||||
|             cursor.y = cursor.y - 1 | ||||
|             if cursor.y < 1 then cursor.y = 9 end | ||||
|         end | ||||
|     elseif key == "1" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][1] = not smallNumbersVal[cursor.x][cursor.y][1] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 1 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][1] = not smallNumbersVal[cursor.x][cursor.y][1] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "2" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][2] = not smallNumbersVal[cursor.x][cursor.y][2] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 2 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][2] = not smallNumbersVal[cursor.x][cursor.y][2] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "3" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][3] = not smallNumbersVal[cursor.x][cursor.y][3] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 3 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][3] = not smallNumbersVal[cursor.x][cursor.y][3] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "4" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][4] = not smallNumbersVal[cursor.x][cursor.y][4] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 4 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][4] = not smallNumbersVal[cursor.x][cursor.y][4] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "5" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][5] = not smallNumbersVal[cursor.x][cursor.y][5] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 5 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][5] = not smallNumbersVal[cursor.x][cursor.y][5] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "6" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][6] = not smallNumbersVal[cursor.x][cursor.y][6] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 6 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][6] = not smallNumbersVal[cursor.x][cursor.y][6] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "7" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][7] = not smallNumbersVal[cursor.x][cursor.y][7] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 7 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][7] = not smallNumbersVal[cursor.x][cursor.y][7] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "8" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][8] = not smallNumbersVal[cursor.x][cursor.y][8] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 8 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][8] = not smallNumbersVal[cursor.x][cursor.y][8] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "9" then | ||||
|         smallNumbersVal[cursor.x][cursor.y][9] = not smallNumbersVal[cursor.x][cursor.y][9] | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 9 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 smallNumbersVal[cursor.x][cursor.y][9] = not smallNumbersVal[cursor.x][cursor.y][9] | ||||
|             end | ||||
|         end | ||||
|     elseif key == "delete" or key == "0" then | ||||
|         if love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') then | ||||
|             board[#board][cursor.y][cursor.x] = 0 | ||||
|         elseif board[#board][cursor.y][cursor.x] ~= 0 then | ||||
|             board[#board][cursor.y][cursor.x] = 0 | ||||
|         else | ||||
|             if board[1][cursor.y][cursor.x] == 0 then | ||||
|                 for i = 1, 9 do | ||||
|                     smallNumbersVal[cursor.x][cursor.y][i] = false | ||||
|                 end | ||||
|             end | ||||
|         end | ||||
|     elseif key == "return" or key == "tab" then | ||||
|         cursor.isSmall = not cursor.isSmall | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function handleRight(shift) | ||||
|     if shift then | ||||
|         for x in iterBoardLine(cursor.x) do | ||||
|             if board[1][cursor.y][x] == 0 then | ||||
|                 cursor.x = x | ||||
|                 break | ||||
|             end | ||||
|         end | ||||
|     else | ||||
|         cursor.x = cursor.x + 1 | ||||
|         if cursor.x > 9 then cursor.x = 1 end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function handleLeft(shift) | ||||
|     if shift then | ||||
|         for x in iterBoardLine(cursor.x, -1) do | ||||
|             if board[1][cursor.y][x] == 0 then | ||||
|                 cursor.x = x | ||||
|                 break | ||||
|             end | ||||
|         end | ||||
|     else | ||||
|         cursor.x = cursor.x - 1 | ||||
|         if cursor.x < 1 then cursor.x = 9 end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function handleDown(shift) | ||||
|     if shift then | ||||
|         for y in iterBoardLine(cursor.y) do | ||||
|             if board[1][y][cursor.x] == 0 then | ||||
|                 cursor.y = y | ||||
|                 break | ||||
|             end | ||||
|         end | ||||
|     else | ||||
|         cursor.y = cursor.y + 1 | ||||
|         if cursor.y > 9 then cursor.y = 1 end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function handleUp(shift) | ||||
|     if shift then | ||||
|         for y in iterBoardLine(cursor.y, -1) do | ||||
|             if board[1][y][cursor.x] == 0 then | ||||
|                 cursor.y = y | ||||
|                 break | ||||
|             end | ||||
|         end | ||||
|     else | ||||
|         cursor.y = cursor.y - 1 | ||||
|         if cursor.y < 1 then cursor.y = 9 end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
| function love.keypressed(key, scancode) | ||||
|     local shift = love.keyboard.isDown('rshift') or love.keyboard.isDown('lshift') | ||||
|     if key == "escape" or key == "q" then | ||||
|         love.event.quit() | ||||
|     elseif key == "right" or key == "l" then | ||||
|         handleRight(shift) | ||||
|     elseif key == "left" or key == "h" then | ||||
|         handleLeft(shift) | ||||
|     elseif key == "down" or key == "j" then | ||||
|         handleDown(shift) | ||||
|     elseif key == "up" or key == "k" then | ||||
|         handleUp(shift) | ||||
|     elseif board[1][cursor.y][cursor.x] == 0 then | ||||
|         local n = tonumber(key) or -1 | ||||
|         if n > 0 and n < 10 then | ||||
|             if shift or cursor.editMode then | ||||
|                 board[#board][cursor.y][cursor.x] = 0 | ||||
|                 cursor.editMode = true | ||||
|                 smallNumbersVal[cursor.x][cursor.y][n] = not smallNumbersVal[cursor.x][cursor.y][n] | ||||
|             else | ||||
|                 -- for i = 1, 9 do | ||||
|                 --     smallNumbersVal[cursor.x][cursor.y][i] = false | ||||
|                 -- end | ||||
|                 board[#board][cursor.y][cursor.x] = n | ||||
|             end | ||||
|         elseif key == "delete" or key == "0" then | ||||
|             if board[#board][cursor.y][cursor.x] ~= 0 then | ||||
|                 board[#board][cursor.y][cursor.x] = 0 | ||||
|             else | ||||
|                 if board[1][cursor.y][cursor.x] == 0 then | ||||
|                     for i = 1, 9 do | ||||
|                         smallNumbersVal[cursor.x][cursor.y][i] = false | ||||
|                     end | ||||
|                 end | ||||
|             end | ||||
|         elseif key == "tab" then | ||||
|             if cursor.editMode == false and board[#board][cursor.y][cursor.x] ~= 0 then | ||||
|                 board[#board][cursor.y][cursor.x] = 0 | ||||
|             end | ||||
|             cursor.editMode = not cursor.editMode | ||||
|         end | ||||
|     end | ||||
| end | ||||
|  | ||||
							
								
								
									
										23
									
								
								ui/mouse.lua
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								ui/mouse.lua
									
									
									
									
									
								
							| @ -1,5 +1,6 @@ | ||||
| function love.mousepressed(x, y, button, istouch) | ||||
|     local inBoard = false | ||||
|     local canChange = false | ||||
|     local dx = math.floor(cell.width / 3) | ||||
|     local dy = math.floor(cell.height / 3) | ||||
|     local cx = 0 | ||||
| @ -15,15 +16,23 @@ function love.mousepressed(x, y, button, istouch) | ||||
|         my = math.floor((y - grid[cx][cy].y1) / dy) + 1 | ||||
|         n = mx + 3 * (my - 1) | ||||
|     end | ||||
|     if button == 1 then | ||||
|         if cursor.x == cx and cursor.y == cy then | ||||
|     if inBoard then | ||||
|         if board[1][cy][cx] == 0 then canChange = true end | ||||
|     end | ||||
|     if inBoard and canChange then | ||||
|         if button == 1 then | ||||
|             cursor.editMode = false | ||||
|             if cursor.x == cx and cursor.y == cy then | ||||
|                 smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] | ||||
|             else | ||||
|                 cursor.x = cx | ||||
|                 cursor.y = cy | ||||
|             end | ||||
|         elseif button == 2 then | ||||
|             cursor.editMode = true | ||||
|             board[#board][cursor.y][cursor.x] = 0 | ||||
|             smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] | ||||
|         else | ||||
|             cursor.x = cx | ||||
|             cursor.y = cy | ||||
|         end | ||||
|     elseif button == 2 then | ||||
|         smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] | ||||
|     end | ||||
| end | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user