# Sum of value in lists using c# — BlackJack

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:
break;
case 2:
suitFace = "Hearts";
break;
case 3:
suitFace = "Clubs";
break;
}
}
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)
{

}
else if (currentPhase == Phase.PlayersTurn)
{
//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);
}
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);
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");
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
{
}
}
}
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(\$"Dealer's Total: {dealersTotalvalue}");
Console.WriteLine("Great job! You've won!");
Console.WriteLine("Press ENTER to play again!");
}
else if (playersTotalValue == dealersTotalvalue)
{
Console.WriteLine(\$"Dealer's Total: {dealersTotalvalue}");
Console.WriteLine("The game is a tie!");
Console.WriteLine("Press ENTER to play again!");
}
else if (playersTotalValue < dealersTotalvalue && playersTotalValue > 21)
{
Console.WriteLine(\$"Dealer's Total: {dealersTotalvalue}");
Console.WriteLine("Oh no! You've lost!");
Console.WriteLine("Press ENTER to play again!");
}
//Console.WriteLine(PlayerTotalCalulate(ref playersTotalValue));
}

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

{
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("nrt");
}
}

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

}
``````

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