Files
LuckyShroom/blackjack.lua

146 lines
3.3 KiB
Lua

local love = require("love")
require("karten")
PlayerHand = {}
DealerHand = {}
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 = 1, 1
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
)
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
::aceloop::
if count > 21 and aces > 0 then
for _ = 1, aces do
count = count - 10
aces = aces - 1
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)
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
end
function DealInitialCards()
for i = 1, 2 do
Karten:hit(PlayerHand)
Karten:hit(DealerHand)
end
end