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

Please clarify code - kind of lost

Started by 25FiveDelta, 21 January 2014 - 11:12 PM
25FiveDelta #1
Posted 22 January 2014 - 12:12 AM
Please help me understand the highlighted code, especially relationship between "xDiff[orientation]" and "xDiff = {0, 1, 0, -1}" I understand how he got the xDiff table.

http://www.youtube.com/watch?v=S6k88NnotM4



xCoord = 8809
yCoord = 65
zCoord = -2582

orientation = 4
orientations ={"North", "East", "South", "West"}
zDiff = {-1, 0, 1, 0}
xDiff = {0, 1, 0, -1}

function left()
   orientation = orientation - 1
   orientation = (orientation -1) % 4
   orientation = orientation + 1
   turtle.turnLeft()
end

function right()
   orientation = orientation - 1
   orientation = (orientation +1) % 4
   orientation = orientation + 1
   turtle.turnRight()
end

function moveForward()
   xCoord = xCoord + xDiff[orientation]
   zCoord = zCoord + zDiff[orientation]

  turtle.dig()
  moved = false
  while not(moved) do
  moved = turtle.forward()
end

end
  for i = 1, 3 do
  moveForward()
  print("X: ".. xCoord.. " Z: "..zCoord.. "Y: "..yCoord)
end
Bomb Bloke #2
Posted 22 January 2014 - 01:15 AM
This:

xDiff = {0, 1, 0, -1}

… defines a table such that:

xDiff[1] = 0
xDiff[2] = 1
xDiff[3] = 0
xDiff[4] = -1

Thus, if "orientation" is equal to 2, then xDiff[orientation] will be equal to 1. If "orientation" is equal to 4, then xDiff[orientation] will be equal to -1. And so on.

In this way, when moving the turtle forward the script knows how much to add or subtract from xCoord or zCoord, based on "orientation"'s current value.

For example, if "orientation" is 1, then that represents the turtle facing north. Moving forward will subtract one from zCoord and add zero to xCoord (leaving the latter unaltered).
25FiveDelta #3
Posted 22 January 2014 - 01:43 AM
Great, I really appreciate the clear explanation and the example. It's now very clear.


Thanks
surferpup #4
Posted 22 January 2014 - 11:30 PM
I created a turtle and loaded this program into it. Specifically, I was interested in what happened to the North,South,East and West orientation as reported by the program as I turned the turtle. So, I focused on the left() function. I would have expected that as I turned the turtle left 360 degrees, the program would report an original orientation of West, then South, East, North and finally West again.

Setup code ( I used the orientation variable, the orientations table, and the exact same left() function. I added a test which simply turns the turtle left 4 times and prints the orientation after each turn.


orientation = 4
orientations ={"North", "East", "South", "West"}
function left()
   orientation = orientation - 1
   orientation = (orientation -1) % 4
   orientation = orientation + 1
   turtle.turnLeft()
end

-- Test Code

print ("Original orientation = "..orientations[orientation])
for i = 1, 4 do
  left()
  print (tostring(i).."..."..orientations[orientation])
end

Here is the output:


Original orientation = West
1…South
2…East
3…North
4…West

And this is exactly correct. Then, I decided to see why this worked.

When the turtle turns left, the orientation variable goes through essentially four changes:
  • decrement by 1
  • decrement by 1
  • modulus 4
  • increment 1

orientation = orientation - 1
orientation = (orientation -1) % 4
orientation = orientation + 1

Creating a table of these changes, one can see the results of each modification:


[[
   -1  -1  %4  +1
4   3   2   2   3 South
3   2   1   1   2 East
2   1   0   0   1 North
1   0  -1   3   4 West
]]

I thought that was pretty neat, even though many here probably already figured that out. I had been under the mistaken impression that -1 % 4 (-1 modulus 4) would have been 1, but it turns out it is 3. This is a great application of the modulus (remainder) function.
Edited on 22 January 2014 - 10:51 PM
Bomb Bloke #5
Posted 22 January 2014 - 11:53 PM
Consider what the rest of the code would look like if this:

orientations ={"North", "East", "South", "West"}

… produced a table along the lines of this:

orientations[0] = "North"
orientations[1] = "East"
orientations[2] = "South"
orientations[3] = "West"

… as it would under eg Java.

The answer is one of the reasons why many coders prefer the use of zero-based numbering when it comes to indexing arrays and tables and such.
surferpup #6
Posted 23 January 2014 - 12:00 AM
The answer is one of the reasons why many coders prefer the use of zero-based numbering when it comes to indexing arrays and tables and such.

True. However, indexing tables by number is merely one way Lua handles table referencing. The flexibility offered in Lua is both liberating and maddening in the same breath. Adjusting thinking from zero-based indexing to one-based indexing is not all that difficult.