如何在c#中循环骰子游戏?

时间:2015-09-24 14:37:00

标签: c# loops

我必须做一个骰子游戏,其中玩家与店主一起玩,这只是玩家必须与店主互动的更大程序的一部分。玩家拥有一定数量的黄金,并且在每一个回合中玩家都可以获得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;
}

2 个答案:

答案 0 :(得分:1)

类似的东西( 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");
  }  
}

答案 1 :(得分:0)

你可以使用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循环here的更多信息,但原则上,do块的内容至少执行一次,并且在while语句结尾处的条件语句求值时重新执行真。