### Author Topic: Numstrat - Stumbling into Lua  (Read 17475 times)

0 Members and 1 Guest are viewing this topic.

#### Jonius7

• python! Lua!
• LV10 31337 u53r (Next: 2000)
• Posts: 1918
• Rating: +82/-18
• Still bringing new dimensions to the TI-nspire...
##### Numstrat - Stumbling into Lua
« on: April 04, 2012, 02:27:10 am »
Hi everyone,
I haven't been active much on the forums in the past few days (in comparison with the past few months), but I have created a simple Lua program while experimenting on oclua. I have transferred the code into a Lua file on the computer and here it is!
Basically you start with a value of 0 and you can choose to Attack or Defend to try and increase your value. right now I am experimenting with the points system as Attack carries no more risk than Defence and both are as equally likely to win as to lose.

Here is also the source code. Feel free to look through it, comment on my program, how my coding is, any improvements on optimising it or making it easier for another programmer to read, and stuff. The more feedback I get the more I can use the feedback to broaden my understanding of Lua!
Code: [Select]
--[[Things to do:Declare variables first within a functionMake things more fun and complexPoints System:OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}Any better ways? Maybe make Atk more risky, so it's less about luck]]--v=0t=0r1=0r2=2r3=0r4=2function on.paint(gc) gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top") gc:drawString(v,10,30,"top") if r2==1 then gc:drawString("Attack was successful. +" .. r1,10,50,"top") end if r2==0 then gc:drawString("Attack was unsuccessful. -" .. r1,10,50,"top") end if r4==1 then gc:drawString("Defence was successful. +" .. r3,10,50,"top") end if r4==0 then gc:drawString("Defence was unsuccessful. -" .. r3,10,50,"top") end gc:drawString("Turn " .. t,200,10,"top") if v~=0 or t~=0 then gc:drawString("Average " .. v/t,200,30,"top") end gc:setFont("sansserif","r",8) gc:drawString("Numstrat - Jason Ho",10,200,"top")endfunction on.charIn(ch) cha=ch if cha=="a" or cha=="d" then if cha=="a" then r4=2 r1=math.random(4,5) r2=math.random(0,1) if r2==1 then v=v+r1 end if r2==0 then v=v-r1 end end if cha=="d" then r2=2 r3=math.random(1,3) r4=math.random(0,1) if r4==1 then v=v+r3 end if r4==0 then v=v-r3 end end t=t+1 end if cha=="r" then v=0 t=0 r1=0 r2=2 r3=0 r4=2 end platform.window:invalidate()end
With my current knowledge, I think a Lua version of my TI-nspire Basic Game, Jason's TI-nspire Hold'em is achievable. I just need to get working on it, persistently

Thanks everyone!
« Last Edit: April 04, 2012, 02:45:51 am by Jonius7 »
 Programmed some CASIO Basic in the pastDJ Omnimaga Music Discographist DJ Omnimaga DiscographyMy Own Music!
My Released Projects (Updated 2015/05/08)
 TI-nspire BASICTI-nspire Hold 'em Health Bar Scissors Paper Rock TI-nspire LuaNumstrat TI-nspire Hold 'em LuaTransport ChooserSecret Project (at v0.08.2 - 2015/05/08) Spoiler For Extra To-Be-Sorted Clutter: Spoiler For Relegated Projects: TI-nspire BASICBattle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)Axe ParserDoodle God (stalled while I go and learn some Axe)

#### Nick

