0 Members and 1 Guest are viewing this topic.
--[[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
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 :)
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 fastera 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]
matrix= { {1,2,3}, {4,5,6}, {7,8,9}}print(matrix[2][3]) -- prints 6
--[[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
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 tableshttp://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables