Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: cyanophycean314 on January 02, 2012, 05:13:41 pm

Title: Checkers Lua
Post by: cyanophycean314 on January 02, 2012, 05:13:41 pm
So I've been working on this version of Checkers. It's based off a previous project from Python a while back. As of now, only two-player is supported, but AI will come later.

I tried taking an animated screenshot using Nick's method, but it ended up giving me a 350 MB gif.  :P

Screenshot:
(http://img.removedfromgame.com/imgs/checker_screen.JPG)

Edit:
Moving screenshots has now been created! With 79KB!
(http://img.removedfromgame.com/imgs/checkers_screen.gif)
Title: Re: Checkers Lua
Post by: renatose on January 02, 2012, 05:55:27 pm
Great! will it have AI?
Did you checked the colors in grayscale mode to verify if it is playable in all nspires?
Title: Re: Checkers Lua
Post by: GB on January 02, 2012, 05:56:26 pm
I think the graphics might look a little better with round pieces. But nice work anyways, no matter what shape they are!
Title: Re: Checkers Lua
Post by: saintrunner on January 02, 2012, 05:58:15 pm
Wow, this looks like a great start! Now I REALLY want an Nspire!
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 02, 2012, 05:59:36 pm
@Renatose: As I said in the first post, AI will be implemented. As a matter of fact, I'm working on it right now. How would you check the colors for non-color? I haven't really worked with non-CXs at all...

@GB:Yes, the rectangles shall become circles soon enough.

@Saintrunner: You posted while I was posting this post...
Title: Re: Checkers Lua
Post by: saintrunner on January 02, 2012, 06:00:59 pm
really? the time stamp says we were a minute apart!
Title: Re: Checkers Lua
Post by: Nick on January 02, 2012, 06:01:27 pm
the screenie is really nice :) works wonderful.. but can you also move to the not-marked places?

and maybe make it possible for multi-step moves (dunno how to say that) like when you can take 3 pieces in a row, you have to do that (officially xp) so maybe, add this if possible?
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 02, 2012, 06:11:15 pm
@saintrunner: Although the time stamp says that, I glanced at the topic summary and saw a new post!

@Nick: Moving to those squares aren't possible. Jumping multiple times will be added later.

I've read on wikipedia that it's called draughts and if given the opportunity to take a piece, you must. I've never heard of those two facts until now...
Title: Re: Checkers Lua
Post by: renatose on January 02, 2012, 06:16:41 pm
In the computer software if you chose a keypad that's not CX i.e. a touchpad or a clickpad then you'll see it in grayscale. That's good for testing ;)
Title: Re: Checkers Lua
Post by: apcalc on January 02, 2012, 06:25:18 pm
This looks very nice!  I can't wait to try it! :)
Title: Re: Checkers Lua
Post by: DJ Omnimaga on January 02, 2012, 06:52:10 pm
Looks nice. By the way aren't the white pieces normally red? I didn't play chess in 15 years but every board I got came with red and black pieces.
Title: Re: Checkers Lua
Post by: Adriweb on January 02, 2012, 07:00:27 pm
Nice :D

@DJ_O : I've always seen blakc and white, for Checkers* ("Jeu de dames")
Title: Re: Checkers Lua
Post by: DJ Omnimaga on January 02, 2012, 07:12:20 pm
Ah ok. Maybe it depends of the region or it was just a coincidence. Not that it matters much to me though, I always lost to the game, anyway :P
Title: Re: Checkers Lua
Post by: epic7 on January 02, 2012, 07:13:04 pm
Cool! How did you get a moving screenie?
Title: Re: Checkers Lua
Post by: DJ Omnimaga on January 02, 2012, 07:15:53 pm
When using TI-Nspire student software, use Camstudio to record an avi file then virtualdub to export as GIF. CalcCapture also works and gives much smaller file sizes, but the quality suffers.
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 02, 2012, 08:57:49 pm
Well, lots of activity. For a moving screenshot, I use Camstudio like DJ_O said, but I use Ulead Animator 5 for conversion/editing. For camstudio, you can just turn the settings down to record around 10 fps to keep down file size. You don't need high framerate anyways.

I might change the color, as it helps displays the kinging. As soon as very early version of AI is done, I'll probably have an initial release. Tomorrow maybe?

@renatose Here's a black and white screenie:

(http://img.removedfromgame.com/imgs/checkers_bw.JPG)

I think the arrow color could be changed, but otherwise it's pretty good!

Right now, every time I want to have a picture in the post, I upload to rfg. If the picture is attached to the post, is there anyway to display it in the post?
Title: Re: Checkers Lua
Post by: 3rik on January 02, 2012, 09:50:52 pm
I think by attaching it automatically shows the picture at the bottom as long as it has a common file extension.

If you want to show it in a different part of the post you can modify your post and use the url of the attachment in [img] tags.

For example

[img]http://www.omnimaga.org/index.php?action=dlattach;topic=12110.0;attach=11007[/img] produces (http://www.omnimaga.org/index.php?action=dlattach;topic=12110.0;attach=11007)

I wish you luck with your project. It looks like you have a great start.
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 03, 2012, 08:58:28 pm
OK. Random AI is now working! Rectangles are circles! White might become red?

More difficulty/menu later!!  :D
Title: Re: Checkers Lua
Post by: Nick on January 04, 2012, 02:29:15 am
nice :) but i think you should think to on the not-cx users too, the difference between the red and the brown will be hard xs

but it looks good. you could ask the guy who made that chess game how you should calculate which step the AI should take. He did something like checking what each step delivers, and te one with the largest number is the one that's chosen
Title: Re: Checkers Lua
Post by: DJ Omnimaga on January 04, 2012, 02:31:27 am
Looks nice. I guess it might be good to check how it looks like on black and white models, though, in case it actually looks too hard to distinguish.
Title: Re: Checkers Lua
Post by: renatose on January 04, 2012, 04:23:56 am
I think that the next thing you should add to the AI is a way to check if there are any moves to take over opponents pieces and if there are various moves possible for that to choose the moves for taking the most pieces.

Thinking on me and all the others that have non-CX nspires I think that you should really test all the colours to check if they are easily distinguishable in grayscale.
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 05, 2012, 08:02:33 pm
Sorry on no updates yesterday, school busy.  :(

Due to universal consensus, the color is going to be white.  :)

This is the initial release of the game, but there shouldn't be too many bugs.

Features
 * AI (Random and King/Jump/Random)
 * 2 Player

Multiple jumping is still not included, but will be.

Maybe I'll change the symbol for kinging...

Black and white moving screenie attached and so is tns.

Edit: On calc, the circles look like octagons. Idk why
Title: Re: Checkers Lua
Post by: Jim Bauwens on January 06, 2012, 03:41:06 am
Looks very nice!


How do you draw your circles?
gc:fillArc ?
Title: Re: Checkers Lua
Post by: Nick on January 06, 2012, 03:50:27 am
Ö i love the 3D-ish in it.. and the dropshadow, and the AI, great :p

maybe you could add a little pause between your move and the one from the AI, since now it happens directly, and you don't have the time to see what happens
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 06, 2012, 04:33:35 pm
Yeah, for the circles are fillArc. Is there a better way to draw circles?

I'll add a pause. Thanks! Though the AI is failed against the tests against my friends at school. They win every time.  :P
Title: Re: Checkers Lua
Post by: Nick on January 06, 2012, 04:35:00 pm
well, tbh, it does really weird things sometimes.. like it sometimes does not 'kill' one of yours, for no reason
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 06, 2012, 04:57:24 pm
Does it not? It should... Will reexamine.

Btw, while trying to implement the pausing function, I have crashed the emulator twice.  :P
Title: Re: Checkers Lua
Post by: Nick on January 06, 2012, 06:15:05 pm
crashing like in real crashing, with reboot?

O.o
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 06, 2012, 09:45:50 pm
I have to use task manager to close out of the program and then restart the program, I should probably try to implement it better.
Title: Re: Checkers Lua
Post by: Jim Bauwens on January 07, 2012, 11:02:37 am
You had a endless loop? :P
Try to use on.timer for the pausing :)
Title: Re: Checkers Lua
Post by: cyanophycean314 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:
Title: Re: Checkers Lua
Post by: Xeda112358 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).
Title: Re: Checkers Lua
Post by: Levak 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.
Title: Re: Checkers Lua
Post by: Nick 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
Title: Re: Checkers Lua
Post by: Adriweb 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 (http://wiki.inspired-lua.org/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 ?
Title: Re: Checkers Lua
Post by: cyanophycean314 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.
Title: Re: Checkers Lua
Post by: Xeda112358 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!
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 07, 2012, 12:41:32 pm
I just checked, it's around 4.  :)
Title: Re: Checkers Lua
Post by: Nick 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 :)
Title: Re: Checkers Lua
Post by: Jim Bauwens 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.
Title: Re: Checkers Lua
Post by: cyanophycean314 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...
Title: Re: Checkers Lua
Post by: Adriweb 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 (http://paste.bwns.be/p/f5a094f0f)
Title: Re: Checkers Lua
Post by: cyanophycean314 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...
Title: Re: Checkers Lua
Post by: Adriweb 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 (http://wiki.inspired-lua.org/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 (http://lua-users.org/wiki/CoroutinesTutorial).... Maybe it can be useful ?
Title: Re: Checkers Lua
Post by: Nick 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 :(
Title: Re: Checkers Lua
Post by: Jim Bauwens 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 ;)
Title: Re: Checkers Lua
Post by: cyanophycean314 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?
Title: Re: Checkers Lua
Post by: Jim Bauwens 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.
Title: Re: Checkers Lua
Post by: jwalker on January 09, 2012, 09:57:35 am
this will be another fun one to play
Title: Re: Checkers Lua
Post by: AzNg0d1030 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.../me loves the Nspire CX CAS!
Title: Re: Checkers Lua
Post by: cyanophycean314 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?
Title: Re: Checkers Lua
Post by: cyanophycean314 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 :(
Title: Re: Checkers Lua
Post by: 3rik 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?
Title: Re: Checkers Lua
Post by: Adriweb 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.
Title: Re: Checkers Lua
Post by: Levak 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.
Title: Re: Checkers Lua
Post by: Nick on January 10, 2012, 12:48:40 pm
thanks for explaining, can be very useful..
Title: Re: Checkers Lua
Post by: Jim Bauwens on January 10, 2012, 01:24:40 pm
For people used to C this is the equalivent of "var=bool ? Val1:Val2" (but better).
Title: Re: Checkers Lua
Post by: cyanophycean314 on January 10, 2012, 05:05:01 pm
Wow. Very helpful! Thanks guys!

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