--To Do: --fail / Win check --info --screen size is 212 by 64 --grid is 21 by 6 --Select stuff local exactSelectPosX = 0 local exactSelectPosY = 0 local selectX = 0 local selectY = 0 local lastSelectX = 0 local lastSelectY = 0 local selectBoxOn = false local selectBoxSwitchTime = 50 local LastSelectBoxSwitchTime = 0 -- info about each box local shown = {} local mine = {} --how many should be placed - it will be slightly random local estimatedMineCount = 20 --Mines actually created local totalMines = 0 --all boxes uncovered local uncovered = 0 --if the game is finished local finished = false --if the game needs to be finished in the run() function local needFinish = false --Current Game mode local mode = 'start Screen' --All things Time local GameStartTime local GameFinishTime --Time to wait before changing to Finish screen local WaitAfterFinishTime = 200 local GameTotalTime local BestTime local finishReason --values to check trim changes local startAilTrim local startThrTrim local function rect(x, y, width ,height) --Simplifies making a rectangle lcd.drawRectangle(x,y,width,height) end local function ChangeSelectPos() --makes sure that the select box is not left anywhere while a new one is created if selectBoxOn then --removes box rect(lastSelectX * 10 + 3, lastSelectY * 10 + 4 , 6, 6) end LastSelectBoxSwitchTime = getTime() rect(selectX * 10 + 3, selectY * 10 + 4 , 6, 6) selectBoxOn = true end local function checkbox(x,y) --checks if function should be called if shown[x][y] == false then if selectX == x and selectY == y then if selectBoxOn == true then rect(lastSelectX * 10 + 3, lastSelectY * 10 + 4 , 6, 6) selectBoxOn = false end end --makes sure that this can't be called again shown[x][y] = true --checks for a mine here if mine[x][y] == false then --counts how many are uncovers uncovered = uncovered + 1 --resets count of mines local minesAround = 0 for i = -1, 1 do for j = -1, 1 do --Gets total positions local totalX = x + i local totalY = y + j if totalX < 21 and totalX >-1 and totalY > -1 and totalY < 6 then --Checks to see if is mines or not if mine[totalX][totalY] == true then --counts how many there are around minesAround = minesAround + 1 end end end end --if there are bombs around if minesAround > 0 then --Writes number for how many bombs there are around lcd.drawText(x * 10 +4, y * 10 + 3, minesAround) -- makes sure that finish and the flood don't happen at the same time - causes CPU overload elseif not needFinish then for i = -1,1 do for j = -1,1 do --Gets the positions of X and Y local totalX = x + i local totalY = y + j -- checks that values exist if totalX < 21 and totalX >-1 and totalY > -1 and totalY < 6 then --shows non bomb areas checkbox(totalX,totalY) end end end end else --Starts finish, starts function in run needFinish = true --Marks mine lcd.drawText(x * 10 +4, y * 10 + 3, 'X',INVERS) end -- removes box outline rect(x * 10 +1, y * 10 + 2 , 10, 10) end end local function finish() GameFinishTime = getTime() mode = 'Wait for end' --runs through all of the blocks to show them for i=0, 20 do for j=0,5 do --Shows them checkbox(i,j) end end finished = true end local function gameSetup() mode = 'Play' --wipes screen lcd.clear() --RESET ALL VARIABLES THAT ARE NEEDED finished = false needFinish = false selectX = 0 selectY = 0 lastSelectX = 0 lastSelectY = 0 uncovered = 0 totalMines = 0 exactSelectPosX = 0 exactSelectPosY = 0 selectBoxOn = false -- makes grid for x = 0, 20 do for y = 0, 5 do rect(x * 10 +1 , 10 * y + 2,10,10) end end --chooses mine places for i = 0, 20 do mine[i] = {} for j = 0, 5 do --picks if it should be a mine based off the estimated value if math.random(126) < estimatedMineCount then mine[i][j] = true --counts mines created totalMines = totalMines + 1 else --Marks them as clear mine[i][j] = false end end end --marks all as hidden for i = 0, 20 do --creates 2d array shown[i] = {} for j = 0, 5 do shown[i][j] = false --hidden end end --sets begin values for trim startAilTrim = getValue('trim-ail') startThrTrim = getValue('trim-thr') --Sets start time GameStartTime = getTime() end local function LoadFinishScreen() mode = 'Finish Screen' lcd.clear() lcd.drawText(67,5,"Finished",DBLSIZE) if finishReason == 'Win' then lcd.drawText(10,30, "Total time: ".. (GameFinishTime - GameStartTime) / 100) else lcd.drawText(10,30, "Total time: ".. finishReason) end lcd.drawText(97,30, " Total mines: ".. totalMines) lcd.drawText(40,55, "Hold Enter to start again") end local function round(num) return tonumber(string.format("%." .. (0) .. "f", num)) end local function init() lcd.clear() mode = 'start Screen' end local function run(event) if mode == 'Play' then --gets new trim values and finds difference local leftRight = getValue("ail") / 10.24 local UpDown = getValue("ele") / 10.24 exactSelectPosX = exactSelectPosX + getValue("ail") / 1024 exactSelectPosY = exactSelectPosY - (getValue("ele") / 1024) if exactSelectPosX > 20 then exactSelectPosX = 20 end if exactSelectPosX < 0 then exactSelectPosX = 0 end if exactSelectPosY > 5 then exactSelectPosY = 5 end if exactSelectPosY < 0 then exactSelectPosY = 0 end selectX = round(exactSelectPosX) selectY = round(exactSelectPosY) if selectX ~= lastSelectX or selectY ~= lastSelectY then ChangeSelectPos() end lastSelectX = selectX lastSelectY = selectY startAilTrim = getValue('trim-ail') startThrTrim = getValue('trim-thr') --if switch is pressed if getValue('sh') > 50 or event == EVT_PAGE_BREAK then --checks box that is selected checkbox(selectX,selectY) end --Select Box if getTime() > LastSelectBoxSwitchTime + selectBoxSwitchTime then --changes if he box is shown or not rect(selectX * 10 + 3, selectY * 10 + 4 , 6, 6) --Changes the known state of the box selectBoxOn = not selectBoxOn --Updates the time LastSelectBoxSwitchTime = getTime() end --checks if all the non mines are uncovered if 126 - totalMines == uncovered then --makes sure its not called several times if finished == false then finish() finishReason = 'Win' end end --used because you cant call functions that are lower than the function that calls them if needFinish then --makes sure its not called several times if not finished then finish() finishReason = 'Fail' end end --calls the restart loop end if mode == 'start Screen' then lcd.clear() if event == EVT_MINUS_FIRST then estimatedMineCount = estimatedMineCount - 1 elseif event == EVT_PLUS_FIRST then estimatedMineCount = estimatedMineCount + 1 end lcd.drawText(48,5,"MINESWEEPER",DBLSIZE) lcd.drawText(10,30, "Estimated Mines: ".. estimatedMineCount) lcd.drawText(55,55, "Hold Enter to start") if event == EVT_ENTER_LONG then gameSetup() end end if mode == 'Wait for end' then if getTime() > GameFinishTime + WaitAfterFinishTime then LoadFinishScreen() end end if mode == 'Finish Screen' then if event == EVT_ENTER_LONG then mode = 'start Screen' elseif event == EVT_EXIT_BREAK then return 1 end end return 0 end --might call the functions return { init=init, run=run }