Starbeamrainbowlabs

Stardust
Blog


Archive


Mailing List Articles Atom Feed Comments Atom Feed Twitter Reddit Facebook

Tag Cloud

3d 3d printing account algorithms android announcement architecture archives arduino artificial intelligence artix assembly async audio automation backups bash batch blender blog bookmarklet booting bug hunting c sharp c++ challenge chrome os cluster code codepen coding conundrums coding conundrums evolved command line compilers compiling compression conference conferences containerisation css dailyprogrammer data analysis debugging defining ai demystification distributed computing dns docker documentation downtime electronics email embedded systems encryption es6 features ethics event experiment external first impressions freeside future game github github gist gitlab graphics guide hardware hardware meetup holiday holidays html html5 html5 canvas infrastructure interfaces internet interoperability io.js jabber jam javascript js bin labs latex learning library linux lora low level lua maintenance manjaro minetest network networking nibriboard node.js open source operating systems optimisation outreach own your code pepperminty wiki performance phd photos php pixelbot portable privacy problem solving programming problems project projects prolog protocol protocols pseudo 3d python reddit redis reference release releases rendering research resource review rust searching secrets security series list server software sorting source code control statistics storage svg systemquery talks technical terminal textures thoughts three thing game three.js tool tutorial twitter ubuntu university update updates upgrade version control virtual reality virtualisation visual web website windows windows 10 worldeditadditions xmpp xslt

Easy C♯ Menus

I have written some easy to use C♯ menus, and I thought that I would post about them here.

There are currently 2 different methods (and 2 extra helper methods). Both methods also centre the dialog box in the middle of the console, and also don't produce much mess that needs cleaning up afterwards.

Firstly, here are the two helper methods:

//from https://stackoverflow.com/questions/17590528/pad-left-pad-right-pad-center-string
///<summary>
///Cool function from stackoverflow that pads a string on both sides to make it a given legnth.
///</summary>
///<param name="source">The source string to pad.</param>
///<param name="length">The desired length.</param>
///<returns>The padded string.</returns>
static string PadBoth(string source, int length)
{
    int spaces = length - source.Length;
    int padLeft = spaces/2 + source.Length;
    return source.PadLeft(padLeft).PadRight(length);
}

//utility function that uses the above to pad a string to the current width of the console.
static string PadToWindowWidth(string str)
{
    return PadBoth(str, Console.WindowWidth - 1);
}

These need to be included in addition to either (or both!) of the methods described below.

A screenshot of the first menu type. The first one is a flexible multiple choice selection window. You can pass in an array of string sthat you want the user to choose from, and the method will deal with the rest, returning the index in the array of the item that the user chose.

This method comes with support for an optional prompt to display at the top of the dialog box. If you omit it, the prompt will not be displayed.

Source code:

///<summary>
///Displays a nice multiple choice menu that the user can intract with using the arrow keys.
///</summary>
///<param name="options">An array of strings that should be used as the possible options in the menu.</param>
///<returns>The index of the option the user chose.</returns>
static int DisplayMenu(string[] options, string prompt = "")
{
    int cursorstartx = Console.CursorLeft,
        cursorstarty = Console.CursorTop,

        menuWidth = (int)(Console.WindowWidth * 0.8),
        menuHeight = 2 + (options.Length * 2) + 1;

    if(prompt.Length > 0)
        menuHeight += 2;

    int currentIndex = 0;

    string menu = "";

    while(true)
    {
        Console.SetCursorPosition(cursorstartx, cursorstarty);

        menu = "";
        menu += new String('\n', ((Console.WindowHeight - 1) - menuHeight) / 2);
        menu += PadToWindowWidth("".PadLeft(menuWidth, '-')) + "\n";

        if(prompt.Length > 0)
        {
            menu += PadToWindowWidth("| " + PadBoth(prompt, menuWidth - 4) + " |") + "\n";
            menu += PadToWindowWidth("".PadLeft(menuWidth, '-')) + "\n";
        }

        menu += PadToWindowWidth("|" + "".PadLeft(menuWidth - 2) + "|") + "\n";

        for(int i = 0; i < options.Length; i++)
        {
            if(currentIndex == i)
            {
                menu += PadToWindowWidth("|" + PadBoth("> " + options[i] + " <", menuWidth - 2) + "|") + "\n";
            }
            else
            {
                menu += PadToWindowWidth("|" + PadBoth(options[i], menuWidth - 2) + "|") + "\n";
            }
            menu += PadToWindowWidth("|" + "".PadLeft(menuWidth - 2) + "|") + "\n";
        }
        menu += PadToWindowWidth("".PadLeft(menuWidth, '-'));
        menu += new String('\n', ((Console.WindowHeight - 1) - menuHeight) / 2);

        Console.WriteLine(menu);

        ConsoleKeyInfo nextkey = Console.ReadKey(true);

        switch(nextkey.Key.ToString())
        {
            case "UpArrow":
                currentIndex--;
                break;

            case "DownArrow":
                currentIndex++;
                break;

            case "Enter":
                return currentIndex;
        }

        if(currentIndex < 0)
            currentIndex = options.Length - 1;
        if(currentIndex > options.Length - 1)
            currentIndex = 0;
    }
}

