-2

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.

Ryan
  • 19
  • 1
  • 6
  • have a look at [Display an Image in a console application](https://stackoverflow.com/a/33652557/2417602) – vikscool Apr 17 '19 at 09:16
  • You can change the mode of the console (at least under Windows) to have direct access to the buffer, instead of it being line based. – Neijwiert Apr 17 '19 at 09:16
  • You need 13 patterns that describe how to show each rank of card. You need some way within the pattern to say "rank symbol" or "suit symbol". Then you write a function where you pass it the suit and rank of the card to show. The patterns are probably encoded as *data*, not *code*. – Damien_The_Unbeliever Apr 17 '19 at 09:18
  • The specifics depend a lot on *what you've been taught so far in your curriculum* though, so we're unable to give much more help. Admitting to your tutor/teacher that you're stuck *isn't* a sign of weakness or failure, and they're more likely to be able to "prompt" you based on what they've taught you already. – Damien_The_Unbeliever Apr 17 '19 at 09:21
  • What you said before about the 13 patterns helped a LOT. I was able to figure it out from there. Thanks for your help. – Ryan Apr 17 '19 at 12:03

2 Answers2

1

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

Ryan
  • 19
  • 1
  • 6
0

You don't need images for cards. You already have them. Press from alt + 3 to alt + 6 (on numericpad)

Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♥ ♦ ♣ ♠");
vik_78
  • 1,107
  • 2
  • 13
  • 20