everything fixed. next step is to implement a function that checks if somebody lost

This commit is contained in:
Nico
2025-08-20 00:15:20 +02:00
parent 25ec371c07
commit 65e738bd41
3 changed files with 58 additions and 31 deletions

View File

@@ -17,7 +17,7 @@ function LoadBlackjack()
Deck = Karten:createDeck() Deck = Karten:createDeck()
Karten:shuffleDeck(Deck) Karten:shuffleDeck(Deck)
DealInitialCards() DealInitialCards()
Cards = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png") Cards = love.graphics.newImage("/cards_asset_pack/JustNormalCardsSwitched.png")
end end
function UpdateBlackjack(dt) function UpdateBlackjack(dt)
@@ -28,39 +28,61 @@ function DrawBlackjack()
local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 } local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 }
local tileWidth = Cards:getWidth() / 13 local tileWidth = Cards:getWidth() / 13
local tileHeight = Cards:getHeight() / 4 local tileHeight = Cards:getHeight() / 4
local scale = 0.5
local drawW, drawH = tileWidth * scale, tileHeight * scale
local spacingX, spacingY = 1, 1
for _, card in pairs(PlayerHand) do local function drawHandGrid(hand, basePos)
local tileX = card.value - 2 if #hand == 0 then
local tileY = suits[card.suit] return
love.graphics.draw( end
Cards,
love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, Cards:getDimensions()), local cols = 3
PlayerHandPosition.x - tileWidth / 4, local rows = math.ceil(#hand / cols)
PlayerHandPosition.y,
0, for row = 0, rows - 1 do
0.5 local startIdx = row * cols + 1
) local endIdx = math.min(startIdx + cols - 1, #hand)
local cardsInRow = endIdx - startIdx + 1
local rowWidth = cardsInRow * drawW + (cardsInRow - 1) * spacingX
local startX = basePos.x - rowWidth / 2
for j = 0, cardsInRow - 1 do
local i = startIdx + j
local card = hand[i]
local tileX = math.min(card.value - 2, 12)
local tileY = suits[card.suit] or 0
local quad = love.graphics.newQuad(
tileX * tileWidth,
tileY * tileHeight,
tileWidth,
tileHeight,
Cards:getDimensions()
)
local x = startX + j * (drawW + spacingX)
local y = basePos.y + row * (drawH + spacingY)
love.graphics.draw(Cards, quad, x, y, 0, scale, scale)
end
end
end end
love.graphics.print(GetHandValue(PlayerHand), PlayerHandPosition.x, PlayerHandPosition.y + tileHeight + 5) drawHandGrid(PlayerHand, PlayerHandPosition)
love.graphics.print(
GetHandValue(PlayerHand),
PlayerHandPosition.x,
PlayerHandPosition.y + (math.ceil(#PlayerHand / 3) * drawH) + 15
)
for _, card in pairs(DealerHand) do drawHandGrid(DealerHand, DealerHandPosition)
suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 } love.graphics.print(
tileWidth = Cards:getWidth() / 13 GetHandValue(DealerHand),
tileHeight = Cards:getHeight() / 4 DealerHandPosition.x,
local tileX = math.min(card.value - 2, 9) DealerHandPosition.y + (math.ceil(#DealerHand / 3) * drawH) + 15
local tileY = suits[card.suit] )
love.graphics.draw(
Cards,
love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, Cards:getDimensions()),
DealerHandPosition.x - tileWidth / 4,
DealerHandPosition.y,
0,
0.5
)
end
love.graphics.print(GetHandValue(DealerHand), DealerHandPosition.x, DealerHandPosition.y + tileHeight + 5)
end end
function GetHandValue(hand) function GetHandValue(hand)
@@ -70,11 +92,15 @@ function GetHandValue(hand)
for _, card in pairs(hand) do for _, card in pairs(hand) do
if card.value < 10 then if card.value < 10 then
count = count + card.value count = count + card.value
print("+" .. card.value)
elseif card.value < 14 then elseif card.value < 14 then
count = count + 10 count = count + 10
print("+" .. card.value)
else else
count = count + 11 count = count + 11
print("+" .. 11)
aces = aces + 1 aces = aces + 1
print("+ 1 ace = " .. aces)
end end
end end
@@ -86,6 +112,7 @@ function GetHandValue(hand)
goto aceloop goto aceloop
end end
end end
print("end------------")
return count return count
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@@ -5,7 +5,7 @@ Karten = {}
function Karten:createDeck() function Karten:createDeck()
local deck = {} local deck = {}
local suits = { "Clubs", "Diamonds", "Spades", "Hearts" } local suits = { "Clubs", "Diamonds", "Spades", "Hearts" }
local values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 } -- 10, J, Q, K sind alle 10, A ist 11 local values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }
for _, suit in ipairs(suits) do for _, suit in ipairs(suits) do
for _, value in ipairs(values) do for _, value in ipairs(values) do
table.insert(deck, { suit = suit, value = value }) table.insert(deck, { suit = suit, value = value })