在C#中创建骰子事件的频率表

时间:2015-10-22 03:10:55

标签: c# console-application

我在编程方面相当新,目前正在学习C#。

我所拥有的是两个骰子随机滚动50卷 两个骰子总数加在一起,得出每个卷的总和。

我的问题是,如何创建一个频率表来计算在50个骰子结束时滚动某个数字的次数。

以下是我想要实现的目标。每个数字的计数图表(来自两个骰子)

1: |||
2: ||||
3: ||||||
4: |||||
5: ||||||||
6: ||

到目前为止,这是我的代码,

class Program
{
    const int DICE_ROLLS = 51;
    static void Main(string[] args)
    {
        Random random = new Random();
        int[] firstDice = new int[DICE_ROLLS];
        int[] secondDice = new int[DICE_ROLLS];
        int diceSum = 0;

            for (int roll = 1; roll <= 50; roll++)
            {
                firstDice[roll] = GenerateNumber(random);
                secondDice[roll] = GenerateNumber(random);
                diceSum = firstDice[roll] + secondDice[roll];
                Console.WriteLine("ROLL {0}:  {1} + {2} = {3}", roll, firstDice[roll], secondDice[roll], diceSum);
            }
            Console.WriteLine();
            Console.WriteLine("~~~~~~~~~~~~~~~~");
            Console.WriteLine("Number Frequency");
    }

    static int GenerateNumber (Random random)
    {
        return random.Next(1, 7);
    }
}

3 个答案:

答案 0 :(得分:0)

试试这个:

const int DICE_ROLLS = 51;
static void Main(string[] args)
{
    Random random = new Random();
    int[] firstDice = new int[DICE_ROLLS];
    int[] secondDice = new int[DICE_ROLLS];
    int[] count = new int[6];
    int diceSum = 0;

    for (int roll = 0; roll <= DICE_ROLLS - 1; roll++)
    {
        firstDice[roll] = GenerateNumber(random);
        secondDice[roll] = GenerateNumber(random);
        diceSum = firstDice[roll] + secondDice[roll];
        Console.WriteLine("ROLL {0}:  {1} + {2} = {3}", roll + 1, firstDice[roll], secondDice[roll], diceSum);
    }
    Console.WriteLine();
    Console.WriteLine("~~~~~~~~~~~~~~~~");
    Console.WriteLine("Number Frequency");

    var allRolls = firstDice.Concat(secondDice).ToList();   
    for(var i = 1; i <= 6; i++)
    {
        Console.Write(i + ": ");
        for(var j = 0; j < allRolls.Count(c => c == i); j++)
            Console.Write("|");
        Console.WriteLine();
    }
}


static int GenerateNumber (Random random)
{
   return random.Next(1, 7);
}

给出:

~~~~~~~~~~~~~~~~
Number Frequency
1: ||||||||||||||||||||
2: |||||||||||
3: ||||||||||||||||||
4: ||||||||||||||||
5: |||||||||||||||||||||
6: ||||||||||||||||

答案 1 :(得分:0)

