--[[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
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 :)
[/code]
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()
[/code]
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 :)
matrix= {
{1,2,3},
{4,5,6},
{7,8,9}
}
print(matrix[2][3]) -- prints 6
--[[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]]--
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.nothing
end
--function on.create()
initialize_variables()
--end
function 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")
end
function 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()
end
function round(value, digits)
return string.format("%." .. digits .. "f", value)
end
any easy way to rotate a table 90 degrees?
"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 . +"
This link might be helpful for understanding more about how to use tables
http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables (http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables)
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. :D
For card games you might learn a lot from chockosta. He made the freecell clone, which is really nice and runs perfect. dragable cards, checking for values and freee places etc.
--[[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]]--
-- Call this function whenever you want to set the variables to their original values
function initialize_variables()
v = 0
t = 0
r1 = 0
r2 = -1
state = ""
end
--function on.create()
initialize_variables()
--end
function 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 == "attack" then
if r2 == 1 then
gc:drawString("Attack was successful . + " .. r1, 10, 50, "top")
else --if r2 == 0 then
gc:drawString("Attack was unsuccessful . -" .. r1, 10, 50, "top")
end
elseif state == "defense" then
if r2 == 1 then
gc:drawString("Defense was successful . + " .. r1, 10, 50, "top")
else --if r2 == 0 then
gc:drawString("Defense was unsuccessful . -" .. r1, 10, 50, "top")
end
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")
end
function on.charIn(ch)
if ch=="a" then
state = "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
elseif ch=="d" then
state = "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
t=t+1
elseif ch=="r" then
initialize_variables()
end
platform.window:invalidate()
end
--This function rounds the value passed depending on the number of digits needed
function round(value, digits)
return string.format("%." .. digits .. "f", value)
end
if state == "attack" then
if r2 == 1 then
gc:drawString("Attack was successful . + " .. r1, 10, 50, "top")
else --if r2 == 0 then
gc:drawString("Attack was unsuccessful . -" .. r1, 10, 50, "top")
end
elseif state == "defense" then
if r2 == 1 then
gc:drawString("Defense was successful . + " .. r1, 10, 50, "top")
else --if r2 == 0 then
gc:drawString("Defense was unsuccessful . -" .. r1, 10, 50, "top")
end
end
if state ~= "" then
gc:drawString((state=="attack" and "Attack" or "Defense") .. " was " .. (r2==0 and "unsuccessful. -" or "successful. +") .. r1, 10, 50, "top")
end
------ Screen Manager -------
-- In this table we will contain all 'screen' objects.
Screens = {}
-- This function is used to push a screen into the above table (Screens)
function pushScreen(scrn)
table.insert(Screens, scrn)
platform.window:invalidate()
end
-- This function is used to remove the last item in the 'Screens' table
function pullScreen()
table.remove(Screens)
platform.window:invalidate()
end
-- Return the last item of 'Screens', the current screen
function currentScreen()
return Screens[#Screens]
end
-- This function will loop through all screens in the 'Screens' table and draw them
function paintScreens(gc)
for _, screen in ipairs(Screens) do
screen:paint(gc)
end
end
-- Our screen object class
Screen = class()
function Screen:arrowKey() end
function Screen:paint() end
function Screen:charIn() end
function Screen:enterKey() end
-- Link the Screen manager to 'on'
function on.paint(gc) paintScreens(gc) end
function on.arrowKey(arrow) currentScreen():arrowKey(arrow) end
function on.charIn(ch) currentScreen():charIn(ch) end
function on.enterKey() currentScreen():enterKey() end
--- User code ----
popup = Screen()
function popup:paint(gc)
gc:setColorRGB(255,255,255)
gc:fillRect(50,50,200,100)
gc:setColorRGB(0,0,0)
gc:drawRect(50,50,200,100)
gc:drawString("Hey!", 52, 50, "top")
gc:drawString("Press enter to hide this popup", 52, 80, "top")
end
function popup:enterKey()
pullScreen()
end
main = Screen()
function main:paint(gc)
gc:drawString("Hello World! Press enter :D", 10, 10, "top")
end
function main:enterKey()
pushScreen(popup)
end
pushScreen(main)
Levak made an excellent Lua sudoku btw (quite advanced/complex Nspire Lua though) available here :
http://levak.free.fr/ftp/nspire/Sudoku/ (http://levak.free.fr/ftp/nspire/Sudoku/)
But using the sourcecode (.lua) and compiling with Luna I cannot get the code to run at all withHave you followed the correct order of source-concatenation described in http://levak.free.fr/ftp/nspire/Sudoku/MakeFile.sh ?
Error: attempt to call global 'PushScreen' (a nil value)
maketns.py Sudokut.tns Buttons.lua Menu.lua Request.lua Constants.lua ScreenManager.lua Engine.lua Help.lua Sudoku.lua
maketns Sudokut.tns Buttons.lua Menu.lua Request.lua Constants.lua ScreenManager.lua Engine.lua Help.lua Sudoku.lua
Well, you should always try to avoid that your script throws errors to the user level (so that you need to catch it with registerErrorHandler). Of course it's not bad to implement it in case that something goes wrong unexpectedly, but still a well designed script should never actually be able to arrive to that point.Of course :)