高级猜谜游戏-代码审查

重力猫190

新成员
已加入
2020年11月23日
留言内容
1
编程经验
Beginner
你好:3
我正在学习C#,看完课程并掌握了基础知识之后,我决定编写一个小项目。这是一款猜谜游戏,玩家可以:
  1. 猜测(当然):这将减去他/她的精力
  2. 休息:这将恢复他/她的精力,但会花费一些时间
  3. 提示:获得有关当前难题的提示,但需要一些时间
  4. 掷骰子:随机获得正面或负面影响
它完成了所有要做的任务,全部与代码审查有关。对于解决方案/其他解决方案的优化建议,良好实践(编写,注释代码)以及您认为将来可能对我有帮助的所有其他事项,我将不胜感激。

PS:正如我上面所写,这是我的第一个C#项目。以前,我用其他技术/语言编写了一些简单的小型项目。

它包含:主程序,英雄类,拼图类
代码- > 重力猫190 /高级猜谜游戏
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,497
地点
弗吉尼亚州切萨皮克
编程经验
10+
首先,永远不要将obj目录上传到github。与从未上传最终构建输出bin目录的方式相同,您不应上传包含编译/链接/构建中间结果的obj目录。

如果您只想表明自己不关心命令行参数,那么这是没有意义的:
C#:
static void Main(string[] args)
{
    Start();
}

static void Start()
{
    : // main loop here
}

Just do remove the parameters and put the code from Start() inside Main():

C#:
static void Main()
{
    : // main loop here
}

在现代编码风格中,首选项是自记录代码。注释越少越好,因为代码应该说明一切。所以像这样:
C#:
static void StartNewGame()
{
    // Introduction
    Console.WriteLine("You started a new game");
    Console.WriteLine("...");
    Console.WriteLine("...");
    Console.WriteLine("...");
    Console.WriteLine("Hello Hero! You forgot to wear your plot armor and some random snake bites you. You will die in 24 hours.");
    Console.WriteLine("Yea well that's all. Good luck!");

    bool gameOver = false;
    while (gameOver == false)
    {
        : // loop body
    }
}

应该看起来像这样:
C#:
static void Introduction()
{
    Console.WriteLine("You started a new game");
    Console.WriteLine("...");
    Console.WriteLine("...");
    Console.WriteLine("...");
    Console.WriteLine("Hello Hero! You forgot to wear your plot armor and some random snake bites you. You will die in 24 hours.");
    Console.WriteLine("Yea well that's all. Good luck!");   
}

static void StartNewGame()
{
    Introduction();

    bool gameOver = false;
    while (gameOver == false)
    {
        : // loop body
    }
}

C#更喜欢Allman缩进样式。如果您有自己的缩进样式,请一致地应用它。所以这:
C#:
if (Hero.Alive == false) { gameOver = true; }
应写为:
C#:
if (Hero.Alive == false)
{
    gameOver = true;
}

不要使用幻数。您必须解释魔术数字的含义是您必须编写更多注释:
C#:
// If player solves a puzzle, whichPuzzle is incremented. If WhichPuzzle returns 4 it means that the player solved all of the puzzles
if (Puzzle.WhichPuzzle == 4) { gameOver = true; }
使用常量和/或枚举:
C#:
static class Puzzle
{
    public const int LastPuzzle = 4;
    :
}

:

gameOver |= Puzzle.WhichPuzzle == Puzzle.LastPuzzle;

为什么在Puzzle类中使用二维提示数组?

不要将异常用于流控制:
C#:
int validCharsCounter = 0;
// playerGuess.Length-1 -> I'll use i to go through the entire array. The returned length will be greater than the last index
for (int i = 0; i <= playerGuess.Length-1; i++)
{
    try
    {
        if (currentAnswer[i] == playerGuess[i]) { validCharsCounter++; }
    }
    catch (IndexOutOfRangeException)
    {
        break;
    }
}
return validCharsCounter;
做正确的事,并找出如何避免异常:
C#:
for (int i = 0; i < Math.Min(playerGuess.Length, currentAnswer.Length); i++)
{
    if (playerGuess[i] == currentAnswer[i])
    {
        validCharsCounter++;
    }
}

这可以简化:
C#:
// if validCharsCounter was equal to 0 before being modified, after modifying it may be equal to -1
if (validCharsCounter == -1) { validCharsCounter = 0; }

C#:
validCharsCounter = Math.Max(validCharsCounter, 0);

Instead of having a public WhichPuzzle and a backing whichPuzzle where your intent was for whichPuzzle to only be updatable 与 in the class, you could have just done this instead
C#:
public static int WhichPuzzle { get; private set; }

您必须注释哪个状态索引这一事实意味着:
C#:
private static int[] statusesCounters = // hero doesn't have any active status at the beginning
{
    0, // AHA Moment - Player will get hint about answer's length
    0, // Motivation - extra energy after guessing the puzzle
    0, // Narcolepsy - chance that rest will not give you energy
    0, // Effective antidote - more time after guessing the puzzle
    0, // Slow moves - resting and taking hint will take more time
    0, // Dizziness - failed guessing may return a modified result of valid chars
    0, // Exhaustion - resting will give only half energy
    0  // Student syndrome - resting and taking hint will take the maximum possible amount of time
};

并使用各种魔术数字来记住哪个状态表示您确实应该拥有一个类似于以下内容的类(或结构):
C#:
class Status
{
    public int AhaMoment { get; set; }
    public int Motivation { get; set; }
    public int Narcolepsy { get; set; }
    public int EffectiveAntidote { get; set; }
    public int SlowMoves { get; set; }
    public int Dizziness { get; set; }
    public int Exhaustion { get; set; }
    public int StudentSyndrome { get; set; }
};

就这样,我一直在等待的事情已经发生了。回去为我工作。
 
最佳 底部