Author Topic: Checkers Lua  (Read 26282 times)

0 Members and 2 Guests are viewing this topic.

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #30 on: January 07, 2012, 12:11:04 pm »
I'm trying to use on.timer() to pause. How would you do that?

Code: [Select]
a = true
timer.start(1)
repeat
until a == false

on.timer just sets a = true, but it still gets stuck in infinite loop.  :banghead:

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Checkers Lua
« Reply #31 on: January 07, 2012, 12:11:30 pm »
Wow, that looks very nice o.o What is the size of the program and whatnot? (I only own TI-84s and a TI-89).

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Checkers Lua
« Reply #32 on: January 07, 2012, 12:17:29 pm »
I'm trying to use on.timer() to pause. How would you do that?

Code: [Select]
a = true
timer.start(1)
repeat
until a == false

on.timer just sets a = true, but it still gets stuck in infinite loop.  :banghead:

Remeber : you can't stop the excecutive flow.
When you enter in a function like on.[something], you have to exit it as quick as possible.
You can't use timer.start() as an immediate timer until you exited the function.
« Last Edit: January 07, 2012, 12:18:15 pm by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Checkers Lua
« Reply #33 on: January 07, 2012, 12:18:17 pm »
euh, of course that keeps stuck in an intinite loop.. you don't change anything, don't you?

do this
Code: [Select]
pause = false
function to calculate move starts here
*here ends the code to 'calculate' the move*
pause = true
timer.start(1)
here ends the function to calculate move

function on.timer()
timer.stop()
pause = false
platform.widow:invalidate()
end
« Last Edit: January 07, 2012, 12:19:12 pm by Nick »

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Checkers Lua
« Reply #34 on: January 07, 2012, 12:28:04 pm »
Eh, Nick, why not, but managing multiple timers through a table for that specific use might be better.

Otherwise, you can use the timer.getMilliSecCounter function to get a relative timestamp (it's a number actually) and make a loop or something that checks the time and wait for time+10000 for 10 seconds, for example.

Anyway, this is not generally what you want to do anyway.

I never had any situation where I had to manually stop the flow of the program.

What do you want to do anyway ?
« Last Edit: January 07, 2012, 12:28:48 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #35 on: January 07, 2012, 12:34:12 pm »
EPIC FACE PALM. Wow. That really sucks.  :P

Wow, that looks very nice o.o What is the size of the program and whatnot? (I only own TI-84s and a TI-89).
It has that picture in it so, it's around 113 KB. Without it maybe it'd be around 3KB? I wonder if that one image is worth it...

What do you want to do anyway ?

I'm trying to get a pause in between the choice being made and the action being shown, but those are intricately connected, so I'm just trying to stop the program flow for a while.
« Last Edit: January 07, 2012, 12:39:56 pm by cyanophycean314 »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Checkers Lua
« Reply #36 on: January 07, 2012, 12:39:35 pm »
Wow, that isn't a bad size! That isn't far off from what I projected a Checkers game with AI should be!

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #37 on: January 07, 2012, 12:41:32 pm »
I just checked, it's around 4.  :)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Checkers Lua
« Reply #38 on: January 07, 2012, 12:46:09 pm »
this is the moment you wish lua was in loops and not event-based, just like "all" (read: a lot) other languages..
and indeed, 3 kb isn't that much
let us know if it works :)
« Last Edit: January 07, 2012, 12:46:40 pm by Nick »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Checkers Lua
« Reply #39 on: January 07, 2012, 01:19:10 pm »
Lua generally works like that, but the nspire version is implemented event based.
At first I did not like it, but soon I realized that you can do anything you want.
And if you really really want to, you can make nspire Lua loop based.

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #40 on: January 07, 2012, 01:28:22 pm »
Ok. Since I'm too lazy/frustrated to figure it out, I'm releasing my source a bit earlier than planned. So far the only update is that white can move first in 2 player.

Do you think I should leave the picture in? It takes up a lot of space, but it still looks pretty nice...
« Last Edit: January 07, 2012, 01:29:07 pm by cyanophycean314 »

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Checkers Lua
« Reply #41 on: January 07, 2012, 03:33:59 pm »
Here's an optimized code, a little bit (not the algorithms, but little optimizations here and there...)

http://paste.bwns.be/p/f5a094f0f
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #42 on: January 07, 2012, 05:25:38 pm »
I see you added the image in the background.

Does anybody know how to do a pause function? I would probably have to change quite a bit of code to get it to work...

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Checkers Lua
« Reply #43 on: January 08, 2012, 05:32:49 am »
I see you added the image in the background.
Oh yeah, I just wanted to see how it looks.

Does anybody know how to do a pause function? I would probably have to change quite a bit of code to get it to work...
As I said before, you can avoid the user of the timer.start() etc. by using the timer.getMilliSecCounter function to get a relative timestamp (it's a number actually) and make a loop or something that checks the time and wait for time+10000 (for 10 seconds), for example.

Look, for example :

Code: [Select]
print("hello")
startTime = timer.getMilliSecCounter()
while timer.getMilliSecCounter() < startTime+3000 do
end
print("world")  --  this will be done after 3 seconds

Meh, I don't like my own code... it doesnt look good to write such flow-stopping things ... :/
And I'm not even sure it would work in all conditions...

Also, I just thought about Lua's coroutines.... Maybe it can be useful ?
« Last Edit: January 08, 2012, 05:40:29 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Checkers Lua
« Reply #44 on: January 08, 2012, 05:42:32 am »
Code: [Select]
function pause(pauselength)
local starttimer = timer.getMilliSecCounter()

while timer.getMilliSecCounter() < starttimer + pauselength*1000
end

end

this should work i think
call it after every movecalculation with
Code: [Select]
pause(1)
this makes the pause 1 second

ninja'd by adriweb :(
« Last Edit: January 08, 2012, 06:15:40 am by Nick »