logic separation done

This commit is contained in:
Reza Behzadan 2020-05-21 21:38:24 +04:30
parent af42e5e74e
commit 82120ea3db
6 changed files with 32 additions and 25 deletions

View File

@ -16,6 +16,9 @@ function love.load()
messages[1] = "Game Info" messages[1] = "Game Info"
board[1] = loadBoard(board.fn) board[1] = loadBoard(board.fn)
board[2] = cloneBoard(board[1]) board[2] = cloneBoard(board[1])
uiBoard[1] = cloneBoard(board[1])
uiBoard[2] = cloneBoard(board[1])
uiBoard[3] = cloneBoard(board[1])
initUI() initUI()
end end

View File

@ -21,7 +21,7 @@ function drawSmallNumbers()
for x = 1, 9 do for x = 1, 9 do
for y = 1, 9 do for y = 1, 9 do
for i = 1, 9 do for i = 1, 9 do
if board[#board][y][x] == 0 then if uiBoard[2][y][x] == 0 then
local x1 = smallNumbers[x][y][i].x local x1 = smallNumbers[x][y][i].x
local y1 = smallNumbers[x][y][i].y local y1 = smallNumbers[x][y][i].y
if smallNumbersVal[x][y][i] then if smallNumbersVal[x][y][i] then
@ -39,13 +39,13 @@ end
function drawBigNumbers() function drawBigNumbers()
for x = 1, 9 do for x = 1, 9 do
for y = 1, 9 do for y = 1, 9 do
if board[1][x][y] ~= 0 then if uiBoard[1][x][y] ~= 0 then
local c = tostring(board[1][x][y]) local c = tostring(uiBoard[1][x][y])
love.graphics.setFont(fonts.bigNumbersOriginal) love.graphics.setFont(fonts.bigNumbersOriginal)
setColor(colors.bigNumbersOriginal) setColor(colors.bigNumbersOriginal)
love.graphics.print(c, grid[y][x].x1+25, grid[y][x].y1+10) love.graphics.print(c, grid[y][x].x1+25, grid[y][x].y1+10)
elseif board[2][x][y] ~= 0 then elseif uiBoard[2][x][y] ~= 0 then
local c = tostring(board[2][x][y]) local c = tostring(uiBoard[2][x][y])
love.graphics.setFont(fonts.bigNumbersPlayer) love.graphics.setFont(fonts.bigNumbersPlayer)
setColor(colors.bigNumbersPlayer) setColor(colors.bigNumbersPlayer)
love.graphics.print(c, grid[y][x].x1+33, grid[y][x].y1+15) love.graphics.print(c, grid[y][x].x1+33, grid[y][x].y1+15)
@ -57,7 +57,7 @@ end
function drawCursor() function drawCursor()
local x = grid[cursor.x][cursor.y].x1 local x = grid[cursor.x][cursor.y].x1
local y = grid[cursor.x][cursor.y].y1 local y = grid[cursor.x][cursor.y].y1
if board[1][cursor.y][cursor.x] == 0 then if uiBoard[1][cursor.y][cursor.x] == 0 then
if cursor.editMode then if cursor.editMode then
setColor(colors.cursorActiveSmall) setColor(colors.cursorActiveSmall)
else else

View File

@ -9,3 +9,4 @@ smallNumbers = {}
smallNumbersVal = {} smallNumbersVal = {}
board = {} board = {}
uiBoard = {}

View File

@ -71,7 +71,7 @@ end
function findFirstEmptyCell() function findFirstEmptyCell()
for x = 1, 9 do for x = 1, 9 do
for y = 1, 9 do for y = 1, 9 do
if board[1][y][x] == 0 then return y, x end if uiBoard[1][y][x] == 0 then return y, x end
end end
end end
return -1, -1 return -1, -1
@ -82,5 +82,3 @@ function initUI()
initGrid() initGrid()
cursor.x, cursor.y = findFirstEmptyCell() cursor.x, cursor.y = findFirstEmptyCell()
end end

View File

@ -15,7 +15,7 @@ end
function handleRight(shift) function handleRight(shift)
if shift then if shift then
for x in iterBoardLine(cursor.x) do for x in iterBoardLine(cursor.x) do
if board[1][cursor.y][x] == 0 then if uiBoard[1][cursor.y][x] == 0 then
cursor.x = x cursor.x = x
break break
end end
@ -29,7 +29,7 @@ end
function handleLeft(shift) function handleLeft(shift)
if shift then if shift then
for x in iterBoardLine(cursor.x, -1) do for x in iterBoardLine(cursor.x, -1) do
if board[1][cursor.y][x] == 0 then if uiBoard[1][cursor.y][x] == 0 then
cursor.x = x cursor.x = x
break break
end end
@ -43,7 +43,7 @@ end
function handleDown(shift) function handleDown(shift)
if shift then if shift then
for y in iterBoardLine(cursor.y) do for y in iterBoardLine(cursor.y) do
if board[1][y][cursor.x] == 0 then if uiBoard[1][y][cursor.x] == 0 then
cursor.y = y cursor.y = y
break break
end end
@ -57,7 +57,7 @@ end
function handleUp(shift) function handleUp(shift)
if shift then if shift then
for y in iterBoardLine(cursor.y, -1) do for y in iterBoardLine(cursor.y, -1) do
if board[1][y][cursor.x] == 0 then if uiBoard[1][y][cursor.x] == 0 then
cursor.y = y cursor.y = y
break break
end end
@ -80,34 +80,38 @@ function love.keypressed(key, scancode)
handleDown(shift) handleDown(shift)
elseif key == "up" or key == "k" then elseif key == "up" or key == "k" then
handleUp(shift) handleUp(shift)
elseif board[1][cursor.y][cursor.x] == 0 then elseif uiBoard[1][cursor.y][cursor.x] == 0 then
local n = tonumber(key) or -1 local n = tonumber(key) or -1
if n > 0 and n < 10 then if n > 0 and n < 10 then
if shift or cursor.editMode then if shift or cursor.editMode then
board[#board][cursor.y][cursor.x] = 0 uiBoard[2][cursor.y][cursor.x] = 0
cursor.editMode = true cursor.editMode = true
smallNumbersVal[cursor.x][cursor.y][n] = not smallNumbersVal[cursor.x][cursor.y][n] smallNumbersVal[cursor.x][cursor.y][n] = not smallNumbersVal[cursor.x][cursor.y][n]
else else
-- for i = 1, 9 do -- for i = 1, 9 do
-- smallNumbersVal[cursor.x][cursor.y][i] = false -- smallNumbersVal[cursor.x][cursor.y][i] = false
-- end -- end
board[#board][cursor.y][cursor.x] = n uiBoard[2][cursor.y][cursor.x] = n
end end
elseif key == "delete" or key == "0" then elseif key == "delete" or key == "0" then
if board[#board][cursor.y][cursor.x] ~= 0 then if uiBoard[2][cursor.y][cursor.x] ~= 0 then
board[#board][cursor.y][cursor.x] = 0 uiBoard[2][cursor.y][cursor.x] = 0
else else
if board[1][cursor.y][cursor.x] == 0 then if uiBoard[1][cursor.y][cursor.x] == 0 then
for i = 1, 9 do for i = 1, 9 do
smallNumbersVal[cursor.x][cursor.y][i] = false smallNumbersVal[cursor.x][cursor.y][i] = false
end end
end end
end end
elseif key == "tab" then elseif key == "tab" then
if cursor.editMode == false and board[#board][cursor.y][cursor.x] ~= 0 then if cursor.editMode == false then
board[#board][cursor.y][cursor.x] = 0 uiBoard[3][cursor.y][cursor.x] = uiBoard[2][cursor.y][cursor.x]
end uiBoard[2][cursor.y][cursor.x] = 0
cursor.editMode = not cursor.editMode cursor.editMode = true
else
uiBoard[2][cursor.y][cursor.x] = uiBoard[3][cursor.y][cursor.x]
cursor.editMode = false
end
end end
end end
end end

View File

@ -17,11 +17,12 @@ function love.mousepressed(x, y, button, istouch)
n = mx + 3 * (my - 1) n = mx + 3 * (my - 1)
end end
if inBoard then if inBoard then
if board[1][cy][cx] == 0 then canChange = true end if uiBoard[1][cy][cx] == 0 then canChange = true end
end end
if inBoard and canChange then if inBoard and canChange then
if button == 1 then if button == 1 then
cursor.editMode = false cursor.editMode = false
uiBoard[2][cursor.y][cursor.x] = uiBoard[3][cursor.y][cursor.x]
if cursor.x == cx and cursor.y == cy then if cursor.x == cx and cursor.y == cy then
smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n]
else else
@ -30,9 +31,9 @@ function love.mousepressed(x, y, button, istouch)
end end
elseif button == 2 then elseif button == 2 then
cursor.editMode = true cursor.editMode = true
board[#board][cursor.y][cursor.x] = 0 uiBoard[3][cursor.y][cursor.x] = uiBoard[2][cursor.y][cursor.x]
uiBoard[2][cursor.y][cursor.x] = 0
smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n]
end end
end end
end end