init
This commit is contained in:
44
karten.lua
Normal file
44
karten.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
karten = {}
|
||||
|
||||
function createDeck()
|
||||
local deck = {}
|
||||
local suits = {"Herz", "Karo", "Pik", "Kreuz"}
|
||||
local values = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11} -- 10, J, Q, K sind alle 10, A ist 11
|
||||
for _, suit in ipairs(suits) do
|
||||
for _, value in ipairs(values) do
|
||||
table.insert(deck, {suit = suit, value = value})
|
||||
end
|
||||
end
|
||||
return deck
|
||||
end
|
||||
|
||||
function 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 hit(hand)
|
||||
local card = table.remove(deck)
|
||||
table.insert(hand, card)
|
||||
end
|
||||
|
||||
function handValue(hand)
|
||||
local value = 0
|
||||
local aces = 0
|
||||
for _, card in ipairs(hand) do
|
||||
value = value + card.value
|
||||
if card.value == 11 then
|
||||
aces = aces + 1
|
||||
end
|
||||
end
|
||||
while value > 21 and aces > 0 do
|
||||
value = value - 10
|
||||
aces = aces - 1
|
||||
end
|
||||
return value
|
||||
end
|
||||
|
||||
return karten
|
||||
|
||||
Reference in New Issue
Block a user