C#找到板上的路径 [英] C# to find the path on the board

查看:75
本文介绍了C#找到板上的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The board has 10 row and 10 Column buttons all Green color the board randomly generate 9 red color obstacle. My question is to find the path (changing the green color to white color)starting from entry to exit going horizontally and vertically using only the Green color button.Every time when i hit Go button the green button will change in to white.The program only find one path at a time. for example the first path execute on (2(10)-1) then when i hit Go button again it will find another path. This will  continue until all the green button changing in to white. btn 1 is the entry and btn 100 is the exit. i use the click event .....?







file:///C:/Users/Tesfahun/Desktop/c2.png





我的尝试:





What I have tried:

for (int i = 1; i <= 99;)
    {
 
        string btn_name = "btn" + Convert.ToString(i+10);
        var btn_now = this.Controls.Find(btn_name, true)[0];
 
        btn_name = "btn" + Convert.ToString(i + 1);
        var btn_next = this.Controls.Find(btn_name, true)[0];
        var btn_below = btn_next;
 

 
        if (i <= 10 || i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i == 70 || i == 80 || i == 90)
        {
            btn_name = "btn" + Convert.ToString(i);
            btn_below = this.Controls.Find(btn_name, true)[0];
        }
        if(btn_next.BackColor == Color.GreenYellow) {
 
            if (i <= 10 || i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i == 70 || i == 80 || i == 90)
                i+= 1;
            else
                i++;
           btn_now = btn_next;
        }
        else {
            btn_now = btn_next;
            i+=1;                    
        }
        btn_now.BackColor = Color.White;
 
    }

推荐答案

这不是一个好的开始!

看看你的代码 - 它显示了没有被考虑的明显迹象:

That's not a good start!
Look at your code - it shows clear signs of not being thought about:
if (i <= 10 || i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i == 70 || i == 80 || i == 90)
    i+= 1;
else
    i++;

如果我删除条件会更清楚:

If I cut out the condition it becomes clearer:

if (...)
    i+= 1;
else
    i++;

这相当于:

Which is the equivalent of:

i++;



在这里使用按钮是一个坏主意,但我可以理解你为什么这么做 - 这是简单的解决方案 - 但是使用面板并且它自己做Paint事件将是一个更好的方法。

尝试一下:将按钮设置为二维数组(10 x 10),而不是使用名称和数字,以及你应该更容易看到你在做什么。然后开始考虑而不是直接跳入代码!


Using buttons is a bad idea here, but I can understand why you have done it - it's the "simple" solution - but using a panel and it's Paint event to do them yourself would be a much better approach.
Try something: set up your buttons as a 2 dimensional array (10 x 10) instead of using the name and a number, and it should be easier to see what you are doing. Then start thinking instead of jumping right into code!


这篇关于C#找到板上的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