Author Topic: [Lua] Speedcubing Timer  (Read 5398 times)

0 Members and 1 Guest are viewing this topic.

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
[Lua] Speedcubing Timer
« on: December 20, 2011, 10:33:43 pm »
Hey guys, it's been a while since I've contributed anything here, but over the last week I had some free time.
Last week I took 8 different finals, and in the time after the tests I spent crafting a tool to help me with a new passion of mine.

Speed cubing, or the process of competitively solving a Rubik's cube.

At first I included a simple timer, that was originally measured in milliseconds, but after further work I formatted the time into a 00:00.00 format and added a list of previous times, automagic saving, and statistic keeping (best, total average, average of 10, soon to be median 3 of 5 and 10 of 12)

So, the controls:
Enter: Start/stop the timer
"Y"/"N": After stopping the timer you have the option whether or not to add it to the list of times, the options should be obvious
"D": Clears the current list of times
"S"/"R": Saves/Recalls one saved list of times

Since I wrote this on calc, and not with the assistance of an emulator, I have no screenshots for you, but here's the code (hopefully it works after transcribing it from my calc)

I'll probably add new features here as I make them.
Note: You can just compile this with luna or something and run it on calc.

So if any of you guys are cubers, maybe you'll use this to practice when you can't access a computer  :)

Code: (lua) [Select]
tn, msg = 0, "";
t, n, aa, a10, b = 0, 0, 0, 0, 0
save = false
times = {};

function a()
local min, avg10, avga = 2^32, 0, 0
for i = 1, tn do
local v = times[i]
if v < min then
min = v
end
if i <= 10 then
avg10 = avg10 + v
end
avga = avga + v
end
b = min
aa = math.floor(avga/tn)
a10 = math.floor(avg10 / math.min(tn, 10))
end

function f(t)
return ("%02d:%02d.%02d"):format(t/60000, t/1000%60, t/10%100)
end

function on.paint(g)
if c then
t = (timer.getMilliSecCounter() - n)
end
g:drawString("time: " .. f(t), 0, 0, "top");
g:drawString(msg, 0, 20, "top");
g:drawString("best:              " .. f(b), 150, 0, "top");
g:drawString("average (10): " .. f(a10), 150, 20, "top");
g:drawString("average (all): " .. f(aa), 150, 40, "top");
for i = 1, 8 do
local v = times[i];
if not v then break end
g:drawString(i .. ")      " .. f(v), 0, 20+i*20, "top")
end
timer.start(0.01);
end

function on.timer()
timer.stop()
platform.window:invalidate()
end

function on.charIn(c)
if save then
save = false
msg = ""
if c == "y" then
table.insert(times, 1, t)
tn = tn + 1
a()
on.charIn("s")
end
end
if c == "d" then
t = 0
times = {};
tn = 0;
msg = "";
a();
elseif c == "s" then
local s = "" .. tn
msg = "saved"
for i = 1, tn do
s = s .. " " .. times[i]
end
var.store("cube_times", s .. " ");
elseif c == "r" then
local s, b , n = var.recall("cube_times") or "0 ", {}, 0;
msg = "loaded";
for w in s:gmatch("%d+%s") do
n = n + 1;
b[n] = tonumber(w);
end
tn = table.remove(b, 1);
times = b;
a();
end
end

function on.enterKey()
 if save then
 save = false
msg = ""
return
end
if c then
save = true;
msg = "save? (y/n)";
c = nil;
else
c = true;
msg = "";
n = timer.getMilliSecCounter();
end
end

on.charIn("r")

« Last Edit: December 21, 2011, 11:47:01 am by NecroBumpist »
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: [Lua] Speedcubing Timer
« Reply #1 on: December 21, 2011, 01:39:35 am »
I'll check it out when I got some time :)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: [Lua] Speedcubing Timer
« Reply #2 on: December 21, 2011, 02:04:53 am »
Code: [Select]
nice, but i had to put this in the code:
[code],2^32,2^32
in
Code: [Select]
function a()
local min, avg10, avga = 2^32[b],2^32,2^32[/b]

and the averages are in milliseconds again?

but except for those things, it works perfect, and is fast (i mean it stops when you enter, and not a second later)[/code]
« Last Edit: December 21, 2011, 02:06:44 am by Nick »

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: [Lua] Speedcubing Timer
« Reply #3 on: December 21, 2011, 11:46:30 am »
nice, but i had to put this in the code:
Code: [Select]
,2^32,2^32in
Code: [Select]
function a()
local min, avg10, avga = 2^32,2^32,2^32

and the averages are in milliseconds again?

but except for those things, it works perfect, and is fast (i mean it stops when you enter, and not a second later)

Oh yeah, I forgot that avg10 and avga need to be initialized to a number before they can be used, so I'll go change that.

And yes, all the times are stored and manipulated in milliseconds, then before it's printed to the screen it gets formatted into the 00:00.00 format.
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: [Lua] Speedcubing Timer
« Reply #4 on: December 21, 2011, 11:53:40 am »
one thing you could add is a random scrambler to (well yeah, you could guess this) scramble the cube before you start (like RU (right up) LD (left down) etc etc) so you can totally use it as a rubik's game
oh and do you use the modulus to calculate, or another way?

is scramble te right word for this? xp
« Last Edit: December 21, 2011, 11:56:25 am by Nick »

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: [Lua] Speedcubing Timer
« Reply #5 on: December 21, 2011, 12:14:59 pm »
one thing you could add is a random scrambler to (well yeah, you could guess this) scramble the cube before you start (like RU (right up) LD (left down) etc etc) so you can totally use it as a rubik's game
oh and do you use the modulus to calculate, or another way?

is scramble te right word for this? xp

Yeah, I've though of adding a scramble generator, so I'll get started on that soon.

I use both plain division and modulus for formatting the time for simplicity, for averages it's division and math.floor()
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: [Lua] Speedcubing Timer
« Reply #6 on: December 21, 2011, 12:26:23 pm »
i just read you couldn't get a screenshot, so here is one.
i hope i've used all functions possible, but i think so

screenietime :)


i have to be honest: the timer runs a lot smoother on calc, you can see the milliseconds passing, and here it just flickers, but that is to make it not too huge to upload..
« Last Edit: December 21, 2011, 12:26:37 pm by Nick »