added winConditionCheck
This commit is contained in:
@@ -3,6 +3,7 @@ require("karten")
|
|||||||
|
|
||||||
PlayerHand = {}
|
PlayerHand = {}
|
||||||
DealerHand = {}
|
DealerHand = {}
|
||||||
|
GameState = "playing" -- "playing", "player_won", "dealer_won", "tie"
|
||||||
|
|
||||||
function LoadBlackjack()
|
function LoadBlackjack()
|
||||||
DealerHandPosition = {
|
DealerHandPosition = {
|
||||||
@@ -30,7 +31,7 @@ function DrawBlackjack()
|
|||||||
local tileHeight = Cards:getHeight() / 4
|
local tileHeight = Cards:getHeight() / 4
|
||||||
local scale = 0.5
|
local scale = 0.5
|
||||||
local drawW, drawH = tileWidth * scale, tileHeight * scale
|
local drawW, drawH = tileWidth * scale, tileHeight * scale
|
||||||
local spacingX, spacingY = 1, 1
|
local spacingX, spacingY = 10, 10
|
||||||
|
|
||||||
local function drawHandGrid(hand, basePos)
|
local function drawHandGrid(hand, basePos)
|
||||||
if #hand == 0 then
|
if #hand == 0 then
|
||||||
@@ -83,24 +84,36 @@ function DrawBlackjack()
|
|||||||
DealerHandPosition.x,
|
DealerHandPosition.x,
|
||||||
DealerHandPosition.y + (math.ceil(#DealerHand / 3) * drawH) + 15
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
function GetHandValue(hand)
|
function GetHandValue(hand)
|
||||||
local count = 0
|
local count = 0
|
||||||
local aces = 0
|
local aces = 0
|
||||||
|
|
||||||
for _, card in pairs(hand) do
|
for _, card in pairs(hand) do
|
||||||
if card.value < 10 then
|
if card.value < 10 then
|
||||||
count = count + card.value
|
count = count + card.value
|
||||||
print("+" .. card.value)
|
|
||||||
elseif card.value < 14 then
|
elseif card.value < 14 then
|
||||||
count = count + 10
|
count = count + 10
|
||||||
print("+" .. card.value)
|
|
||||||
else
|
else
|
||||||
count = count + 11
|
count = count + 11
|
||||||
print("+" .. 11)
|
|
||||||
aces = aces + 1
|
aces = aces + 1
|
||||||
print("+ 1 ace = " .. aces)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -112,34 +125,72 @@ function GetHandValue(hand)
|
|||||||
goto aceloop
|
goto aceloop
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
print("end------------")
|
|
||||||
|
|
||||||
return count
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
function HandleBlackjackInput(key)
|
function HandleBlackjackInput(key)
|
||||||
if key == "h" then
|
if GameState == "playing" then
|
||||||
Karten:hit(PlayerHand)
|
if key == "h" then
|
||||||
if Karten:handValue(PlayerHand) > 21 then
|
Karten:hit(PlayerHand)
|
||||||
love.graphics.print("Spieler hat verloren!", 50, 200)
|
CheckWinCondition()
|
||||||
|
elseif key == "s" then
|
||||||
|
-- Dealer zieht bis 17 oder höher
|
||||||
|
while GetHandValue(DealerHand) < 17 do
|
||||||
|
Karten:hit(DealerHand)
|
||||||
|
end
|
||||||
|
CheckWinCondition()
|
||||||
end
|
end
|
||||||
elseif key == "s" then
|
end
|
||||||
-- while Karten:handValue(DealerHand) < 17 do
|
|
||||||
Karten:hit(DealerHand)
|
if key == "r" then
|
||||||
-- end
|
ResetGame()
|
||||||
-- 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
|
||||||
end
|
end
|
||||||
|
|
||||||
function DealInitialCards()
|
function DealInitialCards()
|
||||||
for i = 1, 2 do
|
for _ = 1, 2 do
|
||||||
Karten:hit(PlayerHand)
|
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
|
||||||
|
|||||||
44
karten.lua
44
karten.lua
@@ -23,22 +23,6 @@ function Karten:shuffleDeck(deck)
|
|||||||
end
|
end
|
||||||
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(hand)
|
function Karten:hit(hand)
|
||||||
if #Deck > 0 then
|
if #Deck > 0 then
|
||||||
local card = table.remove(Deck)
|
local card = table.remove(Deck)
|
||||||
@@ -47,4 +31,32 @@ function Karten:hit(hand)
|
|||||||
end
|
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