假设有6个骰子,<script> //constants var Col = 20, Rows = 20; var cellHeight = 25; var cellWidth = 25; var foodX; var score; var foodY; var Nothing = 0, Snake = 1, Food = 2; var Left = 37, Up = 38, Right = 39, Down = 40; var canvas = document.getElementById('snakeCanvas'); var context = canvas.getContext('2d'); var dead = "false"; var snakeDirection = null; var keystate; var snake = []; function start() //this is where we begin the long journey { init(); Tick(); } function init() { snake = [{ x: 5, y: 5 }]; snakeDirection = null; score = 0; document.getElementById("score").innerHTML = "Score: " + score; setFood(); keystate = null; } function Tick() // just liker a timer tick { document.addEventListener("keydown", function (evt) { keystate = evt.keyCode; // checks key presses }); //document.addEventListener("keyup", function (evt) { //delete keystate[evt.keyCode]; //}); update(); //after we check for a key press we update alllll the stuff setTimeout(Tick, 300); //} } function update() { checkKey(); // checks what key has been pressed for (var i = snake.length-1; i > 0; i--) { snake[i].y = snake[i-1].y; snake[i].x = snake[i-1].x } switch (snakeDirection) { // keys case "DOWN": snake[0].y++; break; case "UP": snake[0].y--; break; case "RIGHT": snake[0].x++; break; case "LEFT": snake[0].x--; break; } draw(); //draws all the stuff like food and snake checkCollisions(); // self explaintory name } function checkKey() //Change the direction of the snake cant go backwards too { if (keystate == Left && snakeDirection != "RIGHT" ) { snakeDirection = "LEFT"; } if (keystate == Up && snakeDirection != "DOWN") { snakeDirection = "UP"; } if (keystate == Right && snakeDirection != "LEFT") { snakeDirection = "RIGHT"; } if (keystate == Down && snakeDirection != "UP") { snakeDirection = "DOWN"; } } function setFood() { //WE ARE RUNNING OUT OF FOOD WE NEED NEW PROVISIONS var next = "true" do { foodX = Math.floor((Math.random() * Rows)); foodY = Math.floor((Math.random() * Col)); for (var i = 0; i < snake.length; i++) { // IT SUCKS WHEN I CANT EAT FOOD BECAUSE ITS ALREADY INSIDE OF ME if (snake[i].x == foodX && snake[i].y == foodY) { next = "false" } } } while (next == "false") draw(); // Pretty pictures } function checkCollisions() { for (var i = 1; i < snake.length; i++) { // STOP hitting yourself if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) { init(); } } if (snake[0].y < 0 || snake[0].y > Rows || snake[0].x < 0 || snake[0].x > Col) // you are forbidon to veture from the canvas { init(); } if (snake[0].x == foodX && snake[0].y == foodY) { //Yummy FOOD EAT EAT EAT score++; document.getElementById("score").innerHTML = "Score: " + score; setFood(); snake.push({ x: null, y: null }); // I got fatter } } function draw() { context.clearRect(0, 0, canvas.width, canvas.height); // clears canvas context.fillStyle = "#FF0000"; // pretty colour for the head of the snake context.fillRect(snake[0].x * cellWidth, snake[0].y * cellWidth, cellWidth, cellHeight); context.fillStyle = "#09F"; for (var i = 1; i < snake.length; i++) { context.fillRect(snake[i].x * cellWidth, snake[i].y * cellWidth, cellWidth, cellHeight); } context.fillStyle = "#F90"; // FOOD FOOD FOOD FOOD context.fillRect(foodX * cellWidth, foodY * cellWidth, cellWidth, cellHeight); } start(); // starts hence the name start </script> 可能是一个不错的选择。

Dictionary

您甚至可以通过基于变量(例如

)生成初始字典值,根据不同的骰子轻松自定义它
var rolls = new SortedDictionary<int, int> { {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0} };

for(int i = 0; i < 50; i++)
{
   var first = Random.Next(1, 7);
   var second = Random.Next(1, 7);
   rolls[first]++;
   rolls[second]++;

   // Rest of rolling display code
}

// Header display code
foreach(var roll in rolls)
{
     Console.Write("{0}: ", roll.Key);
     for(int i = 0; i < rolls.Value; i++)
     {
         Console.Write("|");
     }
     Console.WriteLine();
}

您可以将int sides = 12; var rolls = new SortedDictionary<int, int>(); foreach(var value in Enumerable.Range(1, sides)) { rolls.Add(value, 0); } 替换为Random.Next(1, 7)并使用一个非常通用的程序来掷骰子并计算其频率。

答案 2 :(得分:0)

这有效:

var rnd = new Random();
Console.WriteLine(
    String.Join(
        Environment.NewLine,
            Enumerable
                .Range(0, 50)
                .Select(n => rnd.Next(1, 7))
                .ToLookup(x => x)
                .Select((xs, n) =>
                    String.Format("{0}: {1}", n + 1, new String('|', xs.Count())))));

我得到了这个(例如):

1: |||||||
2: |||||||
3: |||||||||||
4: |||||||||
5: ||||||||
6: ||||||||