如何在c#中循环一个骰子游戏
本文关键字:一个 游戏 循环 | 更新日期: 2023-09-27 18:14:28
我必须制作一款玩家与店主一起玩的骰子游戏,这只是玩家与店主互动的更大程序的一部分。玩家拥有一定数量的金币,在每个回合中玩家可以赢得1.1个金币,输掉1个金币,或者平局。玩家可以选择玩还是不玩。当玩家想要再次玩游戏并且拥有至少1个金币时,我便希望循环多次,当他决定不再玩游戏时,循环便会停止。我试着搜索教程,但没有找到任何特别有用的。
Console.WriteLine("Do you want to play a game of dices with me to win some gold?");
Console.WriteLine("You can lose 1 gold or you can win 1.1 gold.");
Console.WriteLine("Wanna play? y or n");
var diceAnswer = Console.ReadLine();
switch (diceAnswer)
{
case "y":
Random rnd = new Random();
int playerRoll = rnd.Next(1, 7);
Console.WriteLine("You rolled {0}", playerRoll);
int shopRoll = rnd.Next(1, 7);
Console.WriteLine("I rolled {0}", shopRoll);
if (playerRoll > shopRoll)
{
goldLeft += 1.1;
Console.WriteLine("You won 1.1 gold, therefore you now have " + goldLeft + " gold");
}
else if (playerRoll < shopRoll)
{
goldLeft--;
Console.WriteLine("You lost 1 gold, therefore you now have " + goldLeft + " gold");
}
else
{
Console.WriteLine("That is a draw. Seems that we are both lucky");
}
break;
case "n":
Console.WriteLine("That is a safe choice.");
break;
default :
Console.WriteLine("Since that is not a good answer, I will not play with you");
break;
}
类似这样(while loop with break
s):
// do not re-create Random
private static Random rnd = new Random();
...
Console.WriteLine("Do you want to play a game of dices with me to win some gold?");
Console.WriteLine("You can lose 1 gold or you can win 1.1 gold.");
while (goldLeft >= 1.0) { // you must have enough gold if you want to play
Console.WriteLine("Wanna play? y or n");
string answer = Console.ReadLine();
if (answer.Equals("n")) {
Console.WriteLine("That is a safe choice.");
break;
}
else if (!answer.Equals("y")) {
Console.WriteLine("Since that is not a good answer, I will not play with you");
break;
}
// Case "y" here
int playerRoll = rnd.Next(1, 7);
int shopRoll = rnd.Next(1, 7);
if (playerRoll > shopRoll) {
goldLeft += 1.1;
Console.WriteLine("You won 1.1 gold, therefore you now have " + goldLeft + " gold");
}
else if (playerRoll < shopRoll) {
goldLeft -= 1.0;
Console.WriteLine("You lost 1 gold, therefore you now have " + goldLeft + " gold");
}
else {
Console.WriteLine("That is a draw. Seems that we are both lucky");
}
}
你可以使用do-while循环总是在第一次运行,但如果玩家决定不继续这样做,则在结束时中断:
bool keepPlaying = true;
do {
Console.WriteLine("Do you want to play a game of dices with me to win some gold?");
Console.WriteLine("You can lose 1 gold or you can win 1.1 gold.");
Console.WriteLine("Wanna play? y or n");
var diceAnswer = Console.ReadLine();
switch (diceAnswer)
{
case "y":
keepPlaying = true;
Random rnd = new Random();
int playerRoll = rnd.Next(1, 7);
Console.WriteLine("You rolled {0}", playerRoll);
int shopRoll = rnd.Next(1, 7);
Console.WriteLine("I rolled {0}", shopRoll);
if (playerRoll > shopRoll)
{
goldLeft += 1.1;
Console.WriteLine("You won 1.1 gold, therefore you now have " + goldLeft + " gold");
}
else if (playerRoll < shopRoll)
{
goldLeft--;
Console.WriteLine("You lost 1 gold, therefore you now have " + goldLeft + " gold");
}
else
{
Console.WriteLine("That is a draw. Seems that we are both lucky");
}
break;
case "n":
keepPlaying = false;
Console.WriteLine("That is a safe choice.");
break;
default :
keepPlaying = false;
Console.WriteLine("Since that is not a good answer, I will not play with you");
break;
}
} while (keepPlaying);
您可以在这里了解更多关于do-while循环的信息,但原则上,do块的内容至少执行一次,并且在while语句末尾的条件语句求值为true时重新执行。