内部具有随机数的循环来自单独的方法并且在int方法中

时间:2017-12-21 10:44:45

标签: c# loops

我正在制作C#版本的骰子游戏'去波士顿'有三个骰子,每个角色后一个骰子被删除。我希望这个系统拥有最佳的5循环,这样当其中一个玩家达到五轮时他们就会赢。

这是我正在使用的随机数生成器:

public static int diceroll0()
{
    Random DiceRoll0 = new Random();
    Thread.Sleep(200);
    int throw1 = DiceRoll0.Next(1, 18);
    int throw2 = DiceRoll0.Next(1, 12);
    int throw3 = DiceRoll0.Next(1, 6);

    int total0 = (throw1 + throw2 + throw3);
    return total0;
}

public static int diceroll1()
{
    Random DiceRoll1 = new Random();
    Thread.Sleep(200);
    int throw4 = DiceRoll1.Next(1, 18);
    int throw5 = DiceRoll1.Next(1, 12);
    int throw6 = DiceRoll1.Next(1, 6);

    int total1 = (throw4 + throw5 + throw6);
    return total1;
}

public static int diceroll2()
{   
    Random DiceRoll2 = new Random();
    Thread.Sleep(200);
    int throw7 = DiceRoll2.Next(1, 18);
    int throw8 = DiceRoll2.Next(1, 12);
    int throw9 = DiceRoll2.Next(1, 6);
    int total2 = (throw7 + throw8 + throw9);
    return total2;
}

public static int diceroll3()
{       
    Random DiceRoll3 = new Random();
    Thread.Sleep(200);
    int throw10 = DiceRoll3.Next(1, 18);
    int throw11 = DiceRoll3.Next(1, 12);
    int throw12 = DiceRoll3.Next(1, 6);
    int total3 = (throw10 + throw11 + throw12);
    return total3;
}


    int user1points = 0;
    int user2points = 0;

    die rolling = new die();
    int roll0Score = die.diceroll0();
    int roll1Score = die.diceroll1();
    int roll2Score = die.diceroll2();
    int roll3Score = die.diceroll3();       

我可以以任何方式循环下面的系统,以便它可以以5种格式使用吗?

    //Round 1
    Console.WriteLine("Round 1 rolls:");

    Console.WriteLine(roll0Score);
    Console.WriteLine(roll1Score);

    //Statement to figure which player wins the round
    if (roll0Score > roll1Score)
    {
        user1points++;
    }
    else
    {
        user2points++;
    }

    Console.WriteLine("Round 1 scores:");

    Console.WriteLine("Player 1 has scored " + user1points);
    Console.WriteLine("Player 2 has scored " + user2points);
    Console.ReadKey();

    //Round 2

    Console.WriteLine("Round 2 rolls:");

    Console.WriteLine(roll2Score);
    Console.WriteLine(roll3Score);

    if (roll2Score > roll3Score)
    {
        user1points++;
    }
    else
    {
        user2points++;
    }

    Console.WriteLine("Round 2 scores:");

    Console.WriteLine("Player 1 has scored " + user1points);
    Console.WriteLine("Player 2 has scored " + user2points);
    Console.ReadKey();

This is the full code

1 个答案:

答案 0 :(得分:1)

好。这段代码是非常错误的 - 首先,它不遵循游戏规则(它将所有骰子加起来,而不是从每卷中获得最高值),其次,没有必要以这种方式重复它。我已经用三个类创建了一个超级简单的解决方案:

  • Die - 代表单个模具。
  • DicePlayer - 代表玩家和当前得分
  • DiceGame - 代表整个游戏。

为简单起见,此解决方案将游戏打印到控制台。示例输出:

Player Bill
Turn 1: rolling 5 4 4 , keeping 5
Turn 2: rolling 1 6 , keeping 6
Turn 3: rolling 6 , keeping 6
Score for round 17

Player Sam
Turn 1: rolling 5 3 1 , keeping 5
Turn 2: rolling 3 5 , keeping 5
Turn 3: rolling 5 , keeping 5
Score for round 15

Player Bill
Turn 1: rolling 6 4 2 , keeping 6
Turn 2: rolling 6 5 , keeping 6
Turn 3: rolling 1 , keeping 1
Score for round 13

Player Sam
Turn 1: rolling 6 1 4 , keeping 6
Turn 2: rolling 2 5 , keeping 5
Turn 3: rolling 5 , keeping 5
Score for round 16

Sam wins - Bill has 30 and Sam has 31

此处的完整代码:

public static void Main(string[] args)
{
    var diceGame = new DiceGame(2, new DicePlayer("Bill"), new DicePlayer("Sam"));
    diceGame.PlayGame();
    Console.ReadKey();
}

internal class DiceGame
{
    private readonly int rounds;
    private readonly DicePlayer player1;
    private readonly DicePlayer player2;

    public DiceGame(int rounds, DicePlayer player1, DicePlayer player2)
    {
        this.rounds = rounds;
        this.player1 = player1;
        this.player2 = player2;
    }

    public void PlayGame()
    {
        var die = new Die();
        for (var i = 0; i < rounds; i++)
        {
            player1.TakeTurn(die);
            player2.TakeTurn(die);
        }

        if (player1.CurrentScore == player2.CurrentScore)
        {
            Console.WriteLine($"Game is drawn - {player1.Name} has {player1.CurrentScore} and {player2.Name} has {player2.CurrentScore}");
        }
        else if (player1.CurrentScore > player2.CurrentScore)
        {
            Console.WriteLine($"{player1.Name} wins - {player1.Name} has {player1.CurrentScore} and {player2.Name} has {player2.CurrentScore}");
        }
        else
        {
            Console.WriteLine($"{player2.Name} wins - {player1.Name} has {player1.CurrentScore} and {player2.Name} has {player2.CurrentScore}");
        }
    }
}

internal class DicePlayer
{
    public DicePlayer(string name)
    {
        Name = name;
    }

    public string Name { get; }

    public int CurrentScore { get; private set; }

    public void TakeTurn(Die die)
    {
        Console.WriteLine($"Player {Name}");
        //In this game, only the highest dice is put aside each time.
        var roundScore = 0;
        for (int i = 0; i < 3; i++)
        {
            Console.Write($"Turn {i + 1}: rolling ");
            var max = 0;
            for (int j = 0; j < 3 - i; j++)
            {
                var roll = die.NextRoll();
                Console.Write($"{roll} ");
                if (roll > max)
                {
                    max = roll;
                }
            }
            Console.WriteLine($", keeping {max}");
            roundScore += max;
        }

        Console.WriteLine($"Score for round {roundScore}");
        Console.WriteLine();
        CurrentScore += roundScore;
    }
}

internal class Die
{
    private readonly Random rng;

    public Die()
    {
        rng = new Random();
    }

    public int NextRoll()
    {
        return rng.Next(1, 7);
    }
}