jeudi 25 mai 2017

Random Walker loop issue for C#

I am working on a simple Random Walker program that should draw a 10p long line, then choose a random cardinal direction to draw another line (also 10p long) until a certain number of lines have been reached.

I am using four coordinates to draw the line (two coordinates for X and two for Y). The Y coordinates are pushed into a Stack after every line is drawn and they are popped out as the X coordinates. This should ensure that the starting point of every second line is the end-point of the previous line.

The program draws on a Windows Form after pushing a Button controller. As of now, the output is something like this:

Output of program...:-(

This here is my event handler code block for the button:

// Graphics and Pen classes instantiated

        Graphics graphics;
        graphics = this.CreateGraphics();

        Pen pen = new Pen(Color.Black);
        pen.Width = 1;

        // lineLength is 10 pixels
        // gridLength and gridWidth are needed to keep the Random Path inside a 600×600 field (this is not yet implemented in the code)
        // lineCount is for maximizing the number of lines to be drawn and to control the loop

        int lineLength = 10;
        int gridWidth = 600;
        int gridLength = 600;
        int lineCount = 0;

        // Starting line drawn with the following coordinates:
        int x1 = 20;
        int x2 = 20;
        int y1 = 20;
        int y2 = 30;

        graphics.DrawLine(pen, x1, y1, x2, y2);
        lineCount++;

        // Stack initialized to store "y" coordinates
        // "y" coordinates should be passed on as "x" coordinates for every consecutive lines
        // so that the ending point's coordinate of a line 

        Stack<int> stackY = new Stack<int>();
        stackY.Push(y2);
        stackY.Push(y1);

        for (lineCount = 1; lineCount <= 64; lineCount++)
        {
            // X pops current Y coordinates from stack
            x1 = stackY.Pop();
            x2 = stackY.Pop();

            // Initializing the random number (between 1 and 4) generator to choose from the cardinal directions
            Random rnd = new Random();
            int dir = rnd.Next(1, 5);

            switch (dir)
            {
                // up
                case 1:
                    y1 = y1 + lineLength;   // y1 plus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2); //drawing the line
                    stackY.Push(y2);    // pushing the current y coordinates into the stack
                    stackY.Push(y1);
                    break;

                // right
                case 2:
                    y1 = y2 + lineLength; // y2 plus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                    stackY.Push(y2);
                    stackY.Push(y1);
                    break;

                // down
                case 3:
                    y1 = y1 - lineLength; // y1 minus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                    stackY.Push(y2);
                    stackY.Push(y1);
                    break;

                // left
                case 4:
                    y2 = y2 - lineLength; // y2 minus lineLength
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                    stackY.Push(y2);
                    stackY.Push(y1);
                    break;

            } //switch
        } //for     

    } //event handler

I am not really sure what went wrong - I appreciate any heads-up and advices! Thank you!




Aucun commentaire:

Enregistrer un commentaire