简体   繁体   中英

I'm not sure why the dealer isnt hitting

This is the card I'm working with and I'm very confused to why the dealer isn't hitting when instructed to. I've rearranged my code tried different methods but it still doesn't seem to be working. If someone can help me solve this C# script I'd be very thankful.

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;
            }
        }
    }
}

Any help would be very appreciated. My school has set us homework that we have to create a sort of application, and i thought this would be fun to make.

This line here:

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

That will only be true if you bust or get 21. If you stand, you will never exit this loop. Add a break to the stand option:

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

Unfortunately, you have the same issue in the following loop:

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

Add a break to the correct block to exit the loop when the dealer stands. Finally, you will need to add winning/losing logic after both the player and the dealer have stood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM