DEV Community

πŸ’Ύ bun9000
πŸ’Ύ bun9000

Posted on

Learning C# w/ @AlSweigart [Live Coding Recap]

Streamed: 12/06 on Twitch

πŸ”΄ Watch β‡’ Twitch VOD (replay) Part 1 Part 2

Had a fun, impromptu stream Friday night learning C# with @AlSweigart! :D We took a few of his Python stdio Games and remade them in C#.

Language(s) Used: [C# .NET Core 3.1]
Tech & lib(s) used: VSCode
Project Repository ⭐ CODE

πŸ‘‡ Code & notes from stream down below! :D

During the stream we...

βœ” got set up with C# .NET core 3.1
βœ” made a C# ver of a python app that converts texts sPoNgEboB meme
βœ” made a C# ver of a python app that races snails ....@v

Here's the code we wrote...

sPoNgEboB.cs

Console.WriteLine("c# sPoNgEtExTbY aL sWeIGaRt aNd niNjAbuNnY9000. cOmeNtEr YoUr MeSsAgE:");

// takes text input and passes into spongebob func
string input_text = string.Empty;
string output_text = "O K => ";
bool use_upper = false;
var rand =  new Random();

Console.WriteLine("wHaT Do yoU WaNt to cOnVerT?");

input_text = Console.ReadLine();

// for every character in the text
foreach (var c in input_text) {
    // check if it's an upper or lower
    if (use_upper) {
        output_text += char.ToUpper(c);  // change it to upper
    } else {
        output_text += char.ToLower(c);  // change it to lower
    }

    use_upper = !use_upper;  // flip the case

    // every 10 characters, randomly flip the case again
    if (rand.Next(1,10) == 1) {
        use_upper = !use_upper;  // ;D r YoU sUrE/?
    }
}

Console.WriteLine(output_text);
Enter fullscreen mode Exit fullscreen mode

SnailRace.cs

// TODO: input validation
static void Main(string[] args)
{
    // part 0) define vars
    const int MAX_NUM_SNAILS = 8;  // TODO
    const int MAX_NAME_LENGTH = 12; // TODO
    const int FINISH_LINE = 60;
    int num_snails = 0;
    var snail_names = new List<string>();
    var snail_progress = new Dictionary<string, int>();
    var rand =  new Random();
    Console.OutputEncoding = System.Text.Encoding.UTF8;

    // part 1) ask how many snails
    // TODO: validate for bad input
    Console.WriteLine("How many snails are racing today? (MAX 8?");
    num_snails = int.Parse(Console.ReadLine());

    // part 2) ask for the snail's names
    // TODO: implement character limit
    for(int i = 0; i < num_snails; i++) {
        Console.WriteLine($"What do you want to name snail #{i}?");
        snail_names.Add(Console.ReadLine());
        snail_progress.Add(snail_names[i], 0);
    }

    // part 3) print out the starting line setup
    Console.Clear();
    PrintStartingLine(FINISH_LINE);
    Thread.Sleep(1500);  // pause before starting the race

    bool racing = true;  // TODO make it a do-while later too

    // part 4) big while loop that does the race
    while (racing) {
        // pause and clear the screen between each frame
        Thread.Sleep(500);
        Console.Clear();

        // progress a random snail
        string moving_snail = snail_names[rand.Next(num_snails)];
        snail_progress[moving_snail] += 1;

        // check if snails have finished
        if (snail_progress[moving_snail] >= FINISH_LINE) {
            Console.WriteLine($"{moving_snail} has won!!");
            racing = false;  // exit the program
        }

        // render the snails to the console
        PrintStartingLine(FINISH_LINE);
        foreach (var snail in snail_names) {
            PrintSnail(snail, snail_progress[snail]);
        }
    }
}

// TODO: maths so starting line moves to accommodate name length
static void PrintStartingLine(int length) {
    // print starting line 60-spaces from the finish line
    var spaces = new string (' ', length);
    Console.WriteLine($"START{spaces}FINISH");
    Console.WriteLine($"    |{spaces}|");
}

// TODO: align the snail's names (better)
static void PrintSnail(string name, int pos) {
    // print spaces and then the snail's name
    var offset = new string (' ', 5);
    var spaces = new string (' ', pos);
    Console.WriteLine("   " + spaces + name);
    // print number of dots of position and the snail
    var dots = new string ('.', pos);
    Console.WriteLine("    " + dots + "@v");
}
Enter fullscreen mode Exit fullscreen mode

Live Coding Schedule

That pretty much wraps things up for this stream. If you have any questions about the code, Python, or any of the tech used in general - swing by during a live stream and say hello!

πŸ’œ Follow on Twitch

I stream Python coding & lame jokesβ„’ on Twitch every Tuesday, Thursday, & Saturday around 7pm PST.

For the most up to date schedule, check my pinned post on Twitter.

Discussion (0)