Search code examples
c#imageconsole-applicationblackjack

In a C# console app, how would I create 'images' for playing cards in a blackjack game?


I'm working on a simple C# blackjack console application game for my school and I was given an example to look at. This example somehow draws a picture of the card in the console window and I can't figure out how to replicate this without specifying hundreds of Console.Write's for each of the 52 unique cards.

in game scene This is what it looks like when you're actually playing the game. Quite nice.

shuffle and show deck There is also an option from the main menu to shuffle and display all 52 cards.

So what is this wizardry? Did they actually spend a TON of time hard-coding how every single unique card prints out? I sure hope not. This is what I'm trying to replicate and I'm at a loss for ideas besides hard-coding. Thanks for your help.


Solution

  • Thanks to Damien_The_Unbeliever's comment, I was able to come up with these 2 methods within my card class. Also thanks to vik_78's comment for letting me know that I needed UTF8 encoding to be able to see the card symbols.

        public void PrintCard()
        {
            if (_value == 1)
            {
                _printString =
                    " V         " +
                    "           " +
                    "           " +
                    "     S     " +
                    "           " +
                    "           " +
                    "         V " ;
                PrintMethod();
            }
            if (_value == 2)
            {
                _printString =
                    " V         " +
                    "     S     " +
                    "           " +
                    "           " +
                    "           " +
                    "     S     " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 3)
            {
                _printString =
                    " V         " +
                    "     S     " +
                    "           " +
                    "     S     " +
                    "           " +
                    "     S     " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 4)
            {
                _printString =
                    " V         " +
                    "   S   S   " +
                    "           " +
                    "           " +
                    "           " +
                    "   S   S   " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 5)
            {
                _printString =
                    " V         " +
                    "   S   S   " +
                    "           " +
                    "     S     " +
                    "           " +
                    "   S   S   " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 6)
            {
                _printString =
                    " V         " +
                    "   S   S   " +
                    "           " +
                    "   S   S   " +
                    "           " +
                    "   S   S   " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 7)
            {
                _printString =
                    " V         " +
                    "   S   S   " +
                    "     S     " +
                    "   S   S   " +
                    "           " +
                    "   S   S   " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 8)
            {
                _printString =
                    " V         " +
                    "   S   S   " +
                    "     S     " +
                    "   S   S   " +
                    "     S     " +
                    "   S   S   " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 9)
            {
                _printString =
                    " V         " +
                    "   S S S   " +
                    "           " +
                    "   S S S   " +
                    "           " +
                    "   S S S   " +
                    "         V ";
                PrintMethod();
            }
            if (_value == 10 || _value == 11 || _value == 12 || _value == 13)
            {
                _printString =
                    " V         " +
                    "    S S    " +
                    "     S     " +
                    "  S S S S  " +
                    "     S     " +
                    "    S S    " +
                    "         V ";
                PrintMethod();
            }
        }
        private void PrintMethod()
        {
            bool hasWrittenFirstNumber = false;
    
            switch (_suit)
            {
                case "Hearts":
                case "Diamonds":
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                case "Clubs":
                case "Spades":
                    Console.ForegroundColor = ConsoleColor.Black;
                    break;
            }
    
            for (int i = 0; i < _printString.Length; i++)
            {
                Console.BackgroundColor = ConsoleColor.White;
                if (i % 11 == 0 && i != 0)
                {
                    Console.CursorLeft -= 11;
                    Console.CursorTop += 1;
                }
                if (_printString[i] == 'S')
                {
                    switch (_suit)
                    {
                        case "Hearts":
                            Console.Write('♥');
                            break;
                        case "Clubs":
                            Console.Write("♣");
                            break;
                        case "Diamonds":
                            Console.Write("♦");
                            break;
                        case "Spades":
                            Console.Write("♠");
                            break;
                    }
                    continue;
                }
                else if (_printString[i] == 'V')
                {
                    if (_value == 10)
                    {
                        if (hasWrittenFirstNumber == false)
                        {
                            Console.Write(10);
                            hasWrittenFirstNumber = true;
                            i++;
                        }
                        else
                        {
                            Console.CursorLeft--;
                            Console.Write(10);
                        }
                        continue;
                    }
                    else if (_value == 11)
                    {
                        Console.Write("J");
                    }
                    else if (_value == 12)
                    {
                        Console.Write("Q");
                    }
                    else if (_value == 13)
                    {
                        Console.Write("K");
                    }
                    else if (_value == 1)
                    {
                        Console.Write("A");
                    }
                    else
                    {
                        Console.Write(_value);
                    }
                }
                else
                {
                    Console.Write(_printString[i]);
                }
            }
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
        }
    

    vik_78's answer

    Damien_The_Unbeliever's comment