Guest User

CribbageLibrary

a guest
Jul 16th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.83 KB | None | 0 0
  1. local module = {}
  2. --local cards = {"d5", "h4", "c10", "s10", "h1", "d4"}
  3. local combos = { {1,2},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5},{3,4},{3,5},{4,5},
  4. {1,2,3},{1,2,4},{1,2,5},{1,3,4},{1,3,5},{1,4,5},{2,3,4},{2,3,5},{2,4,5},{3,4,5},
  5. {1,2,3,4},{1,2,3,5},{1,2,4,5},{1,3,4,5},{2,3,4,5},{1,2,3,4,5} }
  6. local tossCombos = {
  7.     {1,2,3,4},{1,2,3,5},{1,2,3,6},{1,2,4,5},{1,2,4,6},{1,2,5,6},{1,3,4,5},{1,3,4,6},{1,3,5,6},
  8.     {1,4,5,6},{2,3,4,5},{2,3,4,6},{2,3,5,6},{2,4,5,6},{3,4,5,6}
  9.     }
  10. -----------------------------------------------------------------------------
  11. function fixCards(cards,justNumbers) --justNumbers is by default, true
  12.     local newCardsTable = {}
  13.     local justNumbers = not justNumbers
  14.     for i,v in pairs(cards) do
  15.         table.insert(newCardsTable,(justNumbers and tonumber((v:gsub("%a",""))) or v    )         )
  16.     end
  17.     table.sort(newCardsTable)
  18.     return newCardsTable
  19. end
  20. function getPointsFrom15(cards)
  21.     local pointsFrom15 = 0
  22.     for a,n in pairs(combos) do
  23.         local num = 0
  24.         for i,v in pairs(n) do
  25.             local card = cards[v]
  26.             card = (card and card >= 10 and 10 or card)
  27.             num = (card and num + card or 0)
  28.         end
  29.     pointsFrom15 = (num == 15 and (pointsFrom15 + 2) or pointsFrom15)
  30.     end
  31.     return pointsFrom15
  32. end
  33.  
  34. function getPointsFromPairs(cards)
  35.     local pointsFromPairs = 0
  36.     for a,n in pairs(cards) do
  37.         for i,v in pairs(cards) do
  38.             pointsFromPairs = (((v == n) and a~=i) and (pointsFromPairs + 1) or pointsFromPairs)
  39.             --it counts each pair twice, so only add 1
  40.         end
  41.     end
  42.     return pointsFromPairs
  43. end
  44.  
  45. function runWasCounted(runsCounted,newRun)
  46.     local blank,timesOccurred = runsCounted:gsub(newRun,"")
  47.     if timesOccurred >= 1 then
  48.         return true
  49.     else
  50.         return false
  51.     end
  52. end
  53.  
  54. function getPointsFromRuns(cards)
  55.     --print(table.concat(cards,', '))
  56.     local points = 0
  57.     local runsCounted = ""
  58.     for a = 1, #cards do
  59.         for b = (a + 1), #cards do
  60.             for c = (b + 1), #cards do
  61.                 for d = (c + 1), #cards do
  62.                     if (cards[a] + 1 == cards[b] and cards[b] + 1 == cards[c] and cards[c] + 1 == cards[d]) then
  63.                         points = points + 4
  64.                         runsCounted = runsCounted..a..b..c..d..","
  65.                     end
  66.                 end
  67.                 points = ( (cards[a] + 1 == cards[b] and cards[b] + 1 == cards[c] and not runWasCounted(runsCounted,a..b..c) ) and (points + 3) or points)
  68.             end
  69.         end
  70.     end
  71.     if (cards[1] + 1 == cards[2] and cards[2] + 1 == cards[3] and cards[3] + 1 == cards[4] and cards[4] + 1 == cards[5]) then
  72.         points = 5     
  73.     end
  74.     return points
  75. end
  76. function allSuitsAreTheSame(cards)
  77.     local suit = cards[1]:gsub("%d","")
  78.     local points = 0
  79.     for a = 1,4 do
  80.         print(cards[a])
  81.         local cardSuit = cards[a]:gsub("%d","")
  82.         points = (suit == cardSuit and points + 1 or points)
  83.     end
  84.     local lastCardSuit = (cards[5] and cards[5]:gsub("%d","") or nil)
  85.     points = (points == 4 and points or 0)
  86.     points = ((points == 4 and lastCardSuit and suit == lastCardSuit) and (points + 1) or points)
  87.     return points
  88. end
  89. function getKnobs(cards)
  90.     local jackSuits = {}
  91.     for i = 1, 4 do
  92.         if tonumber((cards[i]:gsub("%a",""))) == 11 then --if card is jack
  93.             local jackSuit = cards[i]:gsub("%d","")
  94.             table.insert(jackSuits,jackSuit)
  95.         end
  96.     end
  97.     for a = 1, #jackSuits do
  98.         local suit = (cards[5] and cards[5]:gsub("%d","") or nil)
  99.         if suit and jackSuits[a] == suit then
  100.             return 1
  101.         end
  102.     end
  103.     return 0
  104. end
  105. function module:getPoints(cards,index,place)
  106.     local bonus = allSuitsAreTheSame(cards)
  107.     local knobs = getKnobs(cards)
  108.     local cards = fixCards(cards)
  109.     --[[
  110.     print("Points from suits: "..bonus)
  111.     print("Points from knobs: "..knobs)
  112.     print("Points from making 15: "..getPointsFrom15(cards))
  113.     print("Points from pairs: "..getPointsFromPairs(cards))
  114.     print("Points from Runs: "..getPointsFromRuns(cards)) --]]
  115.      
  116.     --script:ClearAllChildren()
  117.     --local newPointsValue = Instance.new("IntValue", place)
  118.     --newPointsValue.Name = "Value"..index
  119.     --print(getPointsFrom15(cards) + getPointsFromPairs(cards) + getPointsFromRuns(cards) + bonus + knobs)
  120.     return (getPointsFrom15(cards) + getPointsFromPairs(cards) + getPointsFromRuns(cards) + bonus + knobs)
  121. end
  122.  
  123. function getExpectedValue(cards,knownCards,theValuesShouldBePrinted) --4 cards
  124.     local t={"h","d","s","c"}
  125.     --print("#cards = "..#cards)
  126.     local function differentCardsCheck(knownCards,card2BChecked)
  127.         for a,v in pairs(knownCards) do
  128.             if v == card2BChecked then
  129.                 return false
  130.             end
  131.         end
  132.         return true
  133.     end
  134.     local ExpectedValue = 0
  135.     for a=1,13 do
  136.         for b=1,4 do
  137.             if differentCardsCheck(knownCards,(t[b]..a)) then --if it is an unknown card
  138.                 cards[5] = (t[b]..a)
  139.                 if theValuesShouldBePrinted then
  140.                     print(table.concat(cards,", "))
  141.                     print((module:getPoints(cards)))
  142.                 end
  143.                 ExpectedValue = (module:getPoints(cards) * (1/46)) + ExpectedValue
  144.             end
  145.         end
  146.     end
  147.    
  148.     cards[5] = nil
  149.     return ExpectedValue
  150. end
  151.  
  152. function module:tossCards2Crib(cards,placeForValue) --cribOwner = true for self, cribOwner = false for oppoenent
  153.     table.sort(cards, function(a, b) return tonumber((a:gsub("%a",""))) > tonumber((b:gsub("%a",""))) end) 
  154.     local bestHand = {}
  155.     bestHand["Points1"] = 0
  156.     --[[local ExpectedValues = {}
  157.     local function round3Places(x)
  158.             return (x + 0.0005 - (x + 0.0005)  % .001)
  159.     end--]]
  160.     local index = 2
  161.     for a,v in pairs(tossCombos) do
  162.         local handPoints = getExpectedValue( { cards[v[1]],cards[v[2]],cards[v[3]],cards[v[4]] },cards )
  163.         --table.insert(ExpectedValues,round3Places(handPoints))
  164.         if bestHand["Points1"] < handPoints  then
  165.             bestHand["Points1"] = handPoints
  166.             bestHand["Combo1"] = a
  167.            
  168.             --[[for a = 2,15 do
  169.                 bestHand["Combo"..a] = nil
  170.                 bestHand["Points"..a] = nil
  171.             end
  172.         elseif bestHand["Points1"] == handPoints then
  173.             bestHand["Points"..index] = handPoints
  174.             bestHand["Combo"..index] = a
  175.             index = index + 1
  176.              --]]
  177.         end
  178.     end
  179.     --[[
  180.     local besthands = 0
  181.     for a,v in pairs(bestHand) do
  182.         besthands = besthands + .5
  183.     end
  184.     print("BestHandLength: "..besthands)
  185.     --]]
  186.    
  187.     local keepCards = {cards[tossCombos[bestHand["Combo1"]][1]],cards[tossCombos[bestHand["Combo1"]][2]],cards[tossCombos[bestHand["Combo1"]][3]],cards[tossCombos[bestHand["Combo1"]][4]]}
  188.     for a=4,1,-1 do
  189.         table.remove(cards,tossCombos[bestHand["Combo1"]][a])
  190.     end
  191.     --local tossCards = fixCards(cards,true)
  192.     --  for a,v in pairs(tossCombos[bestHand["Combo1"]]) do --{1,3,5,6}
  193.     --      table.remove(tossCards,v)
  194.     --  end
  195.     --print(getExpectedValue(keepCards,cards)) --,true
  196.    
  197.     local info = Instance.new("IntValue")
  198.     info.Name = "CardInstructions"
  199.     local keepCardInfo = Instance.new("StringValue")
  200.     local tossCardInfo = Instance.new("StringValue")
  201.     keepCardInfo.Name = "keepCards"
  202.     tossCardInfo.Name = "tossCards"
  203.     keepCardInfo.Value = game.HttpService:JSONEncode(keepCards)
  204.     tossCardInfo.Value = game.HttpService:JSONEncode(cards)
  205.    
  206.     info.Parent = placeForValue
  207.     keepCardInfo.Parent = info
  208.     tossCardInfo.Parent = info
  209.     return keepCards,cards --second value is tossCards
  210. end
  211. return module
Advertisement
Add Comment
Please, Sign In to add comment