随机数骰子辊(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/random-number-die-roller-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1194 个词 阅读量 0随机数骰子辊(译文)
原文地址:https://www.codeproject.com/Articles/4224/Random-Number-Die-Roller
原文作者:Zachery
译文由本站 robot-v1.0 翻译
前言
Generates random numbers using user constraints.
使用用户约束条件生成随机数.
介绍(Introduction)
该程序是为那些喜欢RPG和其他需要滚动不同类型骰子的游戏而编写的.(This program was written for those of you who are into RPG’s and other games that require rolling of different types of dice a set number of times.)
设置(Setting It Up)
编写此文件的第一步是为交互创建GUI.该GUI将允许用户指定管芯具有多少侧以及将其滚动多少次.为了指定模具的边数,我使用了Windows窗体控件,(The first step to writing this was to create a GUI for the interaction. This GUI would allow the user to specify how many sides the die has and how many times to roll it. For specifying how many sides the die has, I used the Windows Forms Control,) ComboBox
.然后,我添加了模具可能具有的不同侧面(2-21 ).对于滚动多少次,我使用了一个简单的Windows Forms Control,(. I then added the different sides that the die could have (2 - 21*). For how many times to roll, I used a simple Windows Forms Control,*) TextBox
.(*.*)
滚死(5月30日更新)(Rolling The Die (Updated May 30))
要设置压铸代码,您只需要一个简单的Random.首先,您必须确保如果用户指定了要滚动多少次,以及他在(For setting up the code for the die rolling, all you need is a simple Random. First off, you must make sure that if the user specified how many times to roll, and that what he typed in the) TextBox
是一个数字.默认情况下,您可能需要将卷号设置为" 1".在测试中查看转数是否为数字时,我添加了一个非常简单的(is a number. As a default, you may want to make the roll number “1”. In testing to see if the roll number is a number, I added a very simple) if
声明(statement to the) txtFrom_KeyPress
方法(感谢Richard Day的建议):(method (thanks to Richard Day for his suggestion):)
if (char.IsNumber(e.KeyChar) == false)
{
e.Handled = true;
txtFrom.Text = string.Empty;
}
这样可以确保不能在文本框中输入非数字.(This ensures that no non-number can be entered into the text box.)
接下来,我通过使用时钟的毫秒刻度来初始化随机数,以确保尽可能多的随机性.然后,根据用户指定要掷骰子的次数,我生成一个介于1和一个任意数量(规定了多个)之间的随机数.然后将每个数字放入另一个(Next, I initialize the Random by using the clock’s millisecond ticks to ensure as much randomness as possible. Then, depending upon how many times the user has specified that the die be rolled, I generate a random number between 1 and however many sides are specified, a set number of times. Each number is then put into another) TextBox
并显示给用户.(and displayed to the user.)
额外(Additional)
我添加了一个按钮来添加所有汇总在一起的数字.我要做的就是建立一个类型数组(I added a button to add the total of all the numbers rolled together. All I do to accomplish this is set up an array of type) int
并在生成数字时填写它.然后,我简单地遍历数组,将每个数字加在一起,然后在另一个文本框中显示它.(and fill it as I generate the numbers. Then, I simply iterate through the array, adding each of the numbers together and then displaying that in another text box.)
for (int i = 0; i < numbers.Length; i++)
total += numbers[i];
注意:虽然可能没有21面模具,但我还是决定添加它.(**Note: While there may be no 21 sided die, I decided to add it anyways.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.0 C++ C# VC6 WinXP Windows Win2K MFC Visual-Studio Dev 新闻 翻译