【发布时间】:2023-04-04 23:25:01
【问题描述】:
对于这个猜谜游戏,我想重构这个玻璃大炮的代码以转换为 WPF 应用程序。一般来说,我可以使用任何方法来缩短此/成功转换和提示 VS,将不胜感激。
我正在使用 WPF 应用程序(核心)作为该程序的模板。以及使用 Microsoft 教程来构建它。这个项目的UI大部分已经完成了,只需要导入这段代码。
请注意我在读高中,所以我的知识范围没有那么大。
编辑:好的。
- 首先,我想要对这段代码执行的操作是切断不必要的“IF”和“console.write”语句,以获得干净的解决方案。
- 其次,我将解决方案分成两个文件,一个 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