diff --git a/blackjack.lua b/blackjack.lua index 175081e..c9f7e4a 100644 --- a/blackjack.lua +++ b/blackjack.lua @@ -17,7 +17,7 @@ function LoadBlackjack() Deck = Karten:createDeck() Karten:shuffleDeck(Deck) DealInitialCards() - Cards = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png") + Cards = love.graphics.newImage("/cards_asset_pack/JustNormalCardsSwitched.png") end function UpdateBlackjack(dt) @@ -28,39 +28,61 @@ function DrawBlackjack() local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 } local tileWidth = Cards:getWidth() / 13 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 tileX = card.value - 2 - local tileY = suits[card.suit] - love.graphics.draw( - Cards, - love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, Cards:getDimensions()), - PlayerHandPosition.x - tileWidth / 4, - PlayerHandPosition.y, - 0, - 0.5 - ) + local function drawHandGrid(hand, basePos) + if #hand == 0 then + return + end + + local cols = 3 + local rows = math.ceil(#hand / cols) + + for row = 0, rows - 1 do + 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 - 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 - suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 } - tileWidth = Cards:getWidth() / 13 - tileHeight = Cards:getHeight() / 4 - local tileX = math.min(card.value - 2, 9) - 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) + drawHandGrid(DealerHand, DealerHandPosition) + love.graphics.print( + GetHandValue(DealerHand), + DealerHandPosition.x, + DealerHandPosition.y + (math.ceil(#DealerHand / 3) * drawH) + 15 + ) end function GetHandValue(hand) @@ -70,11 +92,15 @@ function GetHandValue(hand) 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 @@ -86,6 +112,7 @@ function GetHandValue(hand) goto aceloop end end + print("end------------") return count end diff --git a/cards_asset_pack/JustNormalCardsSwitched.png b/cards_asset_pack/JustNormalCardsSwitched.png new file mode 100644 index 0000000..225b3ad Binary files /dev/null and b/cards_asset_pack/JustNormalCardsSwitched.png differ diff --git a/karten.lua b/karten.lua index cbaca82..78ca337 100644 --- a/karten.lua +++ b/karten.lua @@ -5,7 +5,7 @@ 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 } -- 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 _, value in ipairs(values) do table.insert(deck, { suit = suit, value = value })