【问题标题】:C# Dice Roll Program With Images带有图像的 C# 掷骰子程序
【发布时间】:2012-03-18 11:59:41
【问题描述】:

我正在尽我最大的努力做这个程序,即制作一个程序,让用户可以随意掷两个骰子,但掷出的骰子不能显示为数字,而是显示为图像。

比如

[o]

掷骰为 1。

我还没有为程序制作循环代码,我只知道如何为滚动制作随机数,我只是不知道如何为图像制作数组列表并使代码实际使用图片而不是数字...如果您知道我的意思。

这是我目前的代码,感谢您的帮助!

        int[] DiceUno = new int[6];
        int[] DiceDos = new int[6];
        Random rnd = new Random();

        Console.WriteLine("This program will allow you to roll two dice");
        Console.WriteLine("\nAs many times as you want");
        Console.WriteLine("\n\nWhen you want to exit the program, please type (exit)");
        Console.WriteLine("\nPress any key to begin rolling");
        Console.Read();


        for (int i = 0; i < 1; i++)
        {
            int diceRoll = 0;
            diceRoll = rnd.Next(6);
            DiceUno[diceRoll]++;
            Console.WriteLine("Dice 1 is rolled a: {0}", diceRoll + 1);
            diceRoll = rnd.Next(6);
            DiceDos[diceRoll]++;
            Console.WriteLine("Dice 2 is rolled a: {0}", diceRoll + 1);

        }





    }
}

}

【问题讨论】:

  • 我忍不住要编码,但是 die 的复数形式是 dice。骰子没有意义。只是把它扔在那里。
  • 这些是真实的图像还是您只是在数字周围画括号?
  • 我也刚刚意识到这一点。如果您指的是 jpeg 等实际图像而不仅仅是 ascii 图像,请忽略我的回答。
  • 如果这是家庭作业,则添加 homework 标签。顺便说一句,我也会说西班牙语。

标签: c# dice


【解决方案1】:

为什么不像

这么简单
Dictionary<int, string> valueToDiceImage = new Dictionary<int, string>() 

{

 {0, "[0]"},

 {1, "[1]"},

 {2, "[2]"},

 {3, "[3]"},

 {4, "[4]"},

 {5, "[5]"},

 {6, "[6]"},

};

然后像这样使用它:

int diceRoll = rnd.next(6); 
System.Console.Write("User Rolled a " + valueToDiceImage[diceRoll] + "\n");

【讨论】:

  • 对不起,我打算使用 ASCII 艺术来做骰子。所以我的意思是我如何创建一个数组列表来让它调出 dice1、dice2、dice3 等的随机 ASCII 艺术而不是数字
【解决方案2】:

如果要输出文本而不是数字,请创建一个字符串数组:

string[] images = new string[]
    { "o", "oo", "ooo", "oooo", "ooooo", "oooooo" };

而不是 diceRoll + 1 在 Console.WriteLine 中放 images[diceRoll]:

Console.WriteLine("Dice 1 is rolled a: {0}", images[diceRoll]);

现在您可以玩图像了,也许可以创建一个三行图像来显示出现在骰子上的数字(点空白)。

【讨论】:

    【解决方案3】:

    这应该使用一些快速而肮脏的 LINQ。

    var die = new Dictionary<int, string>
    {
        { 1, "[     ]\n[  o  ]\n[     ]" }, //or a path to an image somewhere or anything you want
        { 2, "[     ]\n[ o o ]\n[     ]" },
        { 3, "[  o  ]\n[ o o ]\n[     ]" },
        { 4, "[ o o ]\n[     ]\n[ o o ]" },
        { 5, "[ o o ]\n[  o  ]\n[ o o ]" },
        { 6, "[ o o ]\n[ o o ]\n[ o o ]" },
    };
    
    do
    {
        var shuffled = die.OrderBy(x => Guid.NewGuid()).Take(2);
    
        foreach (KeyValuePair<int, string> i in shuffled)
        {
            Console.WriteLine(i.Value);
            Console.WriteLine();
        }
    } while (Console.ReadLine() != "(exit)");
    

    【讨论】:

      猜你喜欢
      • 2014-04-21
      • 2020-09-18
      • 2012-09-16
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      相关资源
      最近更新 更多