We are creating the Towers of Hanoi game in a console window. We are supposed to use a Dictionary> to hold the game pieces. I have it declared outside of main as a global. When I call the static void function drawboard(Dictionary<char, Stack<int>>
. No matter what I try, a local variable name System.Collections.Generic.Dictionary<TKey, TValue>.this[TKey].get returned
. When I watch the variable it is displayed as $ReturnValue1
, and it is overwriting my original dictionary. What is happening, and how do I stop it, or at least work around it.
I have tried creating clones of the dictionary and passing them through drawboard, but it still overrides the original. I have tried changing the scope of the dictionary, and I have tried adding modifiers to the dictionary like protected, private, ect. I have also jumped through several hoops and made my drawboard a little more complicated than it currently is trying to get around this problem. I can simplify it easily enough if I can figure out the root of my problem.
--declaration--
static Dictionary<char, Stack<int>> towers = new Dictionary<char, Stack<int>>();
towers.Add('A', new Stack<int> { });
for (int x = 4; x > 0; x--)
{
towers['A'].Push(x);
}
towers.Add('B', new Stack<int> { });
towers.Add('C', new Stack<int> { });
---draw board function---
static void printboard(Dictionary<char, Stack<int>> t)
{
Stack<int> A = t['A'];
Stack<int> B = t['B'];
Stack<int> C = t['C'];
for (int y = 0; y < 5; y++)
{
if (y == 4)
{
Console.Write("----------\n" +
"A B C\n");
}
else
{
if (A.Count == 0)
Console.Write("|" + " ");
else
{
Console.Write(A.Pop() + " ");
}
if (B.Count == 0)
Console.Write("|" + " ");
else
{
Console.Write(B.Pop() + " ");
}
if (C.Count == 0)
Console.Write("|" + " ");
else
{
Console.Write(C.Pop() + " ");
}
Console.Write("\n");
}
}
}
The code is supposed to pass the dictionary by value, pop out values from the local copy of the dictionary and print them out to the console window, without affecting the original towers Dictionary.