Hit 和 Double 函数被重复调用 - 按一下即可

问题描述 投票:0回答:1

我最近一直在尝试制作一个roblox游戏,里面有一个使用GUI等内置的纸牌游戏。目前我正在实现游戏的逻辑,当我去测试它时;似乎有时当我单击

Hit()
Double()
时,它决定需要 3 卡 而不是 1 卡,我假设在我的代码中的某个地方我意外地调用了它两次,但我已经走过但没能看到它;所以我开始认为这是别的东西。我将在下面附上我的代码,如果有人有任何关于为什么这不起作用的线索,我们将非常感激。不要用勺子喂我代码,只需给我文档或一些行号即可。

谢谢:D

local function Hit(player, Hand)
    if #deck == 0 then
        shuffleTable(deck)
    end

    local newCard = table.remove(deck, 1)
    local newCardTexture = cardTextures[newCard]

    -- Add the new card to the player's hand
    table.insert(Hand, newCard)
    local handTotal = CalculateHandTotal(Hand)
    player.PlayerGui.Blackjack.Main.PlayerTotal.Text = handTotal

    -- Update the player's hand display
    local templateCard = game.ReplicatedStorage.CardImages.FirstCard:Clone()
    templateCard.Name = "NewCard"  -- Change the name if necessary
    templateCard.Image = newCardTexture
    templateCard.Parent = player.PlayerGui.Blackjack.Main.PlayerCards

    if tonumber(player.PlayerGui.Blackjack.Main.PlayerTotal.Text) > 21 then
        print("You busted!")
        return false
    end
    return true
end
-- Function to handle the "Stand" action
local function Stand(player, playerHand, dealerHand)
    while tonumber(player.PlayerGui.Blackjack.Main.DealerTotal.Text) < 17 do
        dealerTotal = CalculateHandTotal(dealerHand)
        player.PlayerGui.Blackjack.Main.DealerTotal.Text = dealerTotal

        if dealerTotal > 17 then
            break
        end

        if #deck == 0 then
            shuffleTable(deck)
        end

        local newCard = table.remove(deck, 1)
        local newCardTexture = cardTextures[newCard]
        -- Add the new card to the dealer's hand
        table.insert(dealerHand, newCard)


        -- Update the dealer's hand display
        local templateCard = game.ReplicatedStorage.CardImages.FirstCard:Clone()
        templateCard.Name = "NewCard"  -- Change the name if necessary
        templateCard.Image = newCardTexture
        templateCard.Parent = player.PlayerGui.Blackjack.Main.DealerCards
    end

    playerTotal = tonumber(player.PlayerGui.Blackjack.Main.PlayerTotal.Text)
    -- Update the dealer's total display

    -- Implement your win/lose logic based on the final dealer total and player total
    if dealerTotal > 21 or dealerTotal < playerTotal then
        -- Dealer busts or player has a higher total, player wins
        -- You can implement the reward or outcome here
        print("You won")
    else
        -- Dealer has a higher total, player loses
        -- Implement the outcome accordingly
        print("You lost :(!")
    end
end


local function Double(player, playerHand, dealerHand)
    -- Double the player's bet (if you have a betting system)
    -- Perform a "Hit" and then "Stand"
    Hit(player, playerHand)
    Stand(player, playerHand, dealerHand)
end

