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"
|
||||
require "karten"
|
||||
local love = require("love")
|
||||
require("karten")
|
||||
|
||||
spielerHand = {}
|
||||
PlayerHand = {}
|
||||
DealerHand = {}
|
||||
Deck = {}
|
||||
GameState = "playing" -- "playing", "player_won", "dealer_won", "tie"
|
||||
|
||||
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/JustNormalCardsSwitched.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)
|
||||
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 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 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 i = 1, 2 do
|
||||
Karten:hit(spielerHand)
|
||||
Karten:hit(DealerHand)
|
||||
end
|
||||
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
|
||||
|
||||
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": {
|
||||
"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": {
|
||||
"inputs": {
|
||||
"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": {
|
||||
"lastModified": 1731533236,
|
||||
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||
@@ -19,6 +57,22 @@
|
||||
}
|
||||
},
|
||||
"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": {
|
||||
"lastModified": 1753694789,
|
||||
"narHash": "sha256-cKgvtz6fKuK1Xr5LQW/zOUiAC0oSQoA9nOISB0pJZqM=",
|
||||
@@ -36,8 +90,9 @@
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
"cursor": "cursor",
|
||||
"flake-utils": "flake-utils_2",
|
||||
"nixpkgs": "nixpkgs_2"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
@@ -54,6 +109,21 @@
|
||||
"repo": "default",
|
||||
"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",
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
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:
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
@@ -17,6 +18,7 @@
|
||||
with pkgs; mkShell rec {
|
||||
buildInputs = [
|
||||
love
|
||||
cursor.packages.${pkgs.system}.default
|
||||
];
|
||||
|
||||
shellHook = ''
|
||||
|
||||
90
karten.lua
90
karten.lua
@@ -1,56 +1,62 @@
|
||||
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, 11, 12, 13, 14 }
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user