• LV9 Veteran (Next: 1337)
• Posts: 1166
• Rating: +161/-3
• You just got omnom'd
##### Re: Numstrat - Stumbling into Lua
« Reply #1 on: April 04, 2012, 02:50:13 am »
looks good. I've tried it and it runs fine. But there are some things i would change:
- make the average value round (floor or ceil whatever, but it does not now so it dissapears partly from screen because it's too long)
- why do you do "cha = ch" ? since you could just be using the ch from your argument, couldn't you?
- i would make a on.create() which contains all the r1=0, r2=2 etc so that when you press "r" you can just call on.create() and you don't have to type it twice.
- maybe make a table with those value instead of 5 different variables. Like r1 = table[1] etc. but then you have to know what value is at what place

These are just thought, it's already great as is

edit: and why do you do "if cha=="a" or cha=="d" then if cha=="a" then.." ? that's just twice the same statement that follows, this only takes time and space
« Last Edit: April 04, 2012, 02:51:43 am by Nick »

#### Jonius7

• python! Lua!
• LV10 31337 u53r (Next: 2000)
• Posts: 1918
• Rating: +82/-18
• Still bringing new dimensions to the TI-nspire...
##### Re: Numstrat - Stumbling into Lua
« Reply #2 on: April 04, 2012, 02:59:18 am »
Thanks Nick! That's a great summary of things to fix.
Most of that stuff I was partly aware of already, but just hadn't put in yet.
- So how about if I wanted to display the average up to 2 decimal places? i.e. Fix2?
- cha=ch probably because I changed it from a getchar program I got from jimbauwens and just left it there
- Yes on.create() was what I was looking for thanks
- For a table is that different to a matrix/array/list?
- "if cha=="a" or cha=="d" then if cha=="a" then.." I think I did that because there is t=t+1 at the end. But it would make more sense (and save more space), to put t=t+1 inside the if cha=="a" and if cha=="d" I think then.

Thanks for the help!
I used http://wiki.inspired-lua.org/ for some help.
« Last Edit: April 04, 2012, 06:35:10 am by Jonius7 »
 Programmed some CASIO Basic in the pastDJ Omnimaga Music Discographist DJ Omnimaga DiscographyMy Own Music!
My Released Projects (Updated 2015/05/08)
 TI-nspire BASICTI-nspire Hold 'em Health Bar Scissors Paper Rock TI-nspire LuaNumstrat TI-nspire Hold 'em LuaTransport ChooserSecret Project (at v0.08.2 - 2015/05/08) Spoiler For Extra To-Be-Sorted Clutter: Spoiler For Relegated Projects: TI-nspire BASICBattle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)Axe ParserDoodle God (stalled while I go and learn some Axe)

#### Nick

• LV9 Veteran (Next: 1337)
• Posts: 1166
• Rating: +161/-3
• You just got omnom'd
##### Re: Numstrat - Stumbling into Lua
« Reply #3 on: April 04, 2012, 06:04:50 am »
to mak eit only display 2 decimals after the comma, you can do math.floor(average*10)/10. I don't know if there's another way to do this, but that's my way And i don't think there's an easy way to make it only have to decimals at all...

and i think it's faster to put twice t = t+1 than if since if is a statement, the other one just an addition, which is faster

a table is te same as a list in lua, you don't have two dimensional things. just do e.g.
Code: [Select]
function on.create()     vars = {0, 0, 0, 2, 0, 2}end--this must in the if ch=="a" thingieif vars[3] = 0 then     vars[1] = vars[1]+1end[code]this might be a bit more timeconsuming to type, but it think it's more beautiful to see, rather than 5 vars :) [/code]

#### Jonius7

• python! Lua!
• LV10 31337 u53r (Next: 2000)
• Posts: 1918
• Rating: +82/-18
• Still bringing new dimensions to the TI-nspire...
##### Re: Numstrat - Stumbling into Lua
« Reply #4 on: April 04, 2012, 06:37:07 am »
to mak eit only display 2 decimals after the comma, you can do math.floor(average*10)/10. I don't know if there's another way to do this, but that's my way And i don't think there's an easy way to make it only have to decimals at all...

and i think it's faster to put twice t = t+1 than if since if is a statement, the other one just an addition, which is faster

a table is te same as a list in lua, you don't have two dimensional things. just do e.g.
Code: [Select]
function on.create()     vars = {0, 0, 0, 2, 0, 2}end--this must in the if ch=="a" thingieif vars[3] = 0 then     vars[1] = vars[1]+1end[code]this might be a bit more timeconsuming to type, but it think it's more beautiful to see, rather than 5 vars :) [/code]

Thanks Nick, I shall update this if I have time.
From what I gather table is equivalent to list
and an array is equivalent to matrix
Thanks!
Hopefully this will just be the beginning of my Journey in Lua
 Programmed some CASIO Basic in the pastDJ Omnimaga Music Discographist DJ Omnimaga DiscographyMy Own Music!
My Released Projects (Updated 2015/05/08)
 TI-nspire BASICTI-nspire Hold 'em Health Bar Scissors Paper Rock TI-nspire LuaNumstrat TI-nspire Hold 'em LuaTransport ChooserSecret Project (at v0.08.2 - 2015/05/08) Spoiler For Extra To-Be-Sorted Clutter: Spoiler For Relegated Projects: TI-nspire BASICBattle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)Axe ParserDoodle God (stalled while I go and learn some Axe)

