3

我是 c# 和一般编码的新手。为了尝试提高我的技能,我正在尝试创建一个基本游戏,其中两名玩家掷骰子并记录他们的得分。玩家在达到 20 点时获胜。每个玩家轮流掷骰子,将他们的第一个骰子加到第二个,依此类推,直到其中一个达到 20 点。如果玩家掷出 6,则可以再次掷骰子。

我目前的代码是:


        do
        {

            Console.Write("Enter the name of Player 1: ");
            Player[0] = Console.ReadLine();
            Console.Write("Enter the name of Player 2: ");
            Player[1] = Console.ReadLine();

            Random DiceRandom = new Random();
            DiceThrow[0] = DiceRandom.Next(1, 7);

            int i = 0;
            while (i <= 1)
            {
                DiceThrow[0 + i] = DiceRandom.Next(1, 7);
                Console.ReadLine();
                Console.Write(Player[0 + i] + " rolled a " + DiceThrow[0 + i]);
                if (DiceThrow[0 + i] != 6) i++;
            }

            Console.ReadLine();
            PlayerTotal[0] = DiceThrow[0];
            PlayerTotal[1] = DiceThrow[1];


            Console.ReadLine();
            Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
            Console.ReadLine();
            Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
            Console.ReadLine();

        }
        while (PlayerTotal[0] == 20);
        while (PlayerTotal[1] == 20);

我特别苦苦挣扎的是将玩家的第一卷添加到第二卷中。如果玩家掷出一个 6,它会将 6 添加到他们重新掷出的结果中。

任何帮助都将不胜感激。

4

3 回答 3

4

您的代码存在许多问题:

  1. 您在循环中询问玩家姓名
  2. 您正在循环内初始化随机生成器(这可能会在不同的循环上导致相同的结果,因为随机生成器可以使用相同的种子)
  3. 您正在使用 = 而不是 += 重置总计
  4. 停止条件是 totals == 20 而不是 total < 20
  5. 您正在使用两个 while 语句而不是 AND (&&) 条件

这只是一个简短的概述..

这可以是一个解决方案:

  Console.Write("Enter the name of Player 1: ");
  Player[0] = Console.ReadLine();
  Console.Write("Enter the name of Player 2: ");
  Player[1] = Console.ReadLine();

  Random DiceRandom = new Random();

  do
    {

        int i = 0;
        while (i <= 1)
        {
            int thisThrow = DiceRandom.Next(1, 6);
            DiceThrow[0 + i] += thisThrow;
            Console.ReadLine();
            Console.Write(Player[0 + i] + " rolled a " + thisThrow);
            if (thisThrow != 6) i++;
        }

        Console.ReadLine();
        PlayerTotal[0] += DiceThrow[0];
        PlayerTotal[1] += DiceThrow[1];


        Console.ReadLine();
        Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
        Console.ReadLine();
        Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
        Console.ReadLine();

    }
    while (PlayerTotal[0] < 20 && PlayerTotal[1] < 20);
于 2012-12-10T09:59:52.990 回答
1

您的问题是您正在使用这些行重置以前的分数:

PlayerTotal[0] = DiceThrow[0];
PlayerTotal[1] = DiceThrow[1];

您应该将其更改为+=像这样使用:

PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];

这就像说:PlayerTotal[0] = PlayerTotal[0] + DiceThrow[0];

除此之外,您的代码中还有一些问题。

例如,您Do在代码的开头有一个但 2while秒...您可能希望While使用AND语句创建一个。此外,该Do语句需要您获得用户名之后......

例如: // 获取用户名

 do
 {
    // All your Dice throwing logic
 }
 while (PlayerTotal[0] != 20 && PlayerTotal[1] != 20);
于 2012-12-10T09:58:39.290 回答
0
    int i = 0;
    while (i <= 1)
    {
        int thisThrow = DiceRandom.Next(1, 6);
        DiceThrow[0 + i] += thisThrow;
        Console.ReadLine();
        Console.Write(Player[0 + i] + " rolled a " + thisThrow);
        if (thisThrow != 6) i++;
    }

    Console.ReadLine();
    PlayerTotal[0] += DiceThrow[0];
    PlayerTotal[1] += DiceThrow[1];


    Console.ReadLine();
    Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
    Console.ReadLine();
    Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
    Console.ReadLine();
于 2013-05-09T13:26:26.837 回答