Colorful ASCII Christmas Tree in C# by ChatGPT

Visual Studio .NET Core

one year ago

This article is part of C# Advent 2022 event! Thanks Matthew D. Groves for organizing it again this year! Source code available on my GitHub.

Preface

This December I spent considerable time playing with ChatGPT - an AI language model developed by OpenAI that can generate human-like text when given a prompt. It has the ability to write articles, stories, and code, and can engage in conversations with users in a natural and believable way. Impressed by what it can do, I decided to push its limits and ask it to write a Christmas tree generator program in C#, going through several rounds of improvements to arrive at the final result. To go one step further, I asked it to write the accompanying blogpost as well! This post then consists of two parts. The first generated fully by ChatGPT (including the code), the second written by me, explaining the steps I took to arrive at this result. Let's begin!

Blogpost by ChatGPT

Christmas is just around the corner, and what better way to get into the holiday spirit than by drawing a beautiful Christmas tree using ASCII art? In this article, we'll go through the steps of creating a C# program that draws a colorful Christmas tree with randomly placed ornaments and a brown trunk.

The Code

Here is the complete source code for the program:

How It Works

The program starts by parsing the first command line argument as the width of the tree. It then calculates the number of rows needed for the top of the tree based on the width. Next, it creates a random number generator to use for choosing the colors of the ornaments and shuffling their positions. The program then enters a loop to draw the top of the tree. For each row, it calculates the number of asterisks and spaces needed, chooses a random number of ornaments, creates a list of ornaments and asterisks, shuffles the ornaments, and finally draws the row. The ornaments are given random colors by setting the System.Console.ForegroundColor property to a random color before printing each ornament. After the top of the tree has been drawn, the program draws the trunk using the System.Console.ForegroundColor of DarkRed . After the trunk has been drawn, the program resets the console color and prints a message wishing everyone a "Merry Christmas!".

Running the Program

To run the program, you will need to have the .NET framework installed on your machine. You can then compile the program using the csc command:

This will create an executable file called ChristmasTree.exe. You can then run the program by specifying the width of the tree as a command line argument:

This will draw a Christmas tree with a width of 7 characters. You can try different widths to see how the tree looks with different sizes.

Example output

ASCII Christmas Tree

ASCII Christmas Tree

Conclusion

I hope you enjoyed this tutorial on how to draw a colorful Christmas tree using ASCII art in C#. Happy holidays and have a merry Christmas!

Explanation by me, the human

Pretty cool, huh? So how did me conversation with ChatGPT look like to arrive at this result? It all started quite innocently with this prompt:

First prompt asking ChatGPT to generate Christmas Tree

First prompt asking ChatGPT to generate Christmas Tree

Nice and easy. But quite a small tree, right? I wanted it bigger.

Prompting ChatGPT for a bigger tree

Prompting ChatGPT for a bigger tree

Well, it is bigger, but just by one column. I tried to get a more general solution:

Asking ChatGPT to use input arguments for size of the tree

Asking ChatGPT to use input arguments for size of the tree

Very nice, in this form the tree is already looking pretty nice!

A plain ASCII tree

A plain ASCII tree

However, without the Merry Christmas, it would be hard to guess this is actually a Christmas tree! I decided there should be some ornaments added:

Prompt to ChatGPT to add some ornaments on the tree

Prompt to ChatGPT to add some ornaments on the tree

The generated code didn't go too well though:

Generated code for tree ornaments

Generated code for tree ornaments

As you can see, the new code does add o characters as ornaments, but suddenly omits the tree itself!

Ornament-only tree

Ornament-only tree

Ok, let's try again:

Asking ChatGPT to fix the tree

Asking ChatGPT to fix the tree

And it did work!

The tree asterisks are back!

The tree asterisks are back!

Now the tree looks like this:

Getting there, slowly!

Getting there, slowly!

As we can see though, the tree is now all golden. While nice, let's try to adjust the code, so that the ornaments have random color, the tree itself is green and the trunk is brown:

Asking ChatGPT to add colors

Asking ChatGPT to add colors

With that, the provided code generated this beauty:

Colorful tree

Colorful tree

Now just a final adjustment - center the text horizontally and separate it from the tree with a blank line:

Getting the text centered

Getting the text centered

Et voilà! With this I have received the final source code you could see in the first part of the article. And then I tried to go one more step further:

Asking ChatGPT to generate a blogpost

Asking ChatGPT to generate a blogpost

And what about GitHub README.md?

Generating README.md

Generating README.md

All neatly prepared by ChatGPT - and it even included itself in the Acknowledgements!

Summary

As you can see having the power of AI under our fingertips has the potential of accelerating not only software-development, but any creative and knowledge-based effort in general. However, as we have also seen, even though the AI can generate a valid C# code, that compiles, it is not always perfect and may not understand all requirements properly. And that's where we, as humans need to come in. I wholeheartedly recommend you to try out ChatGPT, and imagine scenarios, where it could help you speed up your work. Thank you for reading this far, have Merry Christmas and Happy New Year! And make sure to spend time with all your loved ones as much as possible!

Our ASCII Christmas Tree

Our ASCII Christmas Tree