Files
LuckyShroom/blackjack.lua
2025-08-21 22:47:50 +02:00

197 lines
4.1 KiB
Lua

local love = require("love")
require("karten")
PlayerHand = {}
DealerHand = {}
GameState = "playing" -- "playing", "player_won", "dealer_won", "tie"
function LoadBlackjack()
DealerHandPosition = {
x = love.graphics.getWidth() / 2,
y = love.graphics.getHeight() / 4,
}
PlayerHandPosition = {
x = love.graphics.getWidth() / 2,
y = love.graphics.getHeight() / 4 * 2.5,
}
Deck = Karten:createDeck()
Karten:shuffleDeck(Deck)
DealInitialCards()
Cards = love.graphics.newImage("/cards_asset_pack/JustNormalCardsSwitched.png")
end
function UpdateBlackjack(dt)
dt = dt * 1
end
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 = 10, 10
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
drawHandGrid(PlayerHand, PlayerHandPosition)
love.graphics.print(
GetHandValue(PlayerHand),
PlayerHandPosition.x,
PlayerHandPosition.y + (math.ceil(#PlayerHand / 3) * drawH) + 15
)
drawHandGrid(DealerHand, DealerHandPosition)
love.graphics.print(
GetHandValue(DealerHand),
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
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
function HandleBlackjackInput(key)
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
end
if key == "r" then
ResetGame()
end
end
function DealInitialCards()
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