added winConditionCheck
This commit is contained in:
44
karten.lua
44
karten.lua
@@ -23,22 +23,6 @@ function Karten:shuffleDeck(deck)
|
||||
end
|
||||
end
|
||||
|
||||
function Karten: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
|
||||
|
||||
function Karten:hit(hand)
|
||||
if #Deck > 0 then
|
||||
local card = table.remove(Deck)
|
||||
@@ -47,4 +31,32 @@ function Karten:hit(hand)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user