【问题标题】:I'm not sure why the dealer isnt hitting我不知道为什么经销商不打
【发布时间】:2020-04-10 07:37:19
【问题描述】:

这是我正在使用的牌,我很困惑为什么荷官在指示时没有击牌。我重新排列了我的代码,尝试了不同的方法,但它似乎仍然不起作用。如果有人能帮我解决这个 C# 脚本,我将非常感激。

using System;
using System.Collections.Generic;

public class MainClass 
{
    public static int PlayerTotal;
    public static int DealerTotal;

    static Random random = new Random();

    public static List<int> cards = new List<int>();
    public static List<int> hand = new List<int>();
    public static List<int> DealerHand = new List<int>();

    public static bool GameOver = false;

    public static void Main (string[] args) 
    {
        // Players hand gets created
        for (int i = 2; i <= 11; i++) 
        {
            cards.Add(i);
        }

        for (int i = 0; i < 2; i++)
        {
            int index = random.Next(cards.Count);
            hand.Add(cards[index]);
        }

        foreach (int a in hand)  
        {
            Console.WriteLine("{0}", a);
            PlayerTotal = PlayerTotal + a;

            if(PlayerTotal == 22 || PlayerTotal == 21) 
            {
                Console.WriteLine("You won!");
                GameOver = true;
            }
        }

        Console.WriteLine("Your hand total is " + PlayerTotal);

        // Dealer hand being created
        for(int i = 0; i < 2; i++) 
        {
            int index = random.Next(cards.Count);
            DealerHand.Add(cards[index]);
        }

        foreach(int a in DealerHand) 
        {
            Console.WriteLine("{0}", a);
            DealerTotal =  DealerTotal + a;

            if(DealerTotal == 22 || DealerTotal == 21) 
            {
                Console.WriteLine("The dealer won!");
                GameOver = true;
            }
        }

        Console.WriteLine("The dealer hand total is " + DealerTotal);

        // Player choice to hit or stay
        while (PlayerTotal < 21 && GameOver == false) 
        {
            Console.WriteLine("Do you want to hit or stand? h/s");
            string choice = Console.ReadLine();

            if (choice == "h")
            {
                PlayerHit();
            } 
            else if (choice == "s")
            {
                Console.WriteLine("You stood");
            }
        }

        while (DealerTotal < 21 && GameOver == false ) 
        {
            if(DealerTotal <= 16) 
            {
                DealerHit();
            } 
            else 
            {
                Console.WriteLine("The dealer stood");
            }
        }
    }

    public static void PlayerHit() 
    {
        for (int i = 0; i < 1; i++) 
        {
            int index = random.Next(cards.Count);
            int hitCard = cards[index];
            hand.Add(hitCard);

            PlayerTotal = PlayerTotal + hitCard;
            Console.WriteLine("You got a " + hitCard);
            Console.WriteLine("Your new total is " + PlayerTotal);

            if(PlayerTotal > 21)  
            {
                Console.WriteLine("You lost!");
                GameOver = true;
            } 
            else if (PlayerTotal == 21) 
            {
                Console.WriteLine("You won!");
                GameOver = true;
            }
        }
    }

    public static void DealerHit()
    {
        for (int i = 0; i < 1; i++)
        {
            int index = random.Next(cards.Count);
            int DealerHitCard = cards[index];
            DealerHand.Add(DealerHitCard);

            DealerTotal = DealerTotal + DealerHitCard;
            int DealerHitCardNew = DealerHitCard;

            Console.WriteLine("The dealer got a " + DealerHitCardNew);
            Console.WriteLine("The dealers total is now " + DealerTotal);

            if (DealerTotal > 21)
            {
                Console.WriteLine("You Won!");
                GameOver = true;
            } 
            else if (DealerTotal == 21)
            {
                Console.WriteLine("You Lost!");
                GameOver = true;
            }
        }
    }
}

任何帮助将不胜感激。我的学校给我们布置了作业,我们必须创建一个应用程序,我认为这会很有趣。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请您能准确描述如何使用您发布的代码重现问题吗?如果您可以将其简化为一个最小的示例,请将其格式化(Visual Studio 会为您完成)并向我们解释您在调试代码时发生了什么,这将有助于使您的问题变得有用。
  • Using the free, built-in Step Debugger 调试代码比您想象的要容易。
  • 问题是我不知道这段代码的哪一部分导致了这个问题,我是 c# 的新手以及它的工作方式,所以这让人困惑为什么它不工作。对此感到抱歉。
  • 我没有遇到任何错误,只是没有按照我想要的方式工作。
  • 这就是调试器和调试的用途 - 在执行时观察并检查您的代码和变量。我们确实了解该帖子

标签: c# blackjack


【解决方案1】:

这里的这一行:

while(PlayerTotal < 21 && GameOver == false) {

只有当你破产或得到 21 时才会如此。如果你站着,你将永远不会退出这个循环。为支架选项添加休息时间:

        } else if(choice == "s"){
            Console.WriteLine("You stood");
            break;
        }

很遗憾,您在以下循环中遇到了同样的问题:

while(DealerTotal < 21 && GameOver == false ) {

将break 添加到正确的块中以在庄家站立时退出循环。最后,您需要在玩家和庄家都站好后添加输赢逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2015-07-14
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多