Author Topic: LuaZM chesstimer, need help, not finished  (Read 4484 times)

0 Members and 1 Guest are viewing this topic.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
LuaZM chesstimer, need help, not finished
« on: October 28, 2012, 07:22:54 pm »
OK, here's my chess timer:

Code: [Select]
print("Chess timer starting...")
--function variables
local drawRectFill = zmg.drawRectFill
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local floor = math.floor
local random = math.random
local time = zmg.time
local ticks = zmg.ticks

--screen vars
local SCREEN_WIDTH = 384
local SCREEN_HEIGHT = 216
local exit = 0

--game variables
local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47, Optn=68, Up=28, Down=37, Left=38, Right=27, EXE=31}
local chesstimer = {timerStart=60, add=0, turn="White", turnNum=1}
local color = {bg=makeColor("white"), fg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {white=chesstimer.timerStart, black=chesstimer.timerStart}
local start=0

--first poll and first start timer
keyDirectPoll()
local startTime = ticks()

--functions

local function timeElapsed()
return floor((ticks()-startTime)/100)
end

local function wait(timeToWait)
local start = timeElapsed()
while timeElapsed() - start < timeToWait do end
end

local function waitForKey(key)
while keyDirect(key) == 0 do
keyDirectPoll()
end
end

local function toggleTurn()
if chesstimer.turn == "White" then chesstimer.turn = "Black" chesstimer.turnNum = 2
else chesstimer.turn = "White" chesstimer.turnNum = 1
end
start = timeElapsed()
end

local function introScreen()
continue = 0
drawText(1, 1, "WAIT...", color.fg, color.bg)
fastCopy()

wait(3)

drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
fastCopy()

waitForKey(key.EXE)

intro = 0
end

local function chessTimerScreen()
start = timeElapsed()
while exit ~= 1 do
if keyDirect(key.Exit) > 0 then exit = 1
elseif keyDirect(key.F1) > 0 then toggleTurn()
end

--drawing
drawText(1, SCREEN_HEIGHT - 10, "Next Player", color.fg, color.bg)
drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
drawText(1, 10, "White: " .. timers.white, color.fg, color.bg)
drawText(1, 20, "Black: " .. timers.black, color.bg, color.fg)

--math
timeElapsedSinceTurn = timeElapsed - start
timers[chesstimer.turnNum] = chesstimer.timerStart - timeElapsedSinceTurn

keyDirectPoll()
end
exit=0
intro=1
end


--main loop
while exit~=1 do
clear()

--intro screen
if intro == 1 then fastCopy() introScreen() end

--wait screen
drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
drawText(1, SCREEN_HEIGHT - 10, "Next Player", color.fg, color.bg)

--wait
waitForKey(key.F1)

--chesstimer screen
chessTimerScreen()

--refresh screen
fastCopy()
--keypoll
keyDirectPoll()
end

print("done.")
print("")

For some reason it freezes at "Press [EXE] to continue". I've been debugging it for a half an hour with no results so maybe someone with a fresh brain can help me see where my problem is?



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: LuaZM chesstimer, need help, not finished
« Reply #1 on: October 29, 2012, 02:33:08 pm »
if you put code after "drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)", like another drawText, does it get executed ?
Also, is line 95 intentional ?

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: LuaZM chesstimer, need help, not finished
« Reply #2 on: October 29, 2012, 04:00:39 pm »
if you put code after "drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)", like another drawText, does it get executed ?
Also, is line 95 intentional ?
Actually, i forgot to update this thread but I got past that problem and now have another.

when a timer runs out of time the calc freezes instead of showing the out of time message:

Code: [Select]
print("Chess timer starting...")
--function variables
local drawRectFill = zmg.drawRectFill
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local floor = math.floor
local random = math.random
local time = zmg.time
local ticks = zmg.ticks

--screen vars
local SCREEN_WIDTH = 384
local SCREEN_HEIGHT = 216
local exit = 0

--game variables
local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47,
Optn=68, Up=28, Down=37, Left=38, Right=27, EXE=31}
local chesstimer = {timerStart=60, add=0, turn="White", turnNum=1}
local color = {bg=makeColor("white"), fg=makeColor("black"), dbfg=makeColor("red"), dbbg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {}

local start=0
local timeElapsedSinceTurn = 0

--first poll and first start timer
keyDirectPoll()
local startTime = ticks()

--functions

local function timeElapsed()
return floor((ticks()-startTime)/128)
end

local function convertTime(t, format)
min = floor(t/60)
sec = t-(min*60)
if format=="m" then return min
elseif format=="s" then return sec
end
end

local function wait(timeToWait)
local start = timeElapsed()
while timeElapsed() - start < timeToWait do end
end

local function waitForKey(key)
while keyDirect(key) == 0 do
keyDirectPoll()
end
end

local function toggleTurn()
fastCopy()
timers[chesstimer.turnNum + 2] = (timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn) + chesstimer.add
if chesstimer.turn == "White" then chesstimer.turn = "Black" chesstimer.turnNum = 2
else chesstimer.turn = "White" chesstimer.turnNum = 1
end
start = timeElapsed()
end

local function outOfTime(message)
clear()
drawText(floor(SCREEN_WIDTH/2),floor(SCREEN_HEIGHT/2),message,makeColor("black"), makeColor("red"))
fastCopy()
waitForKey(key.Exit)
end

local function introScreen()
clear()
continue = 0
drawText(1, 1, "WAIT...", color.fg, color.bg)
fastCopy()
min = 1

wait(1)



while continue==0 do
clear()
keyDirectPoll()
if keyDirect(key.Up)>0 and min<15 then
while keyDirect(key.Up) > 0 do keyDirectPoll() end
min = min+1
elseif keyDirect(key.Down)>0 and min>0 then
while keyDirect(key.Down) > 0 do keyDirectPoll() end
min = min-1
end
if keyDirect(key.EXE)>0 then continue = 1 end

drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
drawText(1, 20, "minutes: " .. min, color.fg, color.bg)

fastCopy()
end
chesstimer.timerStart = min*60
timers[1] = chesstimer.timerStart
timers[2] = chesstimer.timerStart
timers[3] = chesstimer.timerStart
timers[4] = chesstimer.timerStart
intro = 0
end

local function chessTimerScreen()
clear()
start = timeElapsed()
while exit ~= 1 do
clear()
if keyDirect(key.Exit) > 0 then exit = 1
elseif keyDirect(key.F1) > 0 then
while keyDirect(key.F1) > 0 do keyDirectPoll() end
toggleTurn()
end

--drawing
drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
drawText(1, 20, "White: " .. convertTime(timers[1],"m") .. ":" .. convertTime(timers[1],"s"), color.fg, color.bg)
drawText(1, 40, "Black: " .. convertTime(timers[2],"m") .. ":" .. convertTime(timers[2],"s"), color.bg, color.fg)

fastCopy()

--math
timeElapsedSinceTurn = timeElapsed() - start
timers[chesstimer.turnNum] = timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn
if timers[chesstimer.turnNum] < 0 then outOfTime(chesstimer.turn .. " ran out of time!") end

--[[-- debug
drawText(1, 70, "DEBUG: " .. timers[1], color.dbfg, color.dbbg)
--]]--

keyDirectPoll()
end
end


--main loop
while exit~=1 do
clear()

--intro screen
if intro == 1 then fastCopy() introScreen() end

--wait screen
clear()
drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
fastCopy()

--wait
waitForKey(key.F1)

--chesstimer screen
chessTimerScreen()

--refresh screen
fastCopy()
--keypoll
keyDirectPoll()
end

print("done.")
print("")



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: LuaZM chesstimer, need help, not finished
« Reply #3 on: October 29, 2012, 04:27:12 pm »
If you call just outOfTime(), without checking, does it crash?

Also, I noticed you have

--[[-- and --]]--
If you simply want to uncomment a block of code, just change the --[[ into ---[[. That's all that's required, and you can comment it as easy later :)

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: LuaZM chesstimer, need help, not finished
« Reply #4 on: October 29, 2012, 05:05:46 pm »
...
Also, I noticed you have

--[[-- and --]]--
If you simply want to uncomment a block of code, just change the --[[ into ---[[. That's all that's required, and you can comment it as easy later :)


What do you mean? I want that section of code commented out.

EDIT:

For some reason this works...

Code: [Select]
print("Chess timer starting...")
--function variables
local drawRectFill = zmg.drawRectFill
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local floor = math.floor
local random = math.random
local time = zmg.time
local ticks = zmg.ticks

--screen vars
local SCREEN_WIDTH = 384
local SCREEN_HEIGHT = 216
local exit = 0

--game variables
local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47,
Optn=68, Up=28, Down=37, Left=38, Right=27, EXE=31}
local chesstimer = {timerStart=60, add=0, turn="White", turnNum=1}
local color = {bg=makeColor("white"), fg=makeColor("black"), dbfg=makeColor("red"), dbbg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {}

local start=0
local timeElapsedSinceTurn = 0

--first poll and first start timer
keyDirectPoll()
local startTime = ticks()

--functions

local function timeElapsed()
return floor((ticks()-startTime)/128)
end

local function convertTime(t, format)
min = floor(t/60)
sec = t-(min*60)
if format=="m" then return min
elseif format=="s" then return sec
end
end

local function wait(timeToWait)
local start = timeElapsed()
while timeElapsed() - start < timeToWait do end
end

local function waitForKey(key)
while keyDirect(key) == 0 do
keyDirectPoll()
end
end

local function toggleTurn()
fastCopy()
timers[chesstimer.turnNum + 2] = (timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn) + chesstimer.add
if chesstimer.turn == "White" then chesstimer.turn = "Black" chesstimer.turnNum = 2
else chesstimer.turn = "White" chesstimer.turnNum = 1
end
start = timeElapsed()
end
--[[--
local function outOfTime(message)
clear()
drawText(floor(SCREEN_WIDTH/2),floor(SCREEN_HEIGHT/2),message,makeColor("black"), makeColor("red"))
fastCopy()
waitForKey(key.Exit)
end
--]]--

local function introScreen()
clear()
continue = 0
drawText(1, 1, "WAIT...", color.fg, color.bg)
fastCopy()
min = 1

wait(1)



while continue==0 do
clear()
keyDirectPoll()
if keyDirect(key.Up)>0 and min<15 then
while keyDirect(key.Up) > 0 do keyDirectPoll() end
min = min+1
elseif keyDirect(key.Down)>0 and min>0 then
while keyDirect(key.Down) > 0 do keyDirectPoll() end
min = min-1
end
if keyDirect(key.EXE)>0 then continue = 1 end

drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
drawText(1, 20, "minutes: " .. min, color.fg, color.bg)

fastCopy()
end
chesstimer.timerStart = min*60
timers[1] = chesstimer.timerStart
timers[2] = chesstimer.timerStart
timers[3] = chesstimer.timerStart
timers[4] = chesstimer.timerStart
intro = 0
end

local function chessTimerScreen()
clear()
start = timeElapsed()
while exit ~= 1 do
clear()
if keyDirect(key.Exit) > 0 then exit = 1
elseif keyDirect(key.F1) > 0 then
while keyDirect(key.F1) > 0 do keyDirectPoll() end
toggleTurn()
end

--drawing
drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
drawText(1, 20, "White: " .. convertTime(timers[1],"m") .. ":" .. convertTime(timers[1],"s"), color.fg, color.bg)
drawText(1, 40, "Black: " .. convertTime(timers[2],"m") .. ":" .. convertTime(timers[2],"s"), color.bg, color.fg)

fastCopy()

--math
timeElapsedSinceTurn = timeElapsed() - start
timers[chesstimer.turnNum] = timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn
if timers[chesstimer.turnNum] < 0 then
clear()
drawText(1,1,chesstimer.turn .. " ran out of time!",makeColor("black"), makeColor("red"))
fastCopy()
waitForKey(key.Exit) exit = 1
end

--[[-- debug
drawText(1, 70, "DEBUG: " .. timers[1], color.dbfg, color.dbbg)
--]]--

keyDirectPoll()
end
end


--main loop
while exit~=1 do
clear()

--intro screen
if intro == 1 then fastCopy() introScreen() end

--wait screen
clear()
drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
fastCopy()

--wait
waitForKey(key.F1)

--chesstimer screen
chessTimerScreen()

--refresh screen
fastCopy()
--keypoll
keyDirectPoll()
end

print("done.")
print("")

EDIT2:

Now its freezing at "WAIT..."

Code: [Select]
print("Chess timer starting...")
--function variables
local drawRectFill = zmg.drawRectFill
local drawRect = zmg.drawRect
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local floor = math.floor
local random = math.random
local time = zmg.time
local ticks = zmg.ticks
local copySprite = zmg.copySprite

--screen vars
local SCREEN_WIDTH = 384
local SCREEN_HEIGHT = 216
local exit = 0

--game variables
local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47,
Optn=68, Up=28, Down=37, Left=38, Right=27, EXE=31}
local chesstimer = {timerStart=60, add=0, turn="White", turnNum=1, selected=1}
local color = {bg=makeColor("white"), fg=makeColor("black"), dbfg=makeColor("red"), dbbg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {}
local playerColor = {makeColor("white"), makeColor("black")}
local start=0
local timeElapsedSinceTurn = 0
local add = 0

--first poll and first start timer
keyDirectPoll()
local startTime = ticks()

--functions

local function timeElapsed()
return floor((ticks()-startTime)/128)
end

local function convertTime(t, format)
min = floor(t/60)
sec = t-(min*60)
if format=="m" then return min
elseif format=="s" then return sec
end
end

local function wait(timeToWait)
local start = timeElapsed()
while timeElapsed() - start < timeToWait do end
end

local function waitForKey(key)
while keyDirect(key) == 0 do
keyDirectPoll()
end
end

local function toggleTurn()
fastCopy()
timers[chesstimer.turnNum + 2] = (timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn)
if timers[chesstimer.turnNum + 2] < chesstimer.timerStart then
timers[chesstimer.turnNum + 2] = timers[chesstimer.turnNum + 2] + chesstimer.add
end
if chesstimer.turn == "White" then chesstimer.turn = "Black" chesstimer.turnNum = 2 chesstimer.selected = 2
else chesstimer.turn = "White" chesstimer.turnNum = 1 chesstimer.selected = 1
end
start = timeElapsed()
end
--[[--
local function outOfTime(message)
clear()
drawText(floor(SCREEN_WIDTH/2),floor(SCREEN_HEIGHT/2),message,makeColor("black"), makeColor("red"))
fastCopy()
waitForKey(key.Exit)
end
--]]--

local function introScreen()
clear()
continue = 0
drawText(1, 1, "WAIT...", color.fg, color.bg)
fastCopy()
min = 1

wait(1)



while continue==0 do
clear()
drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
drawText(1, 20, "Minutes (change with UP/DN): " .. min, color.fg, color.bg)
drawText(1, 20, "Time to add after each turn (change with LT/RT): " .. add, color.fg, color.bg)

fastCopy()

keyDirectPoll()
if keyDirect(key.Up)>0 and min<60 then
while keyDirect(key.Up) > 0 do keyDirectPoll() end
min = min+1
elseif keyDirect(key.Down)>0 and min>1 then
while keyDirect(key.Down) > 0 do keyDirectPoll() end
min = min-1
end

if keyDirect(key.Left)>0 and add<60 then
while keyDirect(key.Left) > 0 do keyDirectPoll() end
add = add+1
elseif keyDirect(key.Right)>0 and add>0 then
while keyDirect(key.Right) > 0 do keyDirectPoll() end
add = add-1
end

if keyDirect(key.EXE)>0 then continue = 1 end


end
chesstimer.timerStart = min*60
timers[1] = chesstimer.timerStart
timers[2] = chesstimer.timerStart
timers[3] = chesstimer.timerStart
timers[4] = chesstimer.timerStart
chesstimer.add = add
intro = 0
end

local function chessTimerScreen()
clear()
start = timeElapsed()
while exit ~= 1 do

if keyDirect(key.Exit) > 0 then exit = 1
elseif keyDirect(key.F1) > 0 then
while keyDirect(key.F1) > 0 do keyDirectPoll() end
toggleTurn()
end

--drawing
clear()
drawRectFill(85, (chesstimer.selected * 20) + 43, 10, 10, makeColor("darkblue"))
drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
drawText(100, 60, "White: " .. convertTime(timers[1],"m") .. ":" .. convertTime(timers[1],"s"), color.fg, color.bg)
drawText(100, 80, "Black: " .. convertTime(timers[2],"m") .. ":" .. convertTime(timers[2],"s"), color.fg, color.bg)
drawRect(1, 115, SCREEN_WIDTH - 3, 30, color.fg)
drawRect(1, 150, SCREEN_WIDTH - 3, 30, color.fg)
drawRect(1, 115, (timers[1]*(SCREEN_WIDTH-3))/chesstimer.timerStart, 30, color.fg)
drawRectFill(1, 151, (timers[2]*(SCREEN_WIDTH-3))/chesstimer.timerStart, 29, color.fg)

fastCopy()

--math
timeElapsedSinceTurn = timeElapsed() - start
timers[chesstimer.turnNum] = timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn
if timers[chesstimer.turnNum] < 0 then
drawText(1,1,chesstimer.turn .. " ran out of time!",makeColor("black"), makeColor("red"))
fastCopy()
waitForKey(key.Exit) exit = 1
end

--[[-- debug
drawText(1, 70, "DEBUG: " .. timers[1], color.dbfg, color.dbbg)
--]]--

keyDirectPoll()
end
end


--main loop
while exit~=1 do
clear()

--intro screen
if intro == 1 then fastCopy() introScreen() end

--wait screen
clear()
drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
fastCopy()

--wait
waitForKey(key.F1)

--chesstimer screen
chessTimerScreen()

--refresh screen
fastCopy()
--keypoll
keyDirectPoll()
end

print("done.")
print("")
« Last Edit: October 29, 2012, 06:47:10 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: LuaZM chesstimer, need help, not finished
« Reply #5 on: October 30, 2012, 02:16:43 pm »
OK, that turned out to be a problem with luazm, not my code.

Anyway, I released this. :)

Blog post
« Last Edit: October 30, 2012, 02:16:55 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0