Visualizing Project Euler Problem 15 using C# Winforms | Lattice Paths

Mapping paths..

public static Brush aBrush = (Brush)Brushes.Black;
public static string path_collect = "";
public static LinkedList<string> complete_paths = new LinkedList<string>();
public bool wall = false;
public bool floor = false;
 
public static int miliseconds = 15;
 
public int current_x = 10;
public int current_y = 10;
 
 
public Form1()
{
    InitializeComponent();
}
 
 
private void Form1_Load(object sender, EventArgs e)
{
    label3.Text = miliseconds + "ms";
}
 
private void Form1_Paint(object sender, PaintEventArgs e)
{
    int x = 10;
    int y = 10;
    for (int i = 1; i < 21; i++)
    {
        for (int j = 1; j < 21; j++)
        {
            e.Graphics.DrawEllipse(Pens.Black, x* i , y *j, 6, 6);
        }
    }
}
 
private void bntPath_Click(object sender, EventArgs e)
{
    bntPath.Enabled = false;
    backgroundWorker1.RunWorkerAsync();
}
 
 
protected void Move(string path)
{
    Graphics g = this.CreateGraphics();
 
    foreach (char c in path)
    {
        if (c == '0')
        {
            g.FillRectangle(aBrush, current_x + 10, current_y, 10, 10);
            current_x += 10;
            path_collect += "→";
        }
        else if (c == '1')
        {
            g.FillRectangle(aBrush, current_x, current_y + 10, 10, 10);
            current_y += 10;
            path_collect += "↓";
        }
    }
 
    current_x = 10;
    current_y = 10;
 
    label2.Text = "New Unique Path\r\n" + path_collect;
 
    Brush NewBrush = new SolidBrush(GetRandomColor());
    aBrush = NewBrush;
    path_collect = "";
 
}
 
 
private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
{
    /*
    while (true) 
    {
        MoveRandom();
        Thread.Sleep(miliseconds);
    }
        */
    Int64 i = 0;
    Int64 count_unique_paths = 0;
 
 
    while (true)
    {
        string bits = Convert.ToString(i, 2).PadLeft(38, '0');
        string check = bits;
        check = Regex.Replace(check, "0", "");
        if (check.Length == 19)
        {
            Move(bits);
            //Thread.Sleep(50);
            count_unique_paths += 1;
            label4.Text = "Distinct Paths " + count_unique_paths;
        }
        i += 1;
    }
 
}
 
string row = "";
 
public bool completerow = true;
 
private Random random;
private Color GetRandomColor()
{
    random = new Random();
    return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}
 
protected void MoveRandom()
{
    Graphics g = this.CreateGraphics();
    Random rand = new Random();
    int random = rand.Next(0, 2);
 
    if (random == 0)
    {
        if (!wall)
        {
            g.FillRectangle(aBrush, current_x + 10, current_y, 10, 10);
            current_x += 10;
            path_collect += "→";
        }
    }
    else if (random == 1)
    {
        if (!floor)
        {
            g.FillRectangle(aBrush, current_x, current_y + 10, 10, 10);
            current_y += 10;
            path_collect += "↓";
        }
    }
    CheckWall();
}
 
protected void CheckWall()
{
    if (current_x == 200)
        wall = true;
    if (current_y == 200)
        floor = true;
    if (wall && floor)
    {
        current_x = 10;
        current_y = 10;
        wall = false;
        floor = false;
        if (!complete_paths.Contains(path_collect))
        {
            complete_paths.AddLast(path_collect);
            label1.Text = "Complete " + complete_paths.Count;
            label2.Text = "New Unique Path\r\n" + path_collect;
 
        }
        Brush NewBrush = new SolidBrush(GetRandomColor());
        aBrush = NewBrush;
        path_collect = "";
    }
}

//Some fun with random paths at different speeds.
speed 50-1ms

One thought on “Visualizing Project Euler Problem 15 using C# Winforms | Lattice Paths

Leave a Reply

Your email address will not be published. Required fields are marked *