【问题标题】:Refactoring C# code for conversion to a WPF application (Visual Studio)重构 C# 代码以转换为 WPF 应用程序 (Visual Studio)
【发布时间】:2023-04-04 23:25:01
【问题描述】:

对于这个猜谜游戏,我想重构这个玻璃大炮的代码以转换为 WPF 应用程序。一般来说,我可以使用任何方法来缩短此/成功转换和提示 VS,将不胜感激。

我正在使用 WPF 应用程序(核心)作为该程序的模板。以及使用 Microsoft 教程来构建它。这个项目的UI大部分已经完成了,只需要导入这段代码。

请注意我在读高中,所以我的知识范围没有那么大。

编辑:好的。

  1. 首先,我想要对这段代码执行的操作是切断不必要的“IF”和“console.write”语句,以获得干净的解决方案。
  2. 其次,我将解决方案分成两个文件,一个 App.xaml.cs 文件和一个 MainWindow.Xaml.cs 文件。在 App.xaml 文件中,我放置了所有公共类(例如:guess、rnd 等)。虽然 MainWindow.Xaml 文件是我放置“游戏逻辑”的位置。

到目前为止,我所做的是;关于 MainWindow.xaml 有两种方法。初始化 rnd ccalculations 的公共方法。还有一个“Button_Click”私有方法,一旦用户提交他们的猜测,“游戏”就会查看它是否匹配并显示它们是对还是错,包括他们需要多长时间才能正确猜测。

    class MainClass 
    {
     public static void Main (string[] args) 
    {
    Random rnd = new Random();
    int ans = rnd.Next(1,10);
    Console.WriteLine("Pick an integer between 1 and 10");
    
    var num1 = Console.ReadLine();
    int v1 = Convert.ToInt32(num1);
     
    if(v1 == ans)
    {
      int count = 1;
      Console.WriteLine($"{v1} is correct. You Win!");
      Console.WriteLine($"It took you to {count} gues find the number {ans}." );
    }
    else
    {
      if(v1<ans){
        Console.WriteLine("To high");
      }
      else
      Console.WriteLine("To low");
      
      Console.WriteLine("Pick an interger between 1 and 10");
      Console.WriteLine($"{v1} isn't correct. Try again!");
      var num2 = Console.ReadLine();
      int v2 = Convert.ToInt32(num2);
        if(v2 == ans)
        {
          int count = 2;
          Console.WriteLine($"{v2} is correct. You Win!");
          Console.WriteLine($"It took you {count} gueses to find the number {ans}" );
        }
        else
        {
          if(v1<ans){
          Console.WriteLine("To high");
          }
          else
          Console.WriteLine("To low");
          
          Console.WriteLine("Pick an interger between 1 and 10");
          Console.WriteLine($"{v2} isn't correct. Try again!");
          var num3 = Console.ReadLine();
          int v3 = Convert.ToInt32(num3);
            if(v3 == ans)
            {
              int count = 3;
              Console.WriteLine($"{v3} is correct. You Win!");
              Console.WriteLine($"It took you {count} gueses to find the number {ans}" );
            }
            else
            {
              if(v1<ans){
              Console.WriteLine("To high");
              }
              else
              Console.WriteLine("To low");
              
              Console.WriteLine("Pick an interger between 1 and 10");
              Console.WriteLine($"{v3} isn't correct");
            }
            Console.WriteLine($"You Lose! The correct number is {ans}. ");
        }
      }  
    }
  }

【问题讨论】:

  • 嗨 TheNijuu,欢迎来到 SO!寻求开放式建议和重构通常不会得到太多回应。我建议做一些事情:1)指出您认为特别脆弱或丑陋的代码部分,2)描述您认为转换所需的内容和 3)最重要的是:向我们展示您已经完成的内容试过了。祝你好运!

标签: c# visual-studio refactoring


【解决方案1】:

这里发生了很多事情,我们需要很多时间来解释。

让我尝试了解基础知识。 C# 中的类是状态和行为的蓝图。

在这个术语中,您可以将代码建模为 GameRound

public class GameRound {
    private int noOfTries;
    private int maxNoOfTries;
    private int correctNumber;
    private bool success;

    public bool HasRoundEnded { get {
        return maxNoOfTries == noOfTries;
      }
    }

    public bool Success { get {
        return success;
      }
    }

    public GameRound() {
        Random rnd = new Random();
        int ans = rnd.Next(1,10);
        correctNumber = ans;
    }

    public bool GuessSolution(int guess) {
        if (guess == correctNumber) {
            this.success = true;
        } else {
            this.success = false;
            maxNoOfTries++;
        }
        return this.success;
    }

您可以看到您的大部分逻辑都包含在一个类中。我会留给你弄清楚如何使用它。

您会注意到没有对 Console.Write 或 read 的依赖。您可以在控制台应用程序或 UI 甚至网站中使用该代码。发生这种情况是因为我们将类的关注点分开,只为游戏回合建模。

另一条建议是,您可以在提供的类中使用while 循环来解决您在控制台应用程序中的问题。这样您将了解如何使用类的重复结构和对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多