Compare commits
4 Commits
686f6d8962
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df9299cefa | ||
|
|
65e738bd41 | ||
|
|
25ec371c07 | ||
|
|
1c2e6e5ca2 |
213
blackjack.lua
213
blackjack.lua
@@ -1,53 +1,196 @@
|
|||||||
local love = require "love"
|
local love = require("love")
|
||||||
require "karten"
|
require("karten")
|
||||||
|
|
||||||
spielerHand = {}
|
PlayerHand = {}
|
||||||
DealerHand = {}
|
DealerHand = {}
|
||||||
Deck = {}
|
GameState = "playing" -- "playing", "player_won", "dealer_won", "tie"
|
||||||
|
|
||||||
function LoadBlackjack()
|
function LoadBlackjack()
|
||||||
Deck = Karten:createDeck()
|
DealerHandPosition = {
|
||||||
Karten:shuffleDeck(Deck)
|
x = love.graphics.getWidth() / 2,
|
||||||
DealInitialCards()
|
y = love.graphics.getHeight() / 4,
|
||||||
Cards = love.graphics.newImage("/cards_asset_pack/CuteCards.png")
|
}
|
||||||
|
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
|
end
|
||||||
|
|
||||||
function UpdateBlackjack(dt)
|
function UpdateBlackjack(dt)
|
||||||
-- Hier können zukünftige Updates für Blackjack hinzugefügt werden
|
dt = dt * 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function DrawBlackjack()
|
function DrawBlackjack()
|
||||||
print(love.graphics:getWidth())
|
local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 }
|
||||||
love.graphics.draw(Cards, love.graphics.getWidth() / 2 - 90 , 50, 0, 0.2)
|
local tileWidth = Cards:getWidth() / 13
|
||||||
-- love.graphics.print("Spieler Hand: " .. Karten:handValue(spielerHand), 50, 50)
|
local tileHeight = Cards:getHeight() / 4
|
||||||
-- love.graphics.print("Dealer Hand: " .. Karten:handValue(DealerHand), 50, 100)
|
local scale = 0.5
|
||||||
-- love.graphics.print("Drücke 'H' für Hit oder 'S' für Stand", 50, 150)
|
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
|
end
|
||||||
|
|
||||||
function HandleBlackjackInput(key)
|
function HandleBlackjackInput(key)
|
||||||
if key == 'h' then
|
if GameState == "playing" then
|
||||||
Karten:hit(spielerHand)
|
if key == "h" then
|
||||||
if Karten:handValue(spielerHand) > 21 then
|
Karten:hit(PlayerHand)
|
||||||
love.graphics.print("Spieler hat verloren!", 50, 200)
|
CheckWinCondition()
|
||||||
end
|
elseif key == "s" then
|
||||||
elseif key == 's' then
|
-- Dealer zieht bis 17 oder höher
|
||||||
while Karten:handValue(DealerHand) < 17 do
|
while GetHandValue(DealerHand) < 17 do
|
||||||
Karten:hit(DealerHand)
|
Karten:hit(DealerHand)
|
||||||
end
|
end
|
||||||
if Karten:handValue(DealerHand) > 21 then
|
CheckWinCondition()
|
||||||
love.graphics.print("Dealer hat verloren!", 50, 200)
|
end
|
||||||
elseif Karten:handValue(spielerHand) > Karten:handValue(DealerHand) then
|
end
|
||||||
love.graphics.print("Spieler gewinnt!", 50, 200)
|
|
||||||
else
|
if key == "r" then
|
||||||
love.graphics.print("Dealer gewinnt!", 50, 200)
|
ResetGame()
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function DealInitialCards()
|
function DealInitialCards()
|
||||||
for i = 1, 2 do
|
for _ = 1, 2 do
|
||||||
Karten:hit(spielerHand)
|
Karten:hit(PlayerHand)
|
||||||
Karten:hit(DealerHand)
|
Karten:hit(DealerHand)
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|||||||
BIN
cards_asset_pack/JustNormalCards.png
Normal file
BIN
cards_asset_pack/JustNormalCards.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
BIN
cards_asset_pack/JustNormalCardsSwitched.png
Normal file
BIN
cards_asset_pack/JustNormalCardsSwitched.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
74
flake.lock
generated
74
flake.lock
generated
@@ -1,9 +1,47 @@
|
|||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
|
"cursor": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1747112672,
|
||||||
|
"narHash": "sha256-kUkbIQwb65KiMaxWVTtLmSdgpE/ANFquJ1OLKlZNjZs=",
|
||||||
|
"owner": "omarcresp",
|
||||||
|
"repo": "cursor-flake",
|
||||||
|
"rev": "52061a31dd7fcdfc04e8c4abfeaadf67039324c9",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "omarcresp",
|
||||||
|
"ref": "main",
|
||||||
|
"repo": "cursor-flake",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"flake-utils": {
|
"flake-utils": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"systems": "systems"
|
"systems": "systems"
|
||||||
},
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1710146030,
|
||||||
|
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-utils_2": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems_2"
|
||||||
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1731533236,
|
"lastModified": 1731533236,
|
||||||
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
@@ -19,6 +57,22 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1725103162,
|
||||||
|
"narHash": "sha256-Ym04C5+qovuQDYL/rKWSR+WESseQBbNAe5DsXNx5trY=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "12228ff1752d7b7624a54e9c1af4b222b3c1073b",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs_2": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1753694789,
|
"lastModified": 1753694789,
|
||||||
"narHash": "sha256-cKgvtz6fKuK1Xr5LQW/zOUiAC0oSQoA9nOISB0pJZqM=",
|
"narHash": "sha256-cKgvtz6fKuK1Xr5LQW/zOUiAC0oSQoA9nOISB0pJZqM=",
|
||||||
@@ -36,8 +90,9 @@
|
|||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake-utils": "flake-utils",
|
"cursor": "cursor",
|
||||||
"nixpkgs": "nixpkgs"
|
"flake-utils": "flake-utils_2",
|
||||||
|
"nixpkgs": "nixpkgs_2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"systems": {
|
"systems": {
|
||||||
@@ -54,6 +109,21 @@
|
|||||||
"repo": "default",
|
"repo": "default",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"systems_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"root": "root",
|
"root": "root",
|
||||||
|
|||||||
@@ -3,8 +3,9 @@
|
|||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
flake-utils.url = "github:numtide/flake-utils";
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
cursor.url = "github:omarcresp/cursor-flake/main";
|
||||||
};
|
};
|
||||||
outputs = { self, nixpkgs, flake-utils }:
|
outputs = { self, nixpkgs, flake-utils, cursor }:
|
||||||
flake-utils.lib.eachDefaultSystem (system:
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs {
|
pkgs = import nixpkgs {
|
||||||
@@ -17,6 +18,7 @@
|
|||||||
with pkgs; mkShell rec {
|
with pkgs; mkShell rec {
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
love
|
love
|
||||||
|
cursor.packages.${pkgs.system}.default
|
||||||
];
|
];
|
||||||
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
|
|||||||
90
karten.lua
90
karten.lua
@@ -1,56 +1,62 @@
|
|||||||
local love = require "love"
|
local love = require("love")
|
||||||
|
|
||||||
Karten = {}
|
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, 10, 10, 10, 11} -- 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 })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
TileMap = love.graphics.newImage("/cards_asset_pack/CuteCards.png")
|
TileMap = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png")
|
||||||
return deck
|
return deck
|
||||||
end
|
end
|
||||||
|
|
||||||
function Karten:shuffleDeck(deck)
|
function Karten:shuffleDeck(deck)
|
||||||
for i = #deck, 2, -1 do
|
for i = #deck, 2, -1 do
|
||||||
local j = love.math.random(i)
|
local j = love.math.random(i)
|
||||||
deck[i], deck[j] = deck[j], deck[i]
|
deck[i], deck[j] = deck[j], deck[i]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Karten:handValue(hand)
|
function Karten:hit(hand)
|
||||||
local value = 0
|
if #Deck > 0 then
|
||||||
local aces = 0
|
local card = table.remove(Deck)
|
||||||
for _, card in ipairs(hand) do
|
table.insert(hand, card)
|
||||||
value = value + card.value
|
return card
|
||||||
if card.value == 11 then
|
end
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Karten: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
|
||||||
|
|
||||||
return Karten
|
return Karten
|
||||||
|
|||||||
Reference in New Issue
Block a user