Shane

Shane

  • NA
  • 11
  • 0

Nested foreach loops

Apr 14 2010 3:18 AM

Is it possible to do nested foreach loops? My problem is that for each item in my Dictionary array the program writes the key and value to the console, and for each of the two dictionaries, compares them, and then moves on. However the program is completely skipping the first foreach loop and just jumpping right into the second one. The problem with this is that the value of "playerHand" stays the same until the loop has gone through all of the values in "dealerHand". FYI this is a card game, and each person has 26 cards, so every 26 loops, playerhand will finnaly move ontot he next value
 
public
static void Flip()
{
int playerValue;//Value of players hand
int dealerValue;//Value of dealer hand

//START FLIP LOOP
foreach (KeyValuePair<string, int> playerPair in playerHand)
{
foreach (KeyValuePair<string, int> dealerPair in dealerHand)
{
if (playerHand.TryGetValue(playerPair.Key, out playerValue) & dealerHand.TryGetValue(dealerPair.Key, out dealerValue))
{
Console.WriteLine("Player draw: {0}, {1}",
playerPair.Key,
playerPair.Value);
Console.WriteLine("Dealer draw: {0}, {1}",
dealerPair.Key,
dealerPair.Value);
if (playerValue > dealerValue)
{
playerTotal++;
dealerTotal--;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nPlayer Score: " + playerTotal);
Console.WriteLine("Dealer Score: " + dealerTotal);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n{0} Player hand is GREAT than dealer hand {1}. Player wins round.", playerPair.Key, dealerPair.Key);
//Update();
}
if (playerValue < dealerValue)
{
playerTotal--;
dealerTotal++;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nPlayer Score: " + playerTotal);
Console.WriteLine("Dealer Score: " + dealerTotal);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n{0} Player hand is LESS than dealer hand {1}. Dealer wins round.", playerPair.Key, dealerPair.Key);
//Update();
}
Console.ReadLine();
}
}
}
}


 
 
I guess i could try copying all the values in each dictionary to a list, and then use for loops instead of foreach.