Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF c#刽子手游戏:禁用错误字母并显示错误消息和2个其他问题_C#_Wpf - Fatal编程技术网

WPF c#刽子手游戏:禁用错误字母并显示错误消息和2个其他问题

WPF c#刽子手游戏:禁用错误字母并显示错误消息和2个其他问题,c#,wpf,C#,Wpf,好了,伙计们,这是我的代码: private void btn_Click(object sender, RoutedEventArgs e) { MessageBoxResult confirmatBoxResult = System.Windows.MessageBox.Show( "Êtes-vous sûr ?", "Confirmer", System.Windows.MessageBoxButton.YesNo); if (confirmatBoxR

好了,伙计们,这是我的代码:

private void btn_Click(object sender, RoutedEventArgs e)
{
    MessageBoxResult confirmatBoxResult = System.Windows.MessageBox.Show(
        "Êtes-vous sûr ?", "Confirmer", System.Windows.MessageBoxButton.YesNo);

    if (confirmatBoxResult == MessageBoxResult.Yes)
    {
        for (char c = 'a'; c <= 'z'; c++)
        {
            char ch = c;
            Button btn = new Button();
            btn.Content = c;
            btn.Width = 60;
            btn.Height = 60;
            btn.FontSize = 36;

            panel_lettre.Children.Add(btn);
            lookupLetters[ch] = new List<Label>();
            btn.Click += new RoutedEventHandler(btnLetter_Click);

            void btnLetter_Click(object sender2, RoutedEventArgs e2)
            {
                if (lookupLetters.TryGetValue(ch, out List<Label> textList))
                {
                    foreach (var el in textList)
                    {
                        el.Content = ch;
                        btn.IsEnabled = false;
                    }
                }
                else
                {
                    foreach (var el in textList)
                    {
                        el.Content = ch;
                        btn.IsEnabled = false;
                        // not correct
                    }
                }
            }
        }

        foreach (char ch in txtMot.Text)
        {
            Label Lbl = new Label();
            Lbl.Content = ch;
            Lbl.Content = "_";
            Lbl.FontSize = 36;
            Lbl.Width = 30;
            lookupLetters[ch].Add(Lbl);
            panel_label.Children.Add(Lbl);
        }

        btnMotProposé.IsEnabled = false;
    }
}
private void btn\u单击(对象发送者,路由目标)
{
MessageBoxResult确认BoxResult=System.Windows.MessageBox.Show(
“您愿意吗?”,“确认人”,System.Windows.MessageBoxButton.YesNo);
if(confirmatimBoxResult==MessageBoxResult.Yes)
{

对于(char c='a';c,以下是我对您所寻找内容的粗略版本:

public partial class MainWindow : Window
{
    private int MaxAttempts = 4;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnMotPropose_Click(object sender, RoutedEventArgs e)
    {
        MessageBoxResult confirmatBoxResult = System.Windows.MessageBox.Show(
            "Êtes-vous sûr ?", "Confirmer", System.Windows.MessageBoxButton.YesNo);

        var lookupLetters = new Dictionary<char, List<Label>>();

        if (confirmatBoxResult == MessageBoxResult.Yes)
        {
            int attemptCounter = 0;

            for (char c = 'a'; c <= 'z'; c++)
            {
                char ch = c;
                Button btn = new Button();
                btn.Content = c;
                btn.Width = 60;
                btn.Height = 60;
                btn.FontSize = 36;

                panel_lettre.Children.Add(btn);
                lookupLetters[ch] = new List<Label>();
                btn.Click += new RoutedEventHandler(btnLetter_Click);

                void btnLetter_Click(object sender2, RoutedEventArgs e2)
                {
                    if (lookupLetters.TryGetValue(ch, out List<Label> textList))
                    {
                        attemptCounter++;

                        foreach (var el in textList)
                        {
                            el.Content = ch;
                            btn.IsEnabled = false;
                        }

                        if (!textList.Any())
                        {
                            btn.IsEnabled = false;
                            MessageBox.Show("Wrong letter!");
                        }

                        if (attemptCounter == MaxAttempts)
                        {
                            MessageBox.Show("Max attempts reached!");
                            ShowAnswer(lookupLetters);
                        }

                        if (AreAllLettersUncovered())
                        {
                            MessageBox.Show("Well done, you've found the word !!");
                        }
                    }
                }
            }

            foreach (char ch in txtMot.Text)
            {
                Label Lbl = new Label();                    
                Lbl.Content = "_";
                Lbl.FontSize = 36;
                Lbl.Width = 30;
                lookupLetters[ch].Add(Lbl);
                panel_label.Children.Add(Lbl);
            }

            btnMotPropose.IsEnabled = false;
        }
    }

    private bool AreAllLettersUncovered()
    {
        return panel_label.Children.Cast<Label>().All(p => (string)p.Content != "_");
    }

    private void ShowAnswer(Dictionary<char, List<Label>> lookupLetters)
    {
        var lettersWithLabels = lookupLetters.Where(p => p.Value.Any());
        foreach (var letterWithLabel in lettersWithLabels)
        {
            foreach (var label in letterWithLabel.Value)
            {
                label.Content = letterWithLabel.Key.ToString();
            }
        }
    }
}
公共部分类主窗口:窗口
{
私有整数最大尝试数=4;
公共主窗口()
{
初始化组件();
}
私有void btnmotropose\u单击(对象发送方,路由目标e)
{
MessageBoxResult确认BoxResult=System.Windows.MessageBox.Show(
“您愿意吗?”,“确认人”,System.Windows.MessageBoxButton.YesNo);
var lookupleters=新字典();
if(confirmatimBoxResult==MessageBoxResult.Yes)
{
int attemptCounter=0;
for(char c='a';c(string)p.Content!=“”);
}
私有void ShowAnswer(字典查找器)
{
var lettersWithLabels=lookupleters.Where(p=>p.Value.Any());
foreach(var letterWithLabel,字体标签为lettersWithLabels)
{
foreach(letterWithLabel.Value中的var标签)
{
label.Content=letterWithLabel.Key.ToString();
}
}
}
}

作为旁注-我将动态点击事件处理程序解耦为一个单独的方法-这将使代码更具可读性。主要的onClick方法很长,不容易阅读。您的命名约定与变量太变量不同(有时与下划线、英语/非英语混合)。当命名约定不变时,更容易阅读代码。

btn.Click+=newroutedeventhandler(btnLetter\u Click);您应该先取消订阅或订阅类的构造函数中的事件处理程序,而不是单击事件。否则,每次额外订阅都会触发多次。@SimonKatanski事件处理程序分配是为一个全新的按钮,不是吗?没有创建多个订阅…@RufusL true,my bad。按钮n位于局部作用域中,其处理程序不依赖于任何全局变量,这也很好。你说得对,很抱歉我删除了我以前的消息,这是一个错误!我没有阅读你的所有代码,但这里有一个建议:不要尝试在一个方法中完成所有工作。尝试使用自己的方法拆分工作,如“addButtons”,“支票”、“刷新标签”等。非常感谢,我会考虑您所说的一切,并改进我的代码。谢谢!
public partial class MainWindow : Window
{
    private int MaxAttempts = 4;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnMotPropose_Click(object sender, RoutedEventArgs e)
    {
        MessageBoxResult confirmatBoxResult = System.Windows.MessageBox.Show(
            "Êtes-vous sûr ?", "Confirmer", System.Windows.MessageBoxButton.YesNo);

        var lookupLetters = new Dictionary<char, List<Label>>();

        if (confirmatBoxResult == MessageBoxResult.Yes)
        {
            int attemptCounter = 0;

            for (char c = 'a'; c <= 'z'; c++)
            {
                char ch = c;
                Button btn = new Button();
                btn.Content = c;
                btn.Width = 60;
                btn.Height = 60;
                btn.FontSize = 36;

                panel_lettre.Children.Add(btn);
                lookupLetters[ch] = new List<Label>();
                btn.Click += new RoutedEventHandler(btnLetter_Click);

                void btnLetter_Click(object sender2, RoutedEventArgs e2)
                {
                    if (lookupLetters.TryGetValue(ch, out List<Label> textList))
                    {
                        attemptCounter++;

                        foreach (var el in textList)
                        {
                            el.Content = ch;
                            btn.IsEnabled = false;
                        }

                        if (!textList.Any())
                        {
                            btn.IsEnabled = false;
                            MessageBox.Show("Wrong letter!");
                        }

                        if (attemptCounter == MaxAttempts)
                        {
                            MessageBox.Show("Max attempts reached!");
                            ShowAnswer(lookupLetters);
                        }

                        if (AreAllLettersUncovered())
                        {
                            MessageBox.Show("Well done, you've found the word !!");
                        }
                    }
                }
            }

            foreach (char ch in txtMot.Text)
            {
                Label Lbl = new Label();                    
                Lbl.Content = "_";
                Lbl.FontSize = 36;
                Lbl.Width = 30;
                lookupLetters[ch].Add(Lbl);
                panel_label.Children.Add(Lbl);
            }

            btnMotPropose.IsEnabled = false;
        }
    }

    private bool AreAllLettersUncovered()
    {
        return panel_label.Children.Cast<Label>().All(p => (string)p.Content != "_");
    }

    private void ShowAnswer(Dictionary<char, List<Label>> lookupLetters)
    {
        var lettersWithLabels = lookupLetters.Where(p => p.Value.Any());
        foreach (var letterWithLabel in lettersWithLabels)
        {
            foreach (var label in letterWithLabel.Value)
            {
                label.Content = letterWithLabel.Key.ToString();
            }
        }
    }
}