Author Topic: Goplat's 15Puzzle for TI-nspire Lua  (Read 8439 times)

0 Members and 1 Guest are viewing this topic.

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Goplat's 15Puzzle for TI-nspire Lua
« on: March 27, 2012, 07:20:48 am »
Well hi everyone, I have been slowly trying to learn TI-nspire Lua here.
I have thought for a bit, made some demo beginner programs with the help of jimbauwens :D, and learnt a lot along the way (also creating a batch file for Luna for quick lua -> tns conversion). But I still have a long way to go.

So I had the tns file for 15puzzle but it didn't work, probably because it was designed to work with OS<3.0.2.

Fortunately I have managed to extract Problem1.xml from the tns file (rename it as a zip), copy to Notepad++ save as a .lua file, then used this page, to fix some errors/discrepancies with xml coding and tinspire Lua code, and got it working!
Played it, got it solved in 353 moves. :D

Now here I post the source code and tns file of 15Puzzle, edited by Jonius7 for OS 3.0.2 and above

And I think one of the best ways to learn is to analyse source code line by line and try to understand what it means.
So I shall try here with some help to dissect this source code:

Code: [Select]
function on.charIn(ch)
if ch == "r" then
board = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }

-- Randomize
local swaps = 0
for i = 1, 16 do
local j = math.random(i, 16)
if i ~= j then
board[i], board[j] = board[j], board[i]
swaps = swaps + 1
end
if board[i] == 16 then
empty = i
end
end

-- If the number of swaps plus the number of empty-space movements is odd,
-- then the puzzle is impossible, so do an impossible move to make it possible
-- (exchange the space with a tile two squares away)
if (swaps + (empty - 1) + math.floor((empty - 1) / 4)) % 2 == 1 then
local i = (empty + 7) % 16 + 1
board[empty] = board[i]
empty = i
board[empty] = 16
end

moves = 0
platform.window:invalidate()
end
end

on.charIn("r")

function on.paint(gc)
local won = true
for i, n in ipairs(board) do
if n ~= 16 then
local x = (platform.window:width() - 128) / 2 + 32*((i - 1) % 4)
local y = (platform.window:height() - 128) / 2 + 32*math.floor((i - 1) / 4)
gc:drawRect(x, y, 30, 30)
gc:drawString(n, x + (30 - gc:getStringWidth(n)) / 2, y + 15, "middle")
end
won = (won and n == i)
end

local str = moves .. " moves"
gc:drawString(str, (platform.window:width() - gc:getStringWidth(str)) / 2, 0, "top")
if won then
str = "You win! Press R to reset"
gc:drawString(str, (platform.window:width() - gc:getStringWidth(str)) / 2, platform.window:height(), "bottom")
end
end

function on.arrowKey(key)
local dir
if key == "left" and empty % 4 ~= 0 then
dir = 1
elseif key == "right" and empty % 4 ~= 1 then
dir = -1
elseif key == "up" and empty <= 12 then
dir = 4
elseif key == "down" and empty > 4 then
dir = -4
else
return
end
board[empty] = board[empty + dir]
empty = empty + dir
board[empty] = 16
moves = moves + 1
platform.window:invalidate()
end


« Last Edit: April 04, 2012, 02:18:44 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: 15Puzzle for TI-nspire Lua
« Reply #1 on: March 27, 2012, 09:42:45 am »
Good progress !
I hope you'll be able to create nice games soon :)

By the way, I already managed to get 15puzzle to work on OS 3.1, just by saving it under an older OS... (3.0.1 IIRC)
(and my highscore is 72 moves)

Good luck with lua !
« Last Edit: March 27, 2012, 12:35:26 pm by Chockosta »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: 15Puzzle for TI-nspire Lua
« Reply #2 on: March 27, 2012, 11:07:12 am »
3.2 is not yet released ..
I think you are mixing version numbers ;)

