WPF C# Hangman猜单词游戏:错误字母禁用等功能实现求助
解决方案:针对你的三个问题的修改方案
嘿,我仔细看了你的代码和需求,这三个功能都可以通过在现有逻辑上补充状态跟踪和判断来实现,下面是具体的修改步骤和完整代码:
首先,我们需要先在类里添加一个变量来跟踪错误尝试的次数:
private int wrongAttempts = 0; // 记录错误次数
问题1:点击不存在的字母时禁用按钮并弹出提示
在你的btnLetter_Click事件里,当lookupLetters.TryGetValue(ch, out List<Label> textList)返回false时,就说明这个字母不在目标单词里。这时候我们只需要弹出提示、禁用当前按钮,同时增加错误次数即可:
void btnLetter_Click(object sender2, RoutedEventArgs e2) { Button clickedBtn = sender2 as Button; // 获取当前点击的按钮 if (lookupLetters.TryGetValue(ch, out List<Label> textList)) { // 原有的正确字母逻辑 foreach (var el in textList) { el.Content = ch; } clickedBtn.IsEnabled = false; // 检查是否猜对了整个单词 CheckIfWordGuessed(); } else { // 错误字母的处理逻辑 System.Windows.MessageBox.Show("Wrong letter !"); clickedBtn.IsEnabled = false; wrongAttempts++; // 检查是否达到4次错误 CheckIfMaxWrongAttemptsReached(); } }
问题2:单词完全猜出时弹出成功提示
我们可以写一个辅助方法,用来检查所有标签的内容是否已经没有下划线了:
private void CheckIfWordGuessed() { bool allLettersGuessed = true; foreach (Label lbl in panel_label.Children) { if (lbl.Content.ToString() == "_") { allLettersGuessed = false; break; } } if (allLettersGuessed) { System.Windows.MessageBox.Show("Well done, you've found the word !"); // 可以选择禁用所有字母按钮,防止继续操作 DisableAllLetterButtons(); } } // 辅助方法:禁用所有字母按钮 private void DisableAllLetterButtons() { foreach (Button btn in panel_lettre.Children) { btn.IsEnabled = false; } }
每次点击正确字母替换完成后,调用这个方法即可判断是否猜对了整个单词。
问题3:累计4次错误后自动显示单词
同样,我们写一个辅助方法来检查错误次数是否达到4次,如果达到就把所有标签的内容替换成正确的字母:
private void CheckIfMaxWrongAttemptsReached() { if (wrongAttempts == 4) { // 显示完整单词 foreach (var kvp in lookupLetters) { foreach (Label lbl in kvp.Value) { lbl.Content = kvp.Key; } } // 禁用所有字母按钮,结束游戏 DisableAllLetterButtons(); System.Windows.MessageBox.Show($"Game over ! The word was: {txtMot.Text}"); } }
每次错误后调用这个方法,就会在第4次错误时自动显示完整单词。
完整修改后的代码
// 新增错误次数变量 private int wrongAttempts = 0; public Dictionary<char, List<Label>> lookupLetters = new Dictionary<char, List<Label>>(); 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) { // 清空之前的控件和数据,防止重复点击按钮时叠加 panel_lettre.Children.Clear(); panel_label.Children.Clear(); lookupLetters.Clear(); wrongAttempts = 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) { Button clickedBtn = sender2 as Button; if (lookupLetters.TryGetValue(ch, out List<Label> textList)) { foreach (var el in textList) { el.Content = ch; } clickedBtn.IsEnabled = false; // 检查是否猜对整个单词 CheckIfWordGuessed(); } else { System.Windows.MessageBox.Show("Wrong letter !"); clickedBtn.IsEnabled = false; wrongAttempts++; // 检查是否达到最大错误次数 CheckIfMaxWrongAttemptsReached(); } } } 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); } btnMotProposé.IsEnabled = false; } } // 检查是否猜对整个单词 private void CheckIfWordGuessed() { bool allLettersGuessed = true; foreach (Label lbl in panel_label.Children) { if (lbl.Content.ToString() == "_") { allLettersGuessed = false; break; } } if (allLettersGuessed) { System.Windows.MessageBox.Show("Well done, you've found the word !"); DisableAllLetterButtons(); } } // 检查是否达到4次错误尝试 private void CheckIfMaxWrongAttemptsReached() { if (wrongAttempts == 4) { foreach (var kvp in lookupLetters) { foreach (Label lbl in kvp.Value) { lbl.Content = kvp.Key; } } DisableAllLetterButtons(); System.Windows.MessageBox.Show($"Game over ! The word was: {txtMot.Text}"); } } // 禁用所有字母按钮 private void DisableAllLetterButtons() { foreach (Button btn in panel_lettre.Children) { btn.IsEnabled = false; } }
另外,我还帮你修复了一个小问题:在创建Label的时候,你之前连续写了两次Lbl.Content赋值,我保留了下划线的设置,这才符合猜单词游戏的初始状态。
内容的提问来源于stack exchange,提问作者Finalmix6
相关产品推荐
相关产品推荐

