diff --git a/blackjack.lua b/blackjack.lua index decfd10..12c30e4 100644 --- a/blackjack.lua +++ b/blackjack.lua @@ -1,53 +1,86 @@ -local love = require "love" -require "karten" +local love = require("love") +require("karten") -spielerHand = {} +PlayerHand = {} DealerHand = {} -Deck = {} function LoadBlackjack() - Deck = Karten:createDeck() - Karten:shuffleDeck(Deck) - DealInitialCards() - Cards = love.graphics.newImage("/cards_asset_pack/CuteCards.png") + 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/CuteCards.png") end function UpdateBlackjack(dt) - -- Hier können zukünftige Updates für Blackjack hinzugefügt werden + dt = dt * 1 end function DrawBlackjack() - print(love.graphics:getWidth()) - love.graphics.draw(Cards, love.graphics.getWidth() / 2 - 90 , 50, 0, 0.2) - -- love.graphics.print("Spieler Hand: " .. Karten:handValue(spielerHand), 50, 50) - -- love.graphics.print("Dealer Hand: " .. Karten:handValue(DealerHand), 50, 100) - -- love.graphics.print("Drücke 'H' für Hit oder 'S' für Stand", 50, 150) + for i, card in ipairs(PlayerHand) do + local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 } + local tileWidth = Cards:getWidth() / 15 + local tileHeight = Cards:getHeight() / 4 + local tileX = suits[card.suit] + local tileY = math.min(card.value - 2, 9) + love.graphics.draw( + Cards, + love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, Cards:getDimensions()), + PlayerHandPosition.x - tileWidth / 4, + PlayerHandPosition.y, + 0, + 0.5 + ) + end + for i, card in ipairs(DealerHand) do + local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 } + local tileWidth = Cards:getWidth() / 15 + local tileHeight = Cards:getHeight() / 4 + local tileX = suits[card.suit] + local tileY = math.min(card.value - 2, 9) + 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 end + function HandleBlackjackInput(key) - if key == 'h' then - Karten:hit(spielerHand) - if Karten:handValue(spielerHand) > 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 - love.graphics.print("Dealer hat verloren!", 50, 200) - elseif Karten:handValue(spielerHand) > Karten:handValue(DealerHand) then - love.graphics.print("Spieler gewinnt!", 50, 200) - else - love.graphics.print("Dealer gewinnt!", 50, 200) - end - end + 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(spielerHand) - Karten:hit(DealerHand) - end + for i = 1, 2 do + Karten:hit(PlayerHand) + Karten:hit(DealerHand) + end end - diff --git a/cards_asset_pack/JustNormalCards.png b/cards_asset_pack/JustNormalCards.png new file mode 100644 index 0000000..8be7be1 Binary files /dev/null and b/cards_asset_pack/JustNormalCards.png differ diff --git a/karten.lua b/karten.lua index d5330ce..b4cddc4 100644 --- a/karten.lua +++ b/karten.lua @@ -1,56 +1,50 @@ -local love = require "love" +local love = require("love") + Karten = {} function Karten:createDeck() - local deck = {} - local suits = {"Clubs", "Diamonds", "Spades", "Hearts"} - local values = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11} -- 10, J, Q, K sind alle 10, A ist 11 - for _, suit in ipairs(suits) do - for _, value in ipairs(values) do - table.insert(deck, {suit = suit, value = value}) - end - end + local deck = {} + local suits = { "Clubs", "Diamonds", "Spades", "Hearts" } + local values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 } -- 10, J, Q, K sind alle 10, A ist 11 + for _, suit in ipairs(suits) do + for _, value in ipairs(values) do + table.insert(deck, { suit = suit, value = value }) + end + end - TileMap = love.graphics.newImage("/cards_asset_pack/CuteCards.png") - return deck + TileMap = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png") + return deck end function Karten:shuffleDeck(deck) - for i = #deck, 2, -1 do - local j = love.math.random(i) - deck[i], deck[j] = deck[j], deck[i] - end + for i = #deck, 2, -1 do + local j = love.math.random(i) + deck[i], deck[j] = deck[j], deck[i] + 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 + 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(x, y, hand) - if #Deck > 0 then - local card = table.remove(Deck) - table.insert(hand, card) - local suits = {["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3} - local tileWidth = TileMap:getWidth() / 4 - local tileHeight = TileMap:getHeight() / 13 - local tileX = suits[card.suit] - local tileY = math.min(card.value - 2, 9) - love.graphics.draw(TileMap, love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, TileMap:getDimensions()), x, y) - end +function Karten:hit(hand) + if #Deck > 0 then + local card = table.remove(Deck) + table.insert(hand, card) + return card + end end - - return Karten