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

Sgt_Flexxx's Custom House Calculator/Builder(Builder WIP)

Started by sgt_flexxx, 10 September 2012 - 09:40 PM
sgt_flexxx #1
Posted 10 September 2012 - 11:40 PM
Hello!

This is my first "real" program, and by real I mean not completely useless.
I'm not sure if this has been done before, probably has though, but whatever. Practice makes perfect.
This program will tell you how many blocks you need to build a house by using a slightly modified Surface Area equation. So let's say that you put this

Length:4
Width:6
Heighth:10(Doesn't include floor)

Then it would calculate exactly how many blocks(Including Roof and Floor) you need to build a house with these dimensions.



Im still working on the builder for it, find it a little complicated, but I'm confident.
For now, ignore what it says about building, and just type "no" if it prompts to ask you if you want to build.

Here's the code!


function iGotEnough()
if response=="yes" then
print("Initiating building stage")
Build()
end
if response=="no" then
print("OK, stopping program")
end
end

function Total()
l=length-1
w=width-1
h=heighth-1
base1=2*(w*h)
base2=2*(l*h)
roofAndFloor=2*(length*width)
total=base1+base2+roofAndFloor
print("I need "..total.." blocks to make a house with these dimensions")
end

function getTotalItemCount()
local a="I currently have "
local b=" blocks"
ItemAmount=0
for f=1,16 do
  ItemAmount=ItemAmount+turtle.getItemCount(f)
end
  print()
  print(a..ItemAmount.. :D/>/>
end

function RequiredAmount()
  if ItemAmount >= total then
  write("I have enought blocks to build, proceed?:")
  response=read()
  iGotEnough()
end
  if ItemAmount < total then
  print("I don't have enough blocks, please insert the correct amount of blocks and run this program again.")
end
end
function ReadyUp()
print("All sides must exceed 2")
write("Enter Length:")
length=read()
write("Enter Width:")
width=read()
write("Enter Heighth:")
heighth=read()
end

function FloorAndRoof()
turtle.forward()
turtle.forward()
for m=1,width do
turtle.turnRight()
turtle.forward()
turtle.turnRight()
for n=1,length do
turtle.digDown()
turtle.placeDown()
turtle.forward()
end
end
end

function Build()
FloorAndRoof()
end


ReadyUp()
Total()
getTotalItemCount()
RequiredAmount()

Please tell me if the code is actually giving the right amount of blocks plz!
GopherAtl #2
Posted 11 September 2012 - 01:59 AM
if the height is blocks above ground level and the floor is being dug and placed AT ground level, then I believe your numbers are correct. Good luck with the build code :D/>/>
sgt_flexxx #3
Posted 11 September 2012 - 02:16 AM
if the height is blocks above ground level and the floor is being dug and placed AT ground level, then I believe your numbers are correct. Good luck with the build code :D/>/>

Yep, that's how it works :D/>/>
sgt_flexxx #4
Posted 11 September 2012 - 02:19 AM
btw, (since im new), is there a way to have functions only act on odd/even numbers? Because I want(for ex) a=1,16. I want it to turn right on even, and turn left on odd. I would do "if a==1,3,5,7,etc,etc,etc" but that wouldn't work because the input from the person running the program could type anything(big numbers)
D3matt #5
Posted 11 September 2012 - 03:35 AM
Please wrap your code in
[code] brackets
to make it easier to read and copy/paste.

Also, does this account for windows and doors, or just the faces of a rectangular prism of that size?
sgt_flexxx #6
Posted 11 September 2012 - 03:38 AM
Please wrap your code in
[code] brackets
to make it easier to read and copy/paste.

Also, does this account for windows and doors, or just the faces of a rectangular prism of that size?

No, unfortunately, I think it might be a little impossible to account for doors and windows, seeing as the house can be whatever size the person using the program wants it to be, plus I cannot predict how many windows/doors the user will put when the house is completed.

Just accounts for a rectangular prism
PROdotes #7
Posted 11 September 2012 - 02:04 PM
btw, (since im new), is there a way to have functions only act on odd/even numbers? Because I want(for ex) a=1,16. I want it to turn right on even, and turn left on odd. I would do "if a==1,3,5,7,etc,etc,etc" but that wouldn't work because the input from the person running the program could type anything(big numbers)

you can do what i do…

function even(number)
while number > 1 do number = number - 2 end
if number == 1 then return false else return true end
end

so even(5) will return false, while even(6) will return true :D/>/>

or if you want to make it odd() just replace the number == 1 with number == 0 :D/>/>

also… why the complication in number of blocks? You need the outer cube minus the inner cube… so (length*width*height)-((length-2)*(width-2)*(height-2))

example: 3x3x3 room is 3*3*3(27) minus 1*1*1(1) so 26 blocks…
4x4x4 room is 4*4*4(64) minus 2*2*2(8) so 56…
screenshot: 4*6*11(264) minus 2*4*9(72) so 192…

height is increased by 1 since you don't count the floor… i do :)/>/>
GopherAtl #8
Posted 11 September 2012 - 02:28 PM
oh my. Yes, you COULD do that. Or you use %, which returns the remainder of division. Odd basically means not divisible by two, so any odd number % 2 == 1. Any even number % 2 == 0.


function isEven(number)
  return number%2==1 and false or true
end
PROdotes #9
Posted 11 September 2012 - 04:36 PM
oh my. Yes, you COULD do that. Or you use %, which returns the remainder of division. Odd basically means not divisible by two, so any odd number % 2 == 1. Any even number % 2 == 0.


function isEven(number)
  return number%2==1 and false or true
end

or that… sadly, I don't speak lua and when looking for a mod operator my google-fu was too weak… ty ^^
only thing i found was math.mod() or something… that didn't work ^^
sgt_flexxx #10
Posted 11 September 2012 - 08:20 PM
oh my. Yes, you COULD do that. Or you use %, which returns the remainder of division. Odd basically means not divisible by two, so any odd number % 2 == 1. Any even number % 2 == 0.


function isEven(number)
  return number%2==1 and false or true
end

or that… sadly, I don't speak lua and when looking for a mod operator my google-fu was too weak… ty ^^
only thing i found was math.mod() or something… that didn't work ^^

Yea, math.mod was my first attempt, it didn't work, even found it in "non-working functions" in the wiki
sgt_flexxx #11
Posted 11 September 2012 - 08:24 PM
oh my. Yes, you COULD do that. Or you use %, which returns the remainder of division. Odd basically means not divisible by two, so any odd number % 2 == 1. Any even number % 2 == 0.


function isEven(number)
  return number%2==1 and false or true
end

I am trying that. Especially when I use my
for m=1,16 do

I tried

while m%2==1 do
something

but it will just give back an error about doing arithmetic on a nil value. m should be 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, right? As it progresses? So it shouldn't be a nil value.

Sorry if my explanation sucks, I suck at explaining stuff :/
PROdotes #12
Posted 11 September 2012 - 10:01 PM

for m = 1, 5 do
if m%2 == 1 then print("odd") else print("even") end
end

this gives me
odd, even, odd, even, odd

so it works… only reason it won't work would be if you didn't define m yet…