首先,永远不要将obj目录上传到github。与从未上传最终构建输出bin目录的方式相同,您不应上传包含编译/链接/构建中间结果的obj目录。
如果您只想表明自己不关心命令行参数,那么这是没有意义的:
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()
:
static void Main()
{
: // main loop here
}
在现代编码风格中,首选项是自记录代码。注释越少越好,因为代码应该说明一切。所以像这样:
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
}
}
应该看起来像这样:
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缩进样式。如果您有自己的缩进样式,请一致地应用它。所以这:
if (Hero.Alive == false) { gameOver = true; }
应写为:
if (Hero.Alive == false)
{
gameOver = true;
}
不要使用幻数。您必须解释魔术数字的含义是您必须编写更多注释:
// 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; }
使用常量和/或枚举:
static class Puzzle
{
public const int LastPuzzle = 4;
:
}
:
gameOver |= Puzzle.WhichPuzzle == Puzzle.LastPuzzle;
为什么在Puzzle类中使用二维提示数组?
不要将异常用于流控制:
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;
做正确的事,并找出如何避免异常:
for (int i = 0; i < Math.Min(playerGuess.Length, currentAnswer.Length); i++)
{
if (playerGuess[i] == currentAnswer[i])
{
validCharsCounter++;
}
}
这可以简化:
// if validCharsCounter was equal to 0 before being modified, after modifying it may be equal to -1
if (validCharsCounter == -1) { validCharsCounter = 0; }
与
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
public static int WhichPuzzle { get; private set; }
您必须注释哪个状态索引这一事实意味着:
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
};
并使用各种魔术数字来记住哪个状态表示您确实应该拥有一个类似于以下内容的类(或结构):
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; }
};
就这样,我一直在等待的事情已经发生了。回去为我工作。