Skip to content

Instantly share code, notes, and snippets.

@germandiagogomez
Created May 24, 2021 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germandiagogomez/9ed37e49d7512f5372097f0af3be27d6 to your computer and use it in GitHub Desktop.
Save germandiagogomez/9ed37e49d7512f5372097f0af3be27d6 to your computer and use it in GitHub Desktop.
Free functions in ChaiScript
def nextPlayer(player) {
return ((player + 1) % 4)
}
// typed (optional)
def nextPlayer2(int player) {
return ((player + 1) % 4)
}
def getPlayerPositionRelativeTo(refPlayer, targetPlayer) {
if (refPlayer == targetPlayer) {
return BottomPlayer
}
var diff = 0
var curr = nextPlayer(refPlayer)
++diff
while (curr != targetPlayer) {
curr = nextPlayer(curr)
++diff
}
if (diff == 0) {
return BottomPlayer
}
else if (diff == 1) {
return LeftPlayer
}
else if (diff == 2) {
return FrontPlayer
}
else if (diff == 3) {
return RightPlayer
}
else {
throw("Error calculating the player position. This is a bug")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment