Home > Back-end >  Sum of value in lists using c# -- BlackJack
Sum of value in lists using c# -- BlackJack

Time:04-29

I'm creating this Blackjack game and I'm having trouble calculating the total value of both the dealer and player. I've created a method to calculate the value for both the player(PlayerTotalCalculate) and the dealer (DealerTotalCalculate) using forloops.

When I run the program, the first sequence of cards are added up correctly, but when I "HIT" to get a new card for the player, it takes the previous value shown and adds the new card given as well as the value of the 2 cards given previously..

using System;
using System.Collections.Generic;

namespace BlackJack2
{
    internal class Program
    {
        //GLOBAL VARIABLES
        static  List<Card> myListOfCards = new List<Card>();
        static  List<Card> dealersHand = new List<Card>();
        static  List<Card> playersHand = new List<Card>();

        enum Phase { StartGame, DealersFirst, PlayersTurn, DealersTurn, ChooseWinner, End }
        static Phase currentPhase;
        static int userInput;

        static void Main(string[] args)
        {
            string suitFace = "";
            int dealersTotalValue = 0;
            int playersTotalvalue = 0;

            //Creating the list of cards
            for (int i = 0; i < 4; i  )
            {
                switch (i)
                {
                    case 0:
                        suitFace = "Diamonds";
                        break;
                    case 1:
                        suitFace = "Spades";
                        break;
                    case 2:
                        suitFace = "Hearts";
                        break;
                    case 3:
                        suitFace = "Clubs";
                        break;
                }
                myListOfCards.Add(new Card("King", suitFace, 10));
                myListOfCards.Add(new Card("Queen", suitFace, 10));
                myListOfCards.Add(new Card("Jack", suitFace, 10));
                myListOfCards.Add(new Card("10", suitFace, 10));
                myListOfCards.Add(new Card("9", suitFace, 9));
                myListOfCards.Add(new Card("8", suitFace, 8));
                myListOfCards.Add(new Card("7", suitFace, 7));
                myListOfCards.Add(new Card("6", suitFace, 6));
                myListOfCards.Add(new Card("5", suitFace, 5));
                myListOfCards.Add(new Card("4", suitFace, 4));
                myListOfCards.Add(new Card("3", suitFace, 3));
                myListOfCards.Add(new Card("2", suitFace, 2));
                myListOfCards.Add(new Card("Ace", suitFace, 1));
            }
            for (int i = 0; i < playersHand.Count; i  )
            {
                playersHand.Remove(playersHand[i]);
            }
            for (int i = 0; i < dealersHand.Count; i  )
            {
                dealersHand.Remove(dealersHand[i]);
            }
                //Assigning the first phase
            currentPhase = Phase.DealersFirst;
            DealersTurn(dealersTotalValue, playersTotalvalue);
        }

                //Deal Card Method using Random Number Generator
        static void DealCard(int dealerTotalValue, int playersTotalValue)
        {
            Random r = new Random();
            int randomNumber = r.Next(0, myListOfCards.Count);

            if (currentPhase == Phase.DealersFirst || currentPhase == Phase.DealersTurn)
            {                
                dealersHand.Add(myListOfCards[randomNumber]);

            }
            else if (currentPhase == Phase.PlayersTurn)
            {
                playersHand.Add(myListOfCards[randomNumber]);
                //PlayerTotalCalulate(ref playersTotalValue);
            }
            myListOfCards.RemoveAt(randomNumber);
        }

                //Determining dealer's actions
        static void DealersTurn(int dealersTotalvalue, int playersTotalValue)
        {
            if (currentPhase == Phase.DealersFirst)
            {
                for (int i = 0; i < 1; i  )
                {
                    DealCard(dealersTotalvalue, playersTotalValue);
                    DealerTotalCalculate(ref dealersTotalvalue);
                }
                DisplayDealerCards();
                Console.WriteLine($"The dealer has a total value of: {dealersTotalvalue}");
                currentPhase = Phase.PlayersTurn;
                PlayersTurn(dealersTotalvalue, playersTotalValue);
            }

            else if (currentPhase == Phase.DealersTurn)
            {
                while (dealersTotalvalue < 15)
                {
                    DealCard(dealersTotalvalue, playersTotalValue);
                    DealerTotalCalculate(ref dealersTotalvalue);
                    DisplayDealerCards();
                    Console.WriteLine($"The dealer's new total is: {dealersTotalvalue}");
                }
                CalculateWinner(dealersTotalvalue, playersTotalValue);
            }        
        }

        static void PlayersTurn(int dealersTotalValue, int playersTotalValue)
        {
            for (int i = 0; i < 2; i  )
            {
                DealCard(dealersTotalValue, playersTotalValue);
            }
            PlayerTotalCalulate(ref playersTotalValue);
            DisplayPlayerCards(playersTotalValue);
            Console.WriteLine($"You have a total value of: {playersTotalValue}");
            while (currentPhase == Phase.PlayersTurn)
            {
                Console.WriteLine("Would you like to hit or stay?");
                Console.WriteLine("1: HIT or 2: STAY");
                int.TryParse(Console.ReadLine(), out userInput);
                if (userInput == 1)
                {
                    DealCard(dealersTotalValue, playersTotalValue);
                    PlayerTotalCalulate(ref playersTotalValue);
                    DisplayPlayerCards(playersTotalValue);
                    Console.WriteLine($"You now have a total value of: {playersTotalValue}");
                }
                else if (userInput == 2)
                {
                    currentPhase = Phase.DealersTurn;
                    DealersTurn(dealersTotalValue, playersTotalValue);
                }
                else
                {
                    Console.WriteLine("Invalid input! Please try again.");
                }
            }
        }
        static void DisplayPlayerCards(int playersTotalValue)
        {
            for (int i = 0; i < playersHand.Count; i  )
            {
                Console.WriteLine($"You were dealt a(n): {playersHand[i].DisplayCard()}");
            }
            PlayerTotalCalulate(ref playersTotalValue);
        }

        static void DisplayDealerCards()
        {
            //Showing only one card from the dealer
            for (int i = 0; i < dealersHand.Count; i  )
            {
                Console.WriteLine($"The dealer is showing: {dealersHand[i].DisplayCard()}. The other card is hidden.");
            }

        }


        static void CalculateWinner(int dealersTotalvalue, int playersTotalValue)
        {
            if (playersTotalValue > dealersTotalvalue && playersTotalValue < 21)
            {
                Console.WriteLine($"Your Total: {playersTotalValue}");
                Console.WriteLine($"Dealer's Total: {dealersTotalvalue}");
                Console.WriteLine("Great job! You've won!");
                Console.WriteLine("Press ENTER to play again!");
                Console.ReadKey();
            }
            else if (playersTotalValue == dealersTotalvalue)
            {
                Console.WriteLine($"Your Total: {playersTotalValue}");
                Console.WriteLine($"Dealer's Total: {dealersTotalvalue}");
                Console.WriteLine("The game is a tie!");
                Console.WriteLine("Press ENTER to play again!");
                Console.ReadKey();
            }
            else if (playersTotalValue < dealersTotalvalue && playersTotalValue > 21)
            {
                Console.WriteLine($"Your Total: {playersTotalValue}");
                Console.WriteLine($"Dealer's Total: {dealersTotalvalue}");
                Console.WriteLine("Oh no! You've lost!");
                Console.WriteLine("Press ENTER to play again!");
                Console.ReadKey();
            }         
        //Console.WriteLine(PlayerTotalCalulate(ref playersTotalValue));
        }

        static void PlayerTotalCalulate(ref int playersTotalValue)
        {
            for (int i = 0; i < playersHand.Count; i  )
            {
                playersTotalValue  = playersHand[i].Value;
            }           
        }

        static void DealerTotalCalculate(ref int dealersTotalValue)
        {
            for (int i = 0; i < dealersHand.Count; i  )
            {
                dealersTotalValue  = dealersHand[i].Value;
            }
        }

        static void DisplayAllCards()
        {
            for (int i = 0; i < myListOfCards.Count; i  )
            {
                Console.WriteLine(myListOfCards[i].DisplayCard());
                Console.WriteLine("\n\r\t");
            }
        }

        static void DisplayingSingleCard()
        {
            Console.WriteLine(myListOfCards[0].DisplayCard());
        }
    }


}

I've also provided a screenshot of the console itself. ConsolePicture

CodePudding user response:

In DealerTotalCalculate, you should set dealersTotalValue to 0 before you add up the cards.

You need to do that in PlayerTotalCalulate as well.

CodePudding user response:

As said in my comment, it's because your total variable is never reseted. So when you call the method to add the new value, it takes also the old value into consideration. So you just need to do total = 0, before calling the compute method.

Also consider this to simplify your compute method :

int[] array = { 1, 2, 3, 4, 5 };
 
int sum = array.Sum();
Console.WriteLine(sum);

It's slightly better, regarding code readability in my opinion. Not a big deal at all.

  •  Tags:  
  • c#
  • Related