#### Jim Bauwens

• Lua! Nspire! Linux!
• Editor
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +206/-7
• Linux!
##### Re: Numstrat - Stumbling into Lua
« Reply #5 on: April 04, 2012, 07:21:01 am »
Lua doesn't have list, arrays or matrices.
The only thing similar are tables, and tables blow all of the other things away.
You can easily combine tables to get a matrix:

Code: [Select]
matrix= {    {1,2,3},     {4,5,6},    {7,8,9}}print(matrix[2][3]) -- prints 6
You can put any lua object in a table, and you can use any object as a key.
Lua tables can also be manipulated by metatables and metamethods, making them extremely cool. But that is a bit more advanced

#### someone

• LV3 Member (Next: 100)
• Posts: 49
• Rating: +9/-0
##### Re: Numstrat - Stumbling into Lua
« Reply #6 on: April 04, 2012, 08:52:28 pm »
I redefined some variables, I think is more legible this way:

Code: [Select]
--[[Things to do:Declare variables first within a functionMake things more fun and complexPoints System:OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}Any better ways? Maybe make Atk more risky, so it's less about luck]]--chance = { [-1] = "",            [0] = "unsuccessful . -",            [1] = "successful . +" }action = { nothing="", attack="Attack", defense="Defense"}function initialize_variables()    v = 0    t = 0    r1 = 0    r2 = -1    state = action.nothingend--function on.create()initialize_variables()--endfunction on.paint(gc)    gc:setFont("sansserif","r",11)    gc:setColorRGB(0,0,0)       gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")    gc:drawString(v,10,30,"top")    if state == action.attack then        gc:drawString("Attack was "  .. chance[r2] .. r1, 10, 50, "top")    elseif state == action.defense then        gc:drawString("Defense was " .. chance[r2] .. r1, 10, 50, "top")    end    gc:drawString("Turn " .. t,200,10,"top")    if v~=0 or t~=0 then        gc:drawString("Average " .. round(v/t,2), 200, 30, "top")    end    gc:setFont("sansserif","r",8)    gc:drawString("Numstrat - Jason Ho",10,200,"top")endfunction on.charIn(ch)    if ch=="a" then        state = action.attack        r1=math.random(4,5)        r2=math.random(0,1)        if r2==1 then            v=v+r1        else    --if r2==0 then            v=v-r1        end        t=t+1    end    elseif ch=="d" then        state = action.defense        r1=math.random(1,3)        r2=math.random(0,1)        if r2==1 then            v=v+r1        else    --if r2==0 then            v=v-r1        end        state=action.defense        t=t+1    end    elseif ch=="r" then        initialize_variables()    end    platform.window:invalidate()endfunction round(value, digits)    return string.format("%." .. digits .. "f", value)end
« Last Edit: April 08, 2012, 02:22:41 pm by someone »

#### Jonius7

• python! Lua!
• LV10 31337 u53r (Next: 2000)
• Posts: 1918
• Rating: +82/-18
• Still bringing new dimensions to the TI-nspire...
##### Re: Numstrat - Stumbling into Lua
« Reply #7 on: April 07, 2012, 07:34:18 pm »
Thanks someone, though I'm don't completely understand sections of it.
So in your version "state" plays an important part here. and I also see "chance" defined as a table?
 Programmed some CASIO Basic in the pastDJ Omnimaga Music Discographist DJ Omnimaga DiscographyMy Own Music!
My Released Projects (Updated 2015/05/08)
 TI-nspire BASICTI-nspire Hold 'em Health Bar Scissors Paper Rock TI-nspire LuaNumstrat TI-nspire Hold 'em LuaTransport ChooserSecret Project (at v0.08.2 - 2015/05/08) Spoiler For Extra To-Be-Sorted Clutter: Spoiler For Relegated Projects: TI-nspire BASICBattle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)Axe ParserDoodle God (stalled while I go and learn some Axe)

#### someone

• LV3 Member (Next: 100)
• Posts: 49
• Rating: +9/-0
##### Re: Numstrat - Stumbling into Lua
« Reply #8 on: April 08, 2012, 02:20:22 pm »
"action" was used to classify which actions can be performed when you press a key. I'm not sure if you intend to add more command actions (like flee, heal, steal, etc.), but if you do, you can now easily add them on the table.

