Calculator Community > Lua

Numstrat - Stumbling into Lua

(1/8) > >>

Jonius7:
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: -----[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points 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=0
t=0
r1=0
r2=2
r3=0
r4=2
function 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")
end
function 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
--- End code ---

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!

Nick:
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 :)

Jonius7:
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.

Nick:
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: ---function on.create()
     vars = {0, 0, 0, 2, 0, 2}
end

--this must in the if ch=="a" thingie

if vars[3] = 0 then
     vars[1] = vars[1]+1
end
[code]

this might be a bit more timeconsuming to type, but it think it's more beautiful to see, rather than 5 vars :)
 
--- End code ---
[/code]

Jonius7:

--- Quote from: Nick 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: ---function on.create()
     vars = {0, 0, 0, 2, 0, 2}
end

--this must in the if ch=="a" thingie

if vars[3] = 0 then
     vars[1] = vars[1]+1
end
[code]

this might be a bit more timeconsuming to type, but it think it's more beautiful to see, rather than 5 vars :)
 
--- End code ---
[/code]

--- End quote ---

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 :D

Navigation

[0] Message Index

[#] Next page

Go to full version