Dictionary Vs Array | Game: Tower of Hanoi

What is Dictionary ? Whats the difference between Dictionary and a Array?

  • An array is just a sorted list of objects. A dictionary stores key-value pairs.
  • Both arrays and dictionaries are containers and can be read sequentally.
  • arrays can be enumerated by means of an index and dictionaries by means of a key.
  • Arrays maintain the order among objects, dictionaries not.
  • In addition, with dictionaries you have the possibility to access a specific object with a specific key in a more user-friendly way (a mnemonic one).

Code Example: Tower of Hanoi | Game

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static Dictionary<string, List> stacks = new Dictionary<string, List>()
    {
        { "a", new List() {5, 4, 3, 2, 1} },
        { "b", new List() {} },
        { "c", new List() {} }
    };

    public static void Main()
    {
        do
        {
            PrintStacks();
            Console.WriteLine("Move start:");
            string start = Console.ReadLine();

            Console.WriteLine("Move ends:");
            string finish = Console.ReadLine();

            //check if the move is legal
            if (IsLegal(start, finish))
            {
                MovePiece(start, finish);
            }
            else
            {
                Console.WriteLine("That move was ilegal, try again");
            }
        } while (!CheckForWin());

        PrintStacks();
        Console.WriteLine("Congrats, you won!");

        Console.ReadLine();
    }

    public static bool CheckForWin()
    {
        // 3 - Check for win

       

        if (stacks["a"].Count == 0 && stacks["b"].Count == 0)
            return true;

        return false;
    }

    public static void MovePiece(string start, string finish)
    {
        //  1 - Move a piece

        int lastElement = stacks[start].Last();

        // add lastElement into the finish pillar
        stacks[finish].Add(lastElement);

        // remove it from start pillar
        stacks[start].Remove(lastElement);
    }

    public static bool IsLegal(string start, string finish)
    {
        //  2 - Is it a legal move?

        // check for empty pillar - finish (List)
        if (stacks[finish].Count == 0)
            return true;

        // check for existing values on both pillars
        int lastNumberFromStart = stacks[start].Last();
        int lastNumberFromFinish = stacks[finish].Last();

        if (lastNumberFromStart < lastNumberFromFinish)
            return true;

        return false;
    }

    public static void PrintStacks()
    {
        string[] letters = new string[] { "a", "b", "c" };
        for (var i = 0; i < letters.Length; i++)
        {
            List blocks = new List();
            for (var j = 0; j < stacks[letters[i]].Count; j++)
            {
                blocks.Add(stacks[letters[i]][j].ToString());
            }
            Console.WriteLine(letters[i] + ": " + String.Join("|", blocks));
        }
    }
}
Advertisements

Leave a Reply