"state", behaves as a filter event. It keeps track of the action executed. Then you can control in your program what to do when something happened.

"chance" was created to reduce the amount of lines. Since attack & defense behave alike, you can group them. I added the blank one just in case you want to create something like a "miss" or so. This is an ordered table, so when the variable r2 = 1, chance[r2] is the same as "successful . +"

http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables

#### linuxgeek96

• LV3 Member (Next: 100)
• Posts: 99
• Rating: +4/-0
• ( ͡° ͜ʖ ͡°)
##### Re: Numstrat - Stumbling into Lua
« Reply #9 on: April 08, 2012, 04:56:47 pm »
any easy way to rotate a table 90 degrees?

#### cyanophycean314

• LV6 Super Member (Next: 500)
• Posts: 363
• Rating: +43/-1
• It's You!
##### Re: Numstrat - Stumbling into Lua
« Reply #10 on: April 08, 2012, 05:58:45 pm »
any easy way to rotate a table 90 degrees?

What exactly do you mean by that?

You can transpose matrices I guess.

#### DJ Omnimaga

• Former TI programmer
• CoT Emeritus
• LV15 Omnimagician (Next: --)
• Posts: 55882
• Rating: +3151/-232
• CodeWalrus founder & retired Omnimaga founder
##### Re: Numstrat - Stumbling into Lua
« Reply #11 on: April 08, 2012, 06:03:29 pm »
From what I remember this was possible in TI-83+ BASIC, although it was rather slow and would only let you rotate the matrix in one direction (meanign rotating the opposite way took 3 times more time, as you had to use the transpose command 3 times), so I wouldn't be surprised if it was possible on the Nspire too.

#### Jonius7

• python! Lua!
• LV10 31337 u53r (Next: 2000)
• Posts: 1918
• Rating: +82/-18
• Still bringing new dimensions to the TI-nspire...
##### Re: Numstrat - Stumbling into Lua
« Reply #12 on: April 08, 2012, 08:23:00 pm »
"action" was used to classify which actions can be performed when you press a key. I'm not sure if you intend to add more command actions (like flee, heal, steal, etc.), but if you do, you can now easily add them on the table.

"state", behaves as a filter event. It keeps track of the action executed. Then you can control in your program what to do when something happened.

"chance" was created to reduce the amount of lines. Since attack & defense behave alike, you can group them. I added the blank one just in case you want to create something like a "miss" or so. This is an ordered table, so when the variable r2 = 1, chance[r2] is the same as "successful . +"

http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables

Thanks someone, it seems you know a great deal about Lua. Have you got or are planning any Lua projects yourself?

Also it would probably be possible to transpose matrices, you would somehow do a for .... do command to transpose x and y coordinates of each value in the matrix over to the new coordinates.
 Programmed some CASIO Basic in the pastDJ Omnimaga Music Discographist DJ Omnimaga DiscographyMy Own Music!
My Released Projects (Updated 2015/05/08)
 TI-nspire BASICTI-nspire Hold 'em Health Bar Scissors Paper Rock TI-nspire LuaNumstrat TI-nspire Hold 'em LuaTransport ChooserSecret Project (at v0.08.2 - 2015/05/08) Spoiler For Extra To-Be-Sorted Clutter: Spoiler For Relegated Projects: TI-nspire BASICBattle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)Axe ParserDoodle God (stalled while I go and learn some Axe)

#### someone

• LV3 Member (Next: 100)
• Posts: 49
• Rating: +9/-0
##### Re: Numstrat - Stumbling into Lua
« Reply #13 on: April 09, 2012, 04:05:48 pm »
Well, I've been reading some documentation regarding LUA & Nspire LUA, so I know it more or less. I would say like at intermediate level...

As for projects, I recently remade a Nonogram program, that you can check on its topic:
http://ourl.ca/15731

#### cyanophycean314

• LV6 Super Member (Next: 500)
• Posts: 363
• Rating: +43/-1
• It's You!
##### Re: Numstrat - Stumbling into Lua
« Reply #14 on: April 11, 2012, 02:18:20 am »
I think he's talking about your own work. I know you've been doing some edits, optimizations, and remakes, but you could try making your own project.

As a suggestion, you could try making a Lua sudoku. It'd be easy enough and it'd be pretty nice.