massacring

Hand

Jun 21st, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. local Card = require("Card")
  2. local Hand = {}
  3. Hand.__index = Hand
  4.  
  5. function Hand.new(includeJoker)
  6.     local hand = setmetatable({}, Hand)
  7.     hand.cards = {}
  8.     hand.cardAmount = 0
  9.     hand:addCard(Card.newRandom(includeJoker))
  10.     hand:addCard(Card.newRandom(includeJoker))
  11.     return hand
  12. end
  13.  
  14. function Hand:addCard(card)
  15.     table.insert(self.cards, card)
  16.     self.cardAmount = self.cardAmount + 1
  17. end
  18.  
  19. function Hand:getCards()
  20.     return self.cards
  21. end
  22.  
  23. function Hand:getValue()
  24.     local total = 0
  25.     local aces = {}
  26.     for _, card in pairs(self.cards) do
  27.         if card.isAce then
  28.             table.insert(aces, card)
  29.         else
  30.             total = total + card.value
  31.         end
  32.     end
  33.     for i=1,#aces,1 do
  34.         if total + #aces - i <= 10 then
  35.             total = total + 11
  36.         else
  37.             total = total + 1
  38.         end
  39.     end
  40.     return total
  41. end
  42.  
  43. function Hand:evaluateHand()
  44.     local totalValue = self:getValue()
  45.     if totalValue == 21 and self.cardAmount == 2 then
  46.         -- BlackJack
  47.         return 22
  48.     end
  49.     if totalValue > 21 then
  50.         -- Bust
  51.         return 0
  52.     end
  53.     return totalValue
  54. end
  55.  
  56. function Hand:draw(window, width, y, first)
  57.     local amount = self.cardAmount
  58.     local startX = width / 2 - 7 - (9 * (amount-1))
  59.     if amount > 3 then
  60.         startX = startX + math.floor((9 * (amount-2)) / 2) + 2
  61.     end
  62.     local cards = self.cards
  63.     for i, card in ipairs(cards) do
  64.         local boost = (16 * (i-1))
  65.         if i < amount-1 then
  66.             boost = (7 * (i-1))
  67.         elseif amount > 3 then
  68.             boost = (7 * (amount - 3)) + (16 * (2 - (amount - i)))
  69.         end
  70.         local x = startX + boost
  71.         if i == amount and first then
  72.             card:drawFaceDown(window, x, y)
  73.         else
  74.             card:draw(window, x, y)
  75.         end
  76.     end
  77. end
  78.  
  79. return Hand
Add Comment
Please, Sign In to add comment