Author Topic: Checkers Lua  (Read 23615 times)

0 Members and 1 Guest are viewing this topic.

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 #45 on: January 08, 2012, 10:11:30 am »
I made a  pause function using lua coroutines a while ago. It just act like a pause function in any other language. (and is not CPU intensive)
BUT I DON'T RECOMMEND IT.
:D

You really should try to keep the even based system, and not this type of pause.
Why? Because we are working in a event based system, and for good results we should stick to it.
You can perfectly do what you want without creating such a pause function ;)

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #46 on: January 08, 2012, 10:17:21 am »
Yeah. My code was basically the same as your guys' except I used a repeat-until loop. The problem with this is that it doesn't move the player's tile until after the AI is chosen. In the movePiece function, it'll also move the AI Piece. So it's basically directly tied to your one keypress, then AI goes, then it's your turn then the screen refreshes.

Jim Bauwens - Could you share your pause function anyway?

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 #47 on: January 09, 2012, 03:58:03 am »
Well, the problem is that using the pause you will have to make some other complex changes to your current layout.
This is because of the switch from event based to loop based. My pause was made for my oncalc Lua console, and only needs to display text.
Its a bit more complex in your case.
And anyway, you should not do it this way.

I would make a timer system were you can add stuff to a queue.
Code: [Select]
_QUEUE  =  {}

function setTimeout(func, ms, toggle)
  ms  =  ms>9 and ms or 10
  table.insert(_QUEUE, {start=timer.getMilliSecCounter(), time=ms, toggle=toggle, action=func})
  if toggle then timer.start(0.01) end
end

function on.timer()
  local now  =  timer.getMilliSecCounter()
  for index, t in ipairs(_QUEUE) do
    if now>=t.start+t.time then
      t.action()
      if t.toggle then timer.stop() end
    end
  end
end


Okay, I did not test this code, so I don't know if it will run.
You just need to run "setTimeout(functiontorun, milliseconds, true)"
This should run functiontorun in milliseconds of time.
functiontorun can have for example something to invalidate the screen and toggle some flags.
« Last Edit: January 09, 2012, 04:05:12 am by jimbauwens »

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Checkers Lua
« Reply #48 on: January 09, 2012, 09:57:35 am »
this will be another fun one to play
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline AzNg0d1030

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 522
  • Rating: +45/-4
  • Hardcore anime watcher.
    • View Profile
Re: Checkers Lua
« Reply #49 on: January 09, 2012, 05:03:45 pm »
I'm so happy I got an Nspire, even if it meant getting yelled at for randomly buying...
* AzNg0d1030 loves the Nspire CX CAS!
« Last Edit: January 09, 2012, 05:04:00 pm by AzNg0d1030 »
You just lost the game.



Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #50 on: January 09, 2012, 07:25:10 pm »
Thanks guys! Hey adriweb! I was looking through your optimized version and I found statements like

Code: [Select]
turn = (turns[2] == 1 and math.random() < .5) and "o" or "x"and
Code: [Select]
color = side == "x" and "Black" or "White"
Could you explain how that works?

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #51 on: January 09, 2012, 09:43:17 pm »
Here's a little update for Checkers. I have implemented all, if not most, of adriweb's optimizations. In addition, mouse support and the chance for white to go first have been added. Oh yeah and color of kinging changed for visibility!

Enjoy! :) Might upload to Ticalc soon.

Pause still not added :(
« Last Edit: January 09, 2012, 09:48:38 pm by cyanophycean314 »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Checkers Lua
« Reply #52 on: January 10, 2012, 12:00:10 am »
Thanks guys! Hey adriweb! I was looking through your optimized version and I found statements like

Code: [Select]
turn = (turns[2] == 1 and math.random() < .5) and "o" or "x"and
Code: [Select]
color = side == "x" and "Black" or "White"
Could you explain how that works?

The statements adriweb wrote are idioms for Lua that take advantage of Lua's short circuit evaluation with logical operators to change the value of a variable without using if ... then ... else statements .

Basically it follows the form:
Code: [Select]
var = A and B or C

It has equivalent results as the following code:
Code: [Select]
if A then
     var = B
else
     var = C
end

So if A is a true value then var = B and if A is a false value then var = C. The only condition for this to work properly is B must be a true value (not false or nil) or it may give unexpected results (unless, of course, you were expecting them).

Great work with the AI so far and I hope you get the pause figured out soon. Would it be possible to split up the player and AI moves and have the AI wait for a signal from the timer to make the move?

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Checkers Lua
« Reply #53 on: January 10, 2012, 06:43:51 am »
Exactly, 3rik :)

These are called ternary structures (fast "if"s, yeah) :P

They are useful when you can have them stand in one line, it's clearer, I think.
For longers if then else, I wouldn't recommend it so much.
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Checkers Lua
« Reply #54 on: January 10, 2012, 09:31:42 am »
It is more than that, look :

Code: [Select]
toto = bool1
toto = bool1 or bool2 -- if bool1, then bool1
toto = bootl1 and bool2 -- if bool1, then bool2
toto = bool1 and bool2 or bool3 -- if bool1 then (bool2 or bool3) seen previously
toto = bool1 and bool2 and bool3 and bool4 or bool5 or bool6 -- if bool1 and bool2 and bool3 and bool4 and bool5 then bool6

And since, in Lua, values are equivalent to true, and nil equivalent to false, it works for every kind of type.
« Last Edit: January 10, 2012, 09:32:04 am 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 #55 on: January 10, 2012, 12:48:40 pm »
thanks for explaining, can be very useful..

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 #56 on: January 10, 2012, 01:24:40 pm »
For people used to C this is the equalivent of "var=bool ? Val1:Val2" (but better).

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Checkers Lua
« Reply #57 on: January 10, 2012, 05:05:01 pm »
Wow. Very helpful! Thanks guys!

I'll try implementing pause, but not highest priority.