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

Help with mini-game

Started by EpicTreeMiner, 14 January 2013 - 12:25 PM
EpicTreeMiner #1
Posted 14 January 2013 - 01:25 PM
This is a game i've been working on. Its Similar to a claw machine :P/> [link]http://images.monste...ine-700x700.jpg[/link]
Anyway what it does is move to where the user says, checks if the block below is obsidian (can change), and if it is, it returns and prints true (a prize will be implemented later)
The problem is that i get no errors but it doesnt return to its original position.

Please help

Code:
Spoiler
local xPos = 1
local zPos = 1
local yPos = 1
local q = xPos - 1
local w = zPos - 1
local e = yPos - 1

print("How far forward?")
local x = tonumber( read() )
term.clear()
term.setCursorPos(1,1)
print("How far left?")
local z = tonumber( read() )
term.clear()
term.setCursorPos(1,1)
print("How far down?")
local y = tonumber( read() )
term.clear()

function xMove()
for i = 1,(x) do
  xPos = xPos + 1
  turtle.forward()
end
end
function zMove()
turtle.turnLeft()
for i = 1,(z) do
  zPos = zPos + 1
  turtle.forward()
end
turtle.turnRight()
end
function yMove()
for i = 1,(y) do
  yPos = yPos + 1
  turtle.down()
end
end
print("Moving...")
xMove()
zMove()
yMove()
function goBack()
for i = 1,(q) do
  turtle.back()
end
turtle.turnLeft()
for i = 1,(w) do
  turtle.back()
end
turtle.turnRight()
for i = 1,(e) do
  turtle.up()
end
end
if turtle.compareDown(1) true then
print("Well done!")
else
print("Try again...")
end
goBack()

Setup:
http://www.flickr.co.../in/photostream
http://www.flickr.co.../in/photostream
http://www.flickr.co.../in/photostream

Simplified - its a turtle in a glass room C:
crazyguymgd #2
Posted 14 January 2013 - 01:28 PM
Does it return to somewhere near the original positions? And if you tell it to go to a different place than the obsidean, does it return to that same, wrong position?
EpicTreeMiner #3
Posted 14 January 2013 - 01:36 PM
Does it return to somewhere near the original positions? And if you tell it to go to a different place than the obsidean, does it return to that same, wrong position?
It returns to the position it was in when you started the programs, or well at least it should. thats the problem
theoriginalbit #4
Posted 14 January 2013 - 01:42 PM
Firstly, for next time this post is in the wrong section, for assistance with problems you should post in the "ask a pro" section…

So I'm assuming the problem comes about when its unloaded mid process?
crazyguymgd #5
Posted 14 January 2013 - 02:00 PM
Does it return to somewhere near the original positions? And if you tell it to go to a different place than the obsidean, does it return to that same, wrong position?
It returns to the position it was in when you started the programs, or well at least it should. thats the problem
So does it just sit above the obsidean and never actually runs the goback function? Or does it run the goback function but returns to a position that is 1 x coord too far? If it does go somewhere, but the wrong place, just tinker with the values in the goback function.
Lyqyd #6
Posted 14 January 2013 - 05:34 PM
Moved to Ask a Pro.
EpicTreeMiner #7
Posted 14 January 2013 - 10:30 PM
Moved to Ask a Pro.
thanks
ChunLing #8
Posted 15 January 2013 - 06:53 AM
It looks like if the y entered is too large, the turtle will stall against the block it encounters, racking up the counter regardless. This would lead to the go back function failing…if it did anything anyway. Which it doesn't, because q,w, and e are all initialized to 0 (1-1) and you never change that.

Try a function structured like:
function yMove()
    for i = 1,(y) do
        if turtle.down() then yPos = i else break end
    end
end
This way the move will only increment yPos for as long as the move succeeds. Then change your goBack() function so that it uses those modified position values for loop limits rather than having them all set to zero.
Edited on 15 January 2013 - 05:58 AM