Skip to content

Instantly share code, notes, and snippets.

@tachoknight
Created December 3, 2020 14:26
Show Gist options
  • Save tachoknight/87bc5e3fd37ed9f950032eec1f61fcae to your computer and use it in GitHub Desktop.
Save tachoknight/87bc5e3fd37ed9f950032eec1f61fcae to your computer and use it in GitHub Desktop.
Lua function for TableTop Simulator to figure out which player gets an extra card
--[[
This lua script is for a TableTop Simulator game I'm working on called
"May I" that is similar to Rummy. In the game 12 cards are dealt to
all the players, with the player to the left of the dealer getting an
extra card. This is my solution at getting it working, and also as
a way to learn more Lua.
--]]
-- Fixed color array
colors = {}
colors[1] = "white"
colors[2] = "brown"
colors[3] = "red"
colors[4] = "orange"
colors[5] = "yellow"
colors[6] = "green"
colors[7] = "teal"
colors[8] = "blue"
colors[9] = "purple"
-- This is the order of players
players = {}
players[1] = "purple"
players[2] = "orange"
players[3] = "white"
players[4] = "green"
--[[
O
W G
P
purple should give extra to white
white should give extra to orange
orange should give extra to green
green should give extra to purple
--]]
local function isempty(s)
return s == nil or s == ''
end
local player = "purple"
local totalColorCount = 0
for k,v in pairs(colors) do
totalColorCount = totalColorCount + 1
end
print("There are " .. totalColorCount .. " colors")
local playerColorPos = 1
for k,v in pairs(colors) do
if v == player then
break
else
playerColorPos = playerColorPos + 1
end
end
print("start num is " .. playerColorPos)
-- now we need to know where to start for the next pass
local nextNum = 0
if playerColorPos == totalColorCount then
nextNum = 1
else
nextNum = playerColorPos + 1
end
print("Next num is " .. nextNum)
local skipNum = 1
local nextColor
for k, v in pairs(colors) do
if skipNum >= nextNum then
for pk, pv in pairs(players) do
if v == pv then
nextColor = pv
break
end
end
if isempty(nextColor) == false then
break
end
else
skipNum = skipNum + 1
end
end
print("the next color should be " .. nextColor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment