Random rd = new Random();
int bombPassword = rd.Next(1, 101);
int chances = 5;
bool isDefused = false;
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
Console.WriteLine("========= 紧急任务:炸弹拆解 =========");
Console.WriteLine($"警告:密码范围 1-100,剩余机会:{chances} 次");
Console.WriteLine("======================================");
for (int i = 1; i <= chances; i++)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"\n第 {i} 次尝试,输入密码:");
if (int.TryParse(Console.ReadLine(), out int guess))
{
if (guess == bombPassword)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("\n\n\n 【 恭喜 】密码正确!炸弹已拆除,你是英雄!");
isDefused = true;
break;
}
else
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
string hint = guess > bombPassword ? " 提示:偏高了 ↑ " : " 提示:偏低了 ↓ ";
Console.WriteLine(hint);
}
}
}
if (!isDefused)
{
for (int j = 0; j < 3; j++)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
System.Threading.Thread.Sleep(200);
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
System.Threading.Thread.Sleep(200);
}
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("\n\n\n 【 轰!!! 】炸弹爆炸,任务失败!");
Console.WriteLine($" 正确密码是:{bombPassword}");
}
Console.ResetColor();
Console.WriteLine("\n\n按任意键退出程序...");
Console.ReadKey();