game.ReplicatedStorage.StartBlackjack.OnServerEvent:Connect(function(plr)
    local dealerCards = plr.PlayerGui.Blackjack.Main.DealerCards
    for _, child in ipairs(dealerCards:GetChildren()) do
        if child.Name ~= "FirstCard" and child.Name ~= "TextLabel" and child.Name ~= "UIListLayout" then
            child:Destroy()
        end
    end

    local playerCards = plr.PlayerGui.Blackjack.Main.PlayerCards
    for _, child in ipairs(playerCards:GetChildren()) do
        if child.Name ~= "FirstCard" and child.Name ~= "SecondCard" and child.Name ~= "UIListLayout" then
            child:Destroy()
        end
    end


    if #deck == 0 then
        shuffleTable(deck)
    end

    local dealerFirstCard = table.remove(deck, 1)
    local dealerFirstCardTexture = cardTextures[dealerFirstCard]
    plr.PlayerGui.Blackjack.Main.DealerCards.FirstCard.Image = dealerFirstCardTexture

    local dealerSecondCard = table.remove(deck, 1)
    local hiddenCard = game.ReplicatedStorage.CardImages.TextLabel:Clone()
    hiddenCard.Parent = plr.PlayerGui.Blackjack.Main.DealerCards

    local playerFirstCard = table.remove(deck, 1)
    local playerFirstCardTexture = cardTextures[playerFirstCard]
    plr.PlayerGui.Blackjack.Main.PlayerCards.FirstCard.Image = playerFirstCardTexture

    local playerSecondCard = table.remove(deck, 1)
    local playerSecondCardTexture = cardTextures[playerSecondCard]
    plr.PlayerGui.Blackjack.Main.PlayerCards.SecondCard.Image = playerSecondCardTexture

    local playerTotal = GetCardValue(playerFirstCard) + GetCardValue(playerSecondCard)
    plr.PlayerGui.Blackjack.Main.PlayerTotal.Text = playerTotal

    local dealerTotal = GetCardValue(dealerFirstCard)
    plr.PlayerGui.Blackjack.Main.DealerTotal.Text = dealerTotal

    -- Initialize player data
    Hand = {playerFirstCard, playerSecondCard}
    Total = plr.PlayerGui.Blackjack.Main.PlayerTotal
    Cards = {
        ["Card1"] = plr.PlayerGui.Blackjack.Main.PlayerCards.FirstCard,
        ["Card2"] = plr.PlayerGui.Blackjack.Main.PlayerCards.SecondCard,
    }

    -- Connect the GUI buttons to the corresponding actions
    local hitButton = plr.PlayerGui.Blackjack.Main.Hit
    local standButton = plr.PlayerGui.Blackjack.Main.Stand
    local doubleButton = plr.PlayerGui.Blackjack.Main.Double
    local gameOver = false  
    
    hitButton.Transparency = 0
    standButton.Transparency = 0
    doubleButton.Transparency = 0
    hitButton.TextTransparency = 0
    standButton.TextTransparency = 0
    doubleButton.TextTransparency = 0

    hitButton.MouseButton1Click:Connect(function()
        if hitButton.Transparency <= 0 then
            if gameOver then
                return -- Exit the handler if the game is already over
            end
                
            if Hit(plr, Hand) == false then
                gameOver = true
                print("Made game quit")
                
                hitButton.Transparency = 0.6
                hitButton.TextTransparency = 0.6
                
                standButton.TextTransparency = 0.6
                standButton.Transparency = 0.6
                
                doubleButton.Transparency = 0.6
                doubleButton.TextTransparency = 0.6
            end
            
            
        else
            return
        end
        
    end)

    standButton.MouseButton1Click:Connect(function()
        if standButton.Transparency <= 0 then
            if gameOver then
                return -- Exit the handler if the game is already over
            end
            
            s, e = pcall(function()
                plr.PlayerGui.Blackjack.Main.DealerCards.TextLabel:Destroy()
            end)
            
            local newCard = game.ReplicatedStorage.CardImages.FirstCard:Clone()
            newCard.Name = "RemoveHiddenCard"
            newCard.Parent = plr.PlayerGui.Blackjack.Main.DealerCards
            newCard.Image = cardTextures[dealerSecondCard]

            print("Standing")
            Stand(plr, Hand, {dealerFirstCard, dealerSecondCard})
            print("Completed")
            
            hitButton.Transparency = 0.6
            hitButton.TextTransparency = 0.6

            standButton.TextTransparency = 0.6
            standButton.Transparency = 0.6

            doubleButton.Transparency = 0.6
            doubleButton.TextTransparency = 0.6
        else
            return
        end
    end)

    doubleButton.MouseButton1Click:Connect(function()
        if doubleButton.Transparency <= 0 then
            if gameOver then
                return
            end
            
            Double(plr, Hand, {dealerFirstCard, dealerSecondCard})
        else
            return
        end
    end)

    if gameOver then
        return true
    end
end)
lua roblox
1个回答
0
投票

我没有办法调试这个或测试它,我真正可以告诉你的是让代码更短,以便你自己查明问题。或者你可以作弊并让它在功能完成后删除 2 张牌。它仍然是一张随机卡。

© www.soinside.com 2019 - 2024. All rights reserved.