63 lines
1.1 KiB
Lua
63 lines
1.1 KiB
Lua
local love = require("love")
|
|
|
|
Karten = {}
|
|
|
|
function Karten:createDeck()
|
|
local deck = {}
|
|
local suits = { "Clubs", "Diamonds", "Spades", "Hearts" }
|
|
local values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }
|
|
for _, suit in ipairs(suits) do
|
|
for _, value in ipairs(values) do
|
|
table.insert(deck, { suit = suit, value = value })
|
|
end
|
|
end
|
|
|
|
TileMap = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png")
|
|
return deck
|
|
end
|
|
|
|
function Karten:shuffleDeck(deck)
|
|
for i = #deck, 2, -1 do
|
|
local j = love.math.random(i)
|
|
deck[i], deck[j] = deck[j], deck[i]
|
|
end
|
|
end
|
|
|
|
function Karten:hit(hand)
|
|
if #Deck > 0 then
|
|
local card = table.remove(Deck)
|
|
table.insert(hand, card)
|
|
return card
|
|
end
|
|
end
|
|
|
|
|
|
function Karten:getHandValue(hand)
|
|
local count = 0
|
|
local aces = 0
|
|
|
|
for _, card in pairs(hand) do
|
|
if card.value < 10 then
|
|
count = count + card.value
|
|
elseif card.value < 14 then
|
|
count = count + 10
|
|
else
|
|
count = count + 11
|
|
aces = aces + 1
|
|
end
|
|
end
|
|
|
|
::aceloop::
|
|
if count > 21 and aces > 0 then
|
|
for _ = 1, aces do
|
|
count = count - 10
|
|
aces = aces - 1
|
|
goto aceloop
|
|
end
|
|
end
|
|
|
|
return count
|
|
end
|
|
|
|
return Karten
|