Skip to content

Instantly share code, notes, and snippets.

@dcampbell24
Created July 23, 2012 06:51
Show Gist options
  • Save dcampbell24/3162325 to your computer and use it in GitHub Desktop.
Save dcampbell24/3162325 to your computer and use it in GitHub Desktop.
Function manipulating int8 values
# Returns the new cell index from the specified cell in the
# specified direction. The index is only valid if the
# starting cell and direction have been checked by the
# out_of_bounds function first.
function shift(cell_::Int8, dir::Int8)
div5_mod2(x) = (x - 1)/5 % 2 != 0
if dir == E
return int8(cell_ + 1)
elseif dir == ESE
if div5_mod2(cell_)
return int8(cell_ + 7)
else
return int8(cell_ + 6)
end
elseif dir == SE
if div5_mod2(cell_)
return int8(cell_ + 6)
else
return int8(cell_ + 5)
end
elseif dir == S
return int8(cell_ + 10)
elseif dir == SW
if div5_mod2(cell_)
return int8(cell_ + 5)
else
return int8(cell_ + 4)
end
elseif dir == WSW
if div5_mod2(cell_)
return int8(cell_ + 4)
else
return int8(cell_ + 3)
end
elseif dir == W
return int8(cell_ - 1)
elseif dir == WNW
if div5_mod2(cell_)
return int8(cell_ - 6)
else
return int8(cell_ - 7)
end
elseif dir == NW
if div5_mod2(cell_)
return int8(cell_ - 5)
else
return int8(cell_ - 6)
end
elseif dir == N
return int8(cell_ - 10)
elseif dir == NE
if div5_mod2(cell_)
return int8(cell_ - 4)
else
return int8(cell_ - 5)
end
elseif dir == ENE
if div5_mod2(cell_)
return int8(cell_ - 3)
else
return int8(cell_ - 4)
end
end
return cell_
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment