Jump to content



Photo
- - - - -

Luazm Chesstimer (Need Help, Not Finished)

LuaZM chess timer

  • Please log in to reply
3 replies to this topic

#1 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 28 October 2012 - 11:22 PM

OK, here's my chess timer:

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 <img src='/dot/public/style_emoticons/<#EMO_DIR#>/EXE.jpg' class='bbc_emoticon' alt='[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 <span class=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?

#2 MicroPro

MicroPro

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 640 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    Casio ClassPad 300

Posted 02 November 2012 - 12:07 PM

I can't test it but try putting debug messages in each line of the introScreen function after printing that message and in the while exit~=1 do part after calling intoScreen.

You are calling drawText to display "Press F1..." and "NextPlayer..." but don't call fastcopy() after that. Can it be that the program is actually waiting in the waitForKey(key.F1) line without displaying the messages?

#3 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 02 November 2012 - 01:29 PM

Yeah, I forgot to update this topic. It was two problems, actually. LuaZM freezes sometimes when trying to display text (consistently with certain strings, its not random). Also, I was not fastCopy'ing everywhere I should have been.

I was able to fix the first problem by changing the string. The second problem was just a matter of adding fastCopy() where it belonged. ;)

#4 MicroPro

MicroPro

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 640 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    Casio ClassPad 300

Posted 07 November 2012 - 12:07 PM

OK. I didn't really look at the dates. glad it fixed.





Also tagged with one or more of these keywords: LuaZM, chess, timer

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users