为每个颜色和数字分配一堆按钮。 WPF [英] Assign a bunch of buttons each color and number. WPF
问题描述
所以我有这个包含5x5按钮的网格,我想在每个按钮上随机生成一个数字(1-5),并随机为每个按钮添加一个(5个)颜色。
< br $>
Ex
[RED 1] [BLUE 5] [GREEN 2] [RED 4] [YELLOW 3]
[绿色1] [黄色5] [橙色3]等等.....
好的,问题是,我应该采取哪些步骤认为?在这种情况下是否有一些好方法可以使用?我从头开始....
我感谢任何帮助!
- -
编辑
所以只有一个颜色的五个按钮,每种颜色都有编号1-5。
So I have this grid containing 5x5 buttons where I want to randomly generate one number(1-5) to each button, and randomly place one (of 5) color to each button.
Ex
[RED 1][BLUE 5][GREEN 2][RED 4][YELLOW 3]
[GREEN 1][YELLOW 5][Orange 3] and so on.....
Ok, so the question is, in which steps should I think? And are there some good methods to use in this case? I''m starting from scratch....
I appreciate any help!
---
EDIT
So there will only exist five buttons of one color, and to each color there are numbered 1-5.
推荐答案
您需要的是生成0-24的随机排列,为每个按钮分配一个数字。然后使用数字%5将数字和数字/ 5分配到颜色列表中。
要有效地生成随机排列,请尝试:
What you need is to generate a random permutation of 0-24, assigning one number for each of the buttons. Then just use number % 5 to assign the number and number / 5 to index into the color list.
To efficiently generate the random permutation try this:
using System.Collections.Generic;
using System.Linq;
private Random Rand = new Random(); // OR use a fixed "seed" here for reproducability when testing
private List<int> RandPerm(int N)
{
List<int> p = new List<int>(Enumerable.Range(0, N));
for (int k = 2; k <= N; k++)
{
int r = (int)Math.Ceiling(k * Rand.NextDouble());
--r;
p[k - 1] = p[r];
p[r] = k - 1;
}
return p;
}
这篇关于为每个颜色和数字分配一堆按钮。 WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!