Confirm Dialog

A screenshot of the second menu type.

In case you want to obtain an answer to a simple yes/no question, this second method allows you to ask the user to choose between 2 choices. Simply specify a prompt, and optionally the text to display in the place of the "Yes" / "No", and the method will return true or false, depending on which one the user selected.

Source code:

///<summary>
///Asks the user a simple yes/no question in the form of a console based dialog box.
///</summary>
///<param name="prompt">The question to ask the user.</param>
///<param name="trueText">The text to display in the place of "Yes"</param>
///<param name="falseText">The text to display in the place of "No"</param>
///<returns>True if the user selected "Yes", or false if the user selected "No".</returns>
static bool DisplayConfirm(string prompt, string trueText = "Yes", string falseText = "No")
{
    int cursorstartx = Console.CursorLeft,
        cursorstarty = Console.CursorTop,

        menuWidth = (int)(Console.WindowWidth * 0.8),
        menuHeight = 7,

        currentIndex = 1;

    string menu = "";

    while(true)
    {
        Console.SetCursorPosition(cursorstartx, cursorstarty);

        menu = "";
        menu += new String('\n', ((Console.WindowHeight - 1) - menuHeight) / 2); //vertical centring

        menu += PadToWindowWidth("".PadLeft(menuWidth, '-')) + "\n"; //dashes
        menu += PadToWindowWidth("|" + "".PadLeft(menuWidth - 2) + "|") + "\n"; //space

        menu += PadToWindowWidth("|" + PadBoth(prompt, menuWidth - 2) + "|") + "\n"; //prompt
        menu += PadToWindowWidth("|" + "".PadLeft(menuWidth - 2) + "|") + "\n"; //space
        menu += PadToWindowWidth("".PadLeft(menuWidth, '-')) + "\n"; //dashes

        if(currentIndex == 0)
            menu += PadToWindowWidth("|" + PadBoth(trueText, (menuWidth - 3) / 2) + "|" + PadBoth("> " + falseText + " <", (menuWidth - 3) / 2) + "|") + "\n";
        else
            menu += PadToWindowWidth("|" + PadBoth("> " + trueText + " <", (menuWidth - 3) / 2) + "|" + PadBoth(falseText, (menuWidth - 3) / 2) + "|") + "\n";

        menu += PadToWindowWidth("".PadLeft(menuWidth, '-')) + "\n"; //dashes

        menu += new String('\n', ((Console.WindowHeight - 1) - menuHeight) / 2); //vertical centring

        Console.WriteLine(menu);

        ConsoleKeyInfo nextkey = Console.ReadKey(true);

        switch(nextkey.Key.ToString())
        {
            case "LeftArrow":
            case "UpArrow":
                currentIndex--;
                break;

            case "RightArrow":
            case "DownArrow":
                currentIndex++;
                break;

            case "Enter":
                if(currentIndex == 0)
                    return true;
                else
                    return false;
        }

        if(currentIndex < 0)
            currentIndex = 1;
        if(currentIndex > 1)
            currentIndex = 0;
    }
}

I will probably write a few more of these in the future, and make these current ones better.

Demonstration binaries are available upon request, simply leave a comment below.

Reverse Bubble Sorting

We had our second algorithms lecture this week - this time is was on bubble sorting. Apparently we will be doing several sorting algorithms over the next few weeks, each with their own strengths and weaknesses.

Today I bring you an optimised bubble sort implementation in C♯! This will be the first C♯ code that I have posted on this blog.

Basically, the bubble sort algorithm iterates over an array of numbers repeatedly and swaps those that are in the wrong order, until there aren't any more numbers left to swap.

Here is the script:

static void DoBubbleSort(int[]arraytosort) {
    int endingpoint = 1,
        temp;
    bool issorted;

    int swaps = 0,
        iterations = 0,
        passes = 0; //debug

    do {
        issorted = true;

        for(int i = arraytosort.Length - 1; i >= endingpoint; i--)
        {
            iterations++; //debug
            //Console.WriteLine("i: " + i + " i-1: " + (i - 1));

            if (arraytosort[i - 1] > arraytosort[i])
            {
                swaps++; //debug
                //swap the numbers around
                temp = arraytosort[i - 1];
                arraytosort[i - 1] = arraytosort[i];
                arraytosort[i] = temp;

                issorted = false;
            }
        }

        Console.Write("pass: " + passes + " ");
        printarray(arraytosort); //debug
        Console.WriteLine();

        passes++; //debug

        endingpoint++;
    } while (!issorted);

    Console.WriteLine("Sorting Complete!");
    Console.WriteLine("Statistics\n----------");
    Console.WriteLine("Passes: " + passes + ", Iterations: " + iterations + " Swaps: " + swaps);
}

...and here is an example of what it outputs:

Original: [66, 51,  0,  5, 42, 92,  8,  8, 28,  8]
pass: 0 [ 0, 66, 51,  5,  8, 42, 92,  8,  8, 28]
pass: 1 [ 0,  5, 66, 51,  8,  8, 42, 92,  8, 28]
pass: 2 [ 0,  5,  8, 66, 51,  8,  8, 42, 92, 28]
pass: 3 [ 0,  5,  8,  8, 66, 51,  8, 28, 42, 92]
pass: 4 [ 0,  5,  8,  8,  8, 66, 51, 28, 42, 92]
pass: 5 [ 0,  5,  8,  8,  8, 28, 66, 51, 42, 92]
pass: 6 [ 0,  5,  8,  8,  8, 28, 42, 66, 51, 92]
pass: 7 [ 0,  5,  8,  8,  8, 28, 42, 51, 66, 92]
pass: 8 [ 0,  5,  8,  8,  8, 28, 42, 51, 66, 92]
Sorting Complete!
Statistics
----------
Passes: 9, Iterations: 45 Swaps: 24

The script keeps track of the furthest point in the array it reached on each pass and goes one less each time - this is because the smallest number will always get pushed into its proper place at the left hand side on each pass.

As for the reason the script iterates backwards, in Javascript it is recommended that you iterate backwards to avoid repeatedly referencing Array.length, since it has to count the contents of an array upon each refernce. This is probably not the case with C♯, but it is a habit of mine :)

It is important to note that even though the function doesn't return anything, it still sorts the array because arrays are passed by reference by default, just like in Javascript (aka Ecmascript).

There are quite a few debug statements in there. Remove then for actual use in your code.

A (64 bit) compiled version of the script is available:

reversebubblesort.exe

Hashes:

Algorithm Hash
CRC32 644f6c6a
MD5 93fba7a072954ee6f34fcf44913eadc7
SHA1 01a54b24c475ec2ff1bf159dc1224e10553f430d
SHA-256 d32d689e2785d738c54e43a9dc70c1d8f2de76383022a87aa4f408519a7941cb
SHA-384 df7c4ac441aabaa1f182ade7532885d8ee5518c26f17d72d7952dcfaa39552dda9ad219a37661591fea169fd6ed514bb
SHA-512 c993509901bb65cd893d1c8455c5ad8dc670632e5476aad899980348b45bc3435cfab3fe6d8fd80606cfea3608770c9900be51e09f6f1a8c9fd5fe28169fd81d

Remember to always verify the integrity of your downloaded files, especially the larger ones. If you would like another type of binary (e.g. 32 bit, ARM, etc.), please post a comment below and I will reply with a download link. The compiler used was csc.exe on a Windows 7 64 bit command line.

Questions and / or comments are welcome below.

First Impressions: C♯

I will be learning C♯ over the next year or so, and since I am almost a week into learning it, I thought that I would post my first impressions here on this blog.

C♯ is a compiled language developed by Microsoft that looks a little bit javascript, and a little bit like Rust (If you haven't heard of rust yet, I have written up a first impressions post here). Like rust, it has a type system, so you can't set a variable with the type int to a string. This type system is not a confusing, though, since it only has a single type for strings (rust has two - one that you can change and one that you can't) and you can convert between the different types quite easily (e.g. int.Parse() converts a string to an int).

The brackets syntax is almost identical to that of Javascript, except C♯ has classes and namespacees too (I don't really know what those do yet). Indentation also seems to be fairly important, which is perfectly fine by me since it improves the readability of your code.

Looking around on the internet, it seems that C♯ is tied in with Visual Studio quite closely. This might cause problems in the future, because I don't really want ot install visual studio (express) on my computer because of it's file size (the installer alone is > 600MB!). Hopefully I can continue to use the csc C♯ compiler I found in the .NET 4.0 folder on my computer as my code becomes more complex.

All in all C♯ looks like a good introductory language to the world of compiled programming. The syntax is straight forward and easy to understand so far, and it is kind of similar to Javascript, which eases the learning process considerably.

Art by Mythdael