Here is a little list of versions:
3.0.0 -- Only on CX in shop
3.0.1
3.0.2 -- They added encryption to documents. Documents made with luna2tns and similar (for 3.0.1) don't work anymore
3.1    -- Present
3.2    -- Coming soon
« Last Edit: March 27, 2012, 11:07:43 am by jimbauwens »

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: 15Puzzle for TI-nspire Lua
« Reply #3 on: March 27, 2012, 12:35:45 pm »
You're right, sorry.
I edited my post.

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: 15Puzzle for TI-nspire Lua
« Reply #4 on: March 27, 2012, 01:15:50 pm »
Also, just to be clear, all .tns created with Luna work with all the versions :)
« Last Edit: March 27, 2012, 01:43:34 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: 15Puzzle for TI-nspire Lua
« Reply #5 on: March 27, 2012, 01:22:21 pm »
They work on 3.2 without changes ;)
The changes I made are for something else :)

Offline DJ Omnimaga

  • Former TI programmer
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55913
  • Rating: +3152/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: 15Puzzle for TI-nspire Lua
« Reply #6 on: March 27, 2012, 02:25:41 pm »
3.2 is not yet released ..
I think you are mixing version numbers ;)

Here is a little list of versions:
3.0.0 -- Only on CX in shop
3.0.1
3.0.2 -- They added encryption to documents. Documents made with luna2tns and similar (for 3.0.1) don't work anymore
3.1    -- Present
3.2    -- Coming soon


There's a 3.0.0?? O.O

Anyway it would be great to check if all the cool old Lua games worked for newer OSes, and if someone could port them if the author left or doesn't plan to maintain them anymore. :) Also I guess this can be a good learning experience.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: 15Puzzle for TI-nspire Lua
« Reply #7 on: March 27, 2012, 02:27:52 pm »
TI has done excelent work in keeping compatibility :)
The 3.0.0 version was found on the first CX's .

Offline DJ Omnimaga

  • Former TI programmer
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55913
  • Rating: +3152/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: 15Puzzle for TI-nspire Lua
« Reply #8 on: March 27, 2012, 02:32:13 pm »
Well they sure didn't with Print for example :P. Had any old game used sound or stuff like that, sound would no longer work on OS 3.1 without Ndless.

And ok about OS 3.0.0, any difference with 3.0.1 in it such as no Boot2 crash?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: 15Puzzle for TI-nspire Lua
« Reply #9 on: March 27, 2012, 02:40:38 pm »
Well, it was preinstalled so we don't know :P

Offline DJ Omnimaga

  • Former TI programmer
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55913
  • Rating: +3152/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: 15Puzzle for TI-nspire Lua
« Reply #10 on: March 27, 2012, 03:11:40 pm »
Well I also meant about what it was missing from OS 3.0.1 or if it had any extra bugs. :P

For example I remember reading somewhere it still had leftovers from the 84+ emulator in the CX version, but I forgot if 3.0.1 still had those.
« Last Edit: March 27, 2012, 03:12:31 pm by DJ_O »

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: 15Puzzle for TI-nspire Lua
« Reply #11 on: March 27, 2012, 06:41:11 pm »
3.2 is not yet released ..
I think you are mixing version numbers ;)

Here is a little list of versions:
3.0.0 -- Only on CX in shop
3.0.1
3.0.2 -- They added encryption to documents. Documents made with luna2tns and similar (for 3.0.1) don't work anymore
3.1    -- Present
3.2    -- Coming soon


I did say 3.0.2 didn't I?
But technically I tested it in 3.1.0
I understand about 60% of the code.

so what do the % signs do?
« Last Edit: March 27, 2012, 08:20:12 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: 15Puzzle for TI-nspire Lua
« Reply #12 on: March 27, 2012, 10:26:33 pm »
% Stands for modulo (though it can have other uses too)

a % b == a - math.floor(a/b)*b

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: 15Puzzle for TI-nspire Lua
« Reply #13 on: March 27, 2012, 10:51:30 pm »
Hmm, I suspected that it stood for the modulo/(or modulus?)
So a subtracted from  (a/b with remainder ignored * b) = modulo?

a=5
b=2
5- 2*2
% = 1

Interesting.
Still not completely sure how it works, but it works!
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: 15Puzzle for TI-nspire Lua
« Reply #14 on: March 28, 2012, 07:14:03 am »
About compatibility, DJ_O :

print() is gone in 3.1 (I haven't checked in 3.2 on-calc though).
gc:setAlpha() is gone in 3.2, and it wasn't documented before
on.create() become on.construction()

There is a retrocompatibility thing with platform.apilevel

So, the last one will be quite (on.create change) is probably the most annoying change, since it has to be updated...
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation