This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
android4682's profile picture

Short out certain parameters

Started by android4682, 20 January 2014 - 04:06 AM
android4682 #1
Posted 20 January 2014 - 05:06 AM
I'm wondering if I could shorten the next sentence.


if bullet == dead or bullet == dead2 or bullet == dead3 or bullet == dead4 or bullet == dead5 then

I just got a feeling that this can be shorter.

Thanks,
Android
theoriginalbit #2
Posted 20 January 2014 - 05:11 AM
in order to better help you we will require further context and/or code.

for example what data does all the `dead` variables contain?
android4682 #3
Posted 20 January 2014 - 05:15 AM
in order to better help you we will require further context and/or code.

for example what data does all the `dead` variables contain?

Here is the full (unfinished) script.
I'm trying to build a sort of Russian Roulette kind of thing.

Line 254-258 = dead
Line 260 = bullet

Line 341 = if statement
Edited on 20 January 2014 - 04:19 AM
OReezy #4
Posted 20 January 2014 - 05:35 AM
There are a couple ways you could do this with your code. If you simple want to check if something is in a list of variables you can make a table and use a loop to check it like this
local table = {"var1","var2","var3","etc"}
for i=1,#table do
  if input==table[i] then
	--do something
	break
  end
end

For your code I'd recommend simply checking if it isn't equal to 6.
if bullet ~= 6 then
robhol #5
Posted 20 January 2014 - 05:35 AM

if compare == true then
....
elseif compare == false then

== true is kind of ugly, and also pointless - just "if compare then" will execute the block as long as compare isn't either false or nil. That also makes "elseif" redundant. Just use else.

monitor.write("") 
As far as I know, this does nothing? print has a newline, write doesn't.


shell.run("Russian_Roulette")
No. Bad. Just don't. Do this with a while loop around the main program logic, you can break or return out of it when the program should end. Or Ctrl-T.


		monitor.setCursorPos(3,1)
		monitor.write("-*-*-*-")
		monitor.setCursorPos(3,2)
		monitor.write("*-*-*-*")
		monitor.setCursorPos(3,3)
		monitor.write("You Win!")
		monitor.setCursorPos(3,4)
		monitor.write("*-*-*-*")
		monitor.setCursorPos(3,5)
		monitor.write("-*-*-*-")
For loop. Also, monitor function calls take up a LOT of your code. I've only been skimming, but look for ways you can move these long strings of calls into separate functions, this'll make your code cleaner and much more appealing/easier to read.


monitor = peripheral.wrap("top")
You could make "top" a variable instead, and put it somewhere close to the top of the script. Not necessarily… necessary, but it would make it easier to change later on.


		dead1 = "1"
		dead2 = "2"
		dead3 = "3"
		dead4 = "4"
		dead5 = "5"
Use tables/arrays.


if bullet == dead or bullet == dead2 or bullet == dead3 or bullet == dead4 or bullet == dead5 then
As far as I can see (just skimming the code), this compares the number "bullet" to the strings from before. the if block will never execute.

Suggestion for "realism": the bullet is always in one chamber. bullet should be a number from 1-6 like now. However, you also spin the barrel, so a variable "chamber" should also be randomly generated. Then, you try to shoot. If chamber == bullet then *splat*, otherwise "click" and cycle to the next chamber.

You might want to take a look at http://www.lua.org/pil/contents.html
Edited on 20 January 2014 - 04:37 AM
android4682 #6
Posted 20 January 2014 - 05:58 AM
Thanks for the help guys :)/>
I will look into all of this.
android4682 #7
Posted 20 January 2014 - 06:28 AM
There are a couple ways you could do this with your code. If you simple want to check if something is in a list of variables you can make a table and use a loop to check it like this
local table = {"var1","var2","var3","etc"} for i=1,#table do if input==table[i] then --do something break end end
For your code I'd recommend simply checking if it isn't equal to 6.
if bullet ~= 6 then

Thanks that helped me out pretty well.

P.S. How to close a topic?
Edited on 20 January 2014 - 05:31 AM
theoriginalbit #8
Posted 20 January 2014 - 06:33 AM
P.S. How to close a topic?
you can't only moderators can; they generally don't close Ask a Pro topics. also its better to keep it open incase you have future problems with this same code, you can just continue to post here.
android4682 #9
Posted 20 January 2014 - 07:47 AM
P.S. How to close a topic?
you can't only moderators can; they generally don't close Ask a Pro topics. also its better to keep it open incase you have future problems with this same code, you can just continue to post here.

Hmm nice to know.
Thanks