Created
August 7, 2013 09:16
-
-
Save Tyderion/6172500 to your computer and use it in GitHub Desktop.
Directions for turtlebot within minecraft.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Directions | |
local Direction = {} | |
Direction.left = "left" | |
Direction.right = "right" | |
Direction.forward = "forward" | |
Direction.back = "back" | |
Direction.up = "up" | |
Direction.down = "down" | |
Direction.isValid = function(direction) | |
local dir = string.lower(direction) | |
return dir == Direction.left or | |
dir == Direction.right or | |
dir == Direction.forward or | |
dir == Direction.back or | |
dir == Direction.up or | |
dir == Direction.down | |
end | |
Direction.new = function(initial) | |
local self = { | |
current = Direction.left | |
} | |
if Direction.isValid(initial) then | |
self.current = initial | |
end | |
function self.opposite() | |
switch = { | |
[Direction.left] = function() return Direction.right end, | |
[Direction.right]= function() return Direction.left end, | |
[Direction.up]= function() return Direction.down end, | |
[Direction.down]= function() return Direction.up end, | |
[Direction.forward]= function() return Direction.back end, | |
[Direction.back]= function() return Direction.forward end, | |
} | |
return switch[self.current]() | |
end | |
return self | |
end | |
t = Direction.new("left") | |
print(t.current) | |
print(t.opposite()) | |
t = Direction.new("up") | |
print(t.current) | |
print(t.opposite()) | |
t = Direction.new("forward") | |
print(t.current) | |
print(t.opposite()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment