added winConditionCheck

This commit is contained in:
Nico
2025-08-21 22:47:50 +02:00
parent 65e738bd41
commit df9299cefa
2 changed files with 102 additions and 39 deletions

View File

@@ -3,6 +3,7 @@ require("karten")
PlayerHand = {}
DealerHand = {}
GameState = "playing" -- "playing", "player_won", "dealer_won", "tie"
function LoadBlackjack()
DealerHandPosition = {
@@ -30,7 +31,7 @@ function DrawBlackjack()
local tileHeight = Cards:getHeight() / 4
local scale = 0.5
local drawW, drawH = tileWidth * scale, tileHeight * scale
local spacingX, spacingY = 1, 1
local spacingX, spacingY = 10, 10
local function drawHandGrid(hand, basePos)
if #hand == 0 then
@@ -83,24 +84,36 @@ function DrawBlackjack()
DealerHandPosition.x,
DealerHandPosition.y + (math.ceil(#DealerHand / 3) * drawH) + 15
)
-- show winner if game is not running
if GameState ~= "playing" then
local message = ""
if GameState == "player_won" then
message = "Player won!"
elseif GameState == "dealer_won" then
message = "Dealer won!"
elseif GameState == "tie" then
message = "Tie!"
end
love.graphics.setColor(1, 1, 0)
love.graphics.print(message, love.graphics.getWidth() / 2 - 100, 100)
love.graphics.setColor(1, 1, 1)
end
end
function GetHandValue(hand)
local count = 0
local aces = 0
for _, card in pairs(hand) do
if card.value < 10 then
count = count + card.value
print("+" .. card.value)
elseif card.value < 14 then
count = count + 10
print("+" .. card.value)
else
count = count + 11
print("+" .. 11)
aces = aces + 1
print("+ 1 ace = " .. aces)
end
end
@@ -112,34 +125,72 @@ function GetHandValue(hand)
goto aceloop
end
end
print("end------------")
return count
end
function HandleBlackjackInput(key)
if key == "h" then
Karten:hit(PlayerHand)
if Karten:handValue(PlayerHand) > 21 then
love.graphics.print("Spieler hat verloren!", 50, 200)
if GameState == "playing" then
if key == "h" then
Karten:hit(PlayerHand)
CheckWinCondition()
elseif key == "s" then
-- Dealer zieht bis 17 oder höher
while GetHandValue(DealerHand) < 17 do
Karten:hit(DealerHand)
end
CheckWinCondition()
end
elseif key == "s" then
-- while Karten:handValue(DealerHand) < 17 do
Karten:hit(DealerHand)
-- end
-- if Karten:handValue(DealerHand) > 21 then
-- print("Dealer hat verloren!", 50, 200)
-- elseif Karten:handValue(PlayerHand) > Karten:handValue(DealerHand) then
-- print("Spieler gewinnt!", 50, 200)
-- else
-- print("Dealer gewinnt!", 50, 200)
-- end
end
if key == "r" then
ResetGame()
end
end
function DealInitialCards()
for i = 1, 2 do
for _ = 1, 2 do
Karten:hit(PlayerHand)
Karten:hit(DealerHand)
end
end
function CheckWinCondition()
if GameState ~= "playing" then
return
end
local playerValue = GetHandValue(PlayerHand)
local dealerValue = GetHandValue(DealerHand)
if dealerValue < 17 then
return
end
if playerValue > 21 then
GameState = "dealer_won"
return
end
if dealerValue > 21 then
GameState = "player_won"
return
end
if playerValue > dealerValue then
GameState = "player_won"
elseif dealerValue > playerValue then
GameState = "dealer_won"
else
GameState = "tie"
end
end
function ResetGame()
PlayerHand = {}
DealerHand = {}
GameState = "playing"
Deck = Karten:createDeck()
Karten:shuffleDeck(Deck)
DealInitialCards()
end

View File

@@ -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