我创造了一个让玩家有5次机会玩的游戏,之后我想询问玩家是否想再玩或退出。我已经看到它在Python中完成,但我不知道python。我的代码工作得很好,但我想添加这两个附加功能 如何在C#中实现这些功能? 作为参考,这是我的代码主类代码的样子。
namespace NumBaseBall
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t*************************************");
Console.WriteLine("\t\t\t* Let's Have Some Fun *");
Console.WriteLine("\t\t\t* Welcome To The *");
Console.WriteLine("\t\t\t* Number Baseball Game *");
Console.WriteLine("\t\t\t*************************************\n");
GameResults gameresults = new GameResults();
for (int trysCounter = 1; trysCounter <= 5; trysCounter++)
{
gameresults.Strikes = 0;
Random r = new Random();
var myRange = Enumerable.Range(1, 9);
var computerNumbers = myRange.OrderBy(i => r.Next()).Take(3).ToList();
Console.WriteLine("The Game's Three Random Integers Are: (Hidden from user)");
foreach (int integer in computerNumbers)
{
Console.WriteLine("{0}", integer);
}
List<int> playerNumbers = new List<int>();
Console.WriteLine("Please Enter Three Unique Single Digit Integers and Press ENTER after each:");
for (int i = 0; i < 3; i++)
{
Console.Write("");
int number = Convert.ToInt32(Console.ReadLine());
playerNumbers.Add(number);
}
gameresults.StrikesOrBalls(computerNumbers,playerNumbers);
Console.WriteLine("---> Computer's Numbers = {0}{1}{2}", computerNumbers[0], computerNumbers[1], computerNumbers[2]);
Console.WriteLine("---> Player's Numbers = {0}{1}{2}", playerNumbers[0], playerNumbers[1], playerNumbers[2]);
Console.WriteLine("---> Game Results = {0} STRIKES & {1} BALLS\n", gameresults.Strikes, gameresults.Balls);
Console.WriteLine("You have played this games {0} times\n", trysCounter);
gameresults.TotalStrikes = gameresults.TotalStrikes + gameresults.Strikes;
Console.WriteLine("STRIKES = {0} ", gameresults.TotalStrikes);
if (gameresults.TotalStrikes >= 3)
{
gameresults.Wins++;
Console.WriteLine("YOU ARE A WINNER!!!");
break;
}
}
if (gameresults.TotalStrikes <3)
Console.WriteLine("YOU LOSE :( PLEASE TRY AGAIN!");
}
}
}
答案 0 :(得分:1)
将代码插入循环中,检查用户是否要继续:
while(true) // Continue the game untill the user does want to anymore...
{
// Your original code or routine.
while(true) // Continue asking until a correct answer is given.
{
Console.Write("Do you want to play again [Y/N]?");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
break; // Exit the inner while-loop and continue in the outer while loop.
if (answer == "N")
return; // Exit the Main-method.
}
}
但也许最好将一个大的例程分成单独的例程。
让我们将您的Main方法重命名为PlayTheGame
。
将我的惯例分成:
static public bool PlayAgain()
{
while(true) // Continue asking until a correct answer is given.
{
Console.Write("Do you want to play again [Y/N]?");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
return true;
if (answer == "N")
return false;
}
}
现在Main方法可以是:
static void Main(string[] args)
{
do
{
PlayTheGame();
}
while(PlayAgain());
}
您必须将一些局部变量作为静态字段移动到类中。或者您可以创建一个Game类的实例,但我认为这是目前为止的一步。
答案 1 :(得分:0)
有两种方法可以实现这一目标:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill%28v=vs.110%29.aspx
System.Diagnostics.Process.GetCurrentProcess().Kill();
或者
https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx
int exitCode =1;
System.Environment.Exit(exitCode);
Environment.Exit是退出程序的首选方法,因为Kill命令“导致异常进程终止,应该仅在必要时使用。” [msdn]
答案 2 :(得分:0)
首先,建议将实际游戏的代码移动到单独的功能中。它会清理很多东西。
像
这样的东西private static bool PlayGame()
{
// Win branch returns true.
// Loss branch returns false.
}
这使您可以大大简化Main
功能,使其只能处理菜单功能。
对于实际的菜单功能,我倾向于选择do/while
循环。你有一些额外的规定,你只能在5场比赛之后提出要求,但这很容易处理。
static void Main(string[] args)
{
int playCount = 0;
string answer = "Y";
bool winner;
do
{
if(playCount < 5)
{
playCount++;
}
else
{
do
{
Console.Write("Play again? (Y/N): ");
answer = Console.ReadLine().ToUpper();
} while(answer != "Y" && answer != "N");
}
winner = PlayGame();
} while(!winner && answer == "Y");
Console.WriteLine("Thanks for playing!");
}
通过使用增量运算符将5个游戏的测试移动到if
条件,可以稍微简化一下。唯一的问题是如果有人玩你的游戏大约十亿次,事情可能会变得奇怪。
static void Main(string[] args)
{
int playCount = 0;
string answer = "Y";
bool winner;
do
{
if(playCount++ > 3)
{
do
{
Console.Write("Play again? (Y/N): ");
answer = Console.ReadLine().ToUpper();
} while(answer != "Y" && answer != "N");
}
winner = PlayGame();
} while(!winner && answer == "Y");
Console.WriteLine("Thanks for playing!");
}
编辑:改变了原始代码中的内容,游戏在游戏结束后游戏结束。
根据您在下面的评论中的问题,您可以在GameResults
课程中制作Program
课程的静态实例。您的代码最终会看起来像以下
class Program
{
private static GameResults results = new GameResults();
public static void Main(string[] args)
{
// Code
}
private static bool PlayGame()
{
// Code
}
}
在PlayGame
中,您只需使用静态results
对象,而不是每次调用PlayGame
时都创建一个新对象。