Home > Mobile >  print 2d array based on the enum values
print 2d array based on the enum values

Time:11-21

Firstly I have an enum storing the c ConsoleColor values, I want to print a 2d array to the console such that it shows an '#' instead of the enum value based on the color set in the enum, I have been trying to so this for a while now but I couldent get anywhere any help will be much appreciated.

Here is my code

public enum CandyCrushCandies
{
    JellyBean = ConsoleColor.Red, 
    Lozenge = ConsoleColor.DarkCyan, 
    LemonDrop = ConsoleColor.Yellow, 
    GumSquare = ConsoleColor.Green, 
    LollipopHead = ConsoleColor.Blue, 
    JujubeCluster = ConsoleColor.Magenta 
}

method for displaying to the console

  void DisplayCandyCrushCandies(CandyCrushCandies[,] playingField)
    {
        Array values = Enum.GetValues(typeof(CandyCrushCandies));
        Random rnd = new Random();
        CandyCrushCandies randomBar = (CandyCrushCandies)values.GetValue(rnd.Next(values.Length));

        string name = Enum.GetName(typeof(CandyCrushCandies), randomBar);
        var type = typeof(ConsoleColor);
      

        for (int row = 0; row < playingField.GetLength(0); row  )
        {
            for (int col = 0; col < playingField.GetLength(1); col  )
            {
                playingField[row, col] = (CandyCrushCandies)(ConsoleColor)type.GetProperty(name).GetValue(playingField[row, col]);
                Console.Write("# ", playingField[row, col]);
            }
            Console.WriteLine();
        }
    }

thanks a lot in advanced

CodePudding user response:

It is not clear from your question, but i assume you were attempting to ask how to output colorized # characters to the console. Which shouldn't be too difficult, given that System.Console has properties both for setting the foreground color (text color) as well as the background color of whatever is being printed by subsequent System.Console.Write/WriteLine calls.

CodePudding user response:

I found out that you want to print rows and columns of '#' whose color is determined by another array:

void DisplayCandyCrushCandies(CandyCrushCandies[,] playingField)
{
   // I don't know what you want to do with this
   Array values = Enum.GetValues(typeof(CandyCrushCandies));
   Random rnd = new Random();
   CandyCrushCandies randomBar = (CandyCrushCandies)values.GetValue(rnd.Next(values.Length));

   string name = Enum.GetName(typeof(CandyCrushCandies), randomBar);
   var type = typeof(ConsoleColor);
   //


   for (int row = 0; row < playingField.GetLength(0); row  )
   {
       for (int col = 0; col < playingField.GetLength(1); col  )
       {
          //playingField[row, col] = (CandyCrushCandies)(ConsoleColor)type.GetProperty(name).GetValue(playingField[row, col]);

          Console.ForegroundColor = (ConsoleColor)playingField[row, col];
          Console.Write("# ");
       }
                Console.WriteLine();
   }
}
  •  Tags:  
  • c#
  • Related