原文:
基于WPF实现双色球
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37591671/article/details/82959625
基于WPF实现双色球
文章目录
1.实现思路:
分析:双色球由六个红色号码和一个蓝色号码组成;红色球号码从01–33中选择,而且不能重复;蓝色球号码从01–16中选择(可以跟红球重复)。
(1)点击开始按钮,开启七个线程,获取数据
(2)由于前六个的数据不一样,所以应当将获得的数据与除了当前Lable外的其他Lable内容进行比较,如果重复,则需要重新获取;否则,显示
(3)当点击停止按钮,等待所有线程执行完,显示结果
2.实现代码:
(1)点击后,开启线程执行:
private bool IsGoon = true;
private static object _Lock = new object();
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
try
{
Image image = (Image)sender;
if (image.Source.ToString().Equals("pack://application:,,,/DoubleChromosphere;component/Images/click.png"))
{
this.IsGoon = true;
image.Source = new BitmapImage(new Uri("Images/stop.jpg", UriKind.RelativeOrAbsolute));
//foreach (UIElement element in LayoutRoot.Children)
//{
// if (element is Label)
// {
// ((Label)element).Content = "00";
// }
//}
Thread.Sleep(500);
TaskFactory taskFactory = new TaskFactory();
List<Task> taskList = new List<Task>();
foreach (UIElement element in LayoutRoot.Children)
{
if (element is Label)
{
Label lbl = element as Label;
taskList.Add(taskFactory.StartNew(() =>
{
while (this.IsGoon)
{
string text = "";
if (this.Dispatcher.Invoke(() => { return lbl.Name.Contains("red"); }))
{
//1 解决冲突的最好办法,是没有冲突,上来就随机分6组,每个线程只取一个组的
//2 线程安全的集合,出一个进一个
//3 每次先找出6个不同的球,然后分配给线程
//4 每次线程随意获取球,更新前检测下有没有冲突,有则重新随机,没有则更新
text = this.GetRandomText();
lock (_Lock)
{
List<string> redList = this.Dispatcher.Invoke(() => GetUsedRed(lbl));
if (redList.Contains(text))
continue;
else
{
Thread.Sleep(50);//让停止的时候,线程可能在这里
this.UpdateLable(lbl, text);
}
}
}
else
{
int index = RandomHelper.GetRandomNumber(0, 16);
//Random random = new Random();//伪随机
//string text = this.BlueNums[random.Next(0, 15)];
//int index = RandomHelper.GetRandomNumber(0, 16);
text = this.BlueNums[index];
//lbl.Text = text;//不行
this.UpdateLable(lbl, text);
}
//this.UpdateLable(lbl, text);
Thread.Sleep(50);
}
}));
}
}
//所有线程执行完,展示结果
taskFactory.ContinueWhenAll(taskList.ToArray(), tList => this.ShowResult());
{
}
}
else
{
image.Source = new BitmapImage(new Uri("Images/click.png", UriKind.Relative));
this.IsGoon = false;
}
}
catch (Exception ex)
{
Console.WriteLine("双色球启动出现异常:{0}", ex.Message);
}
}
(2)获取随机号码代码:
private string GetRandomText()
{
int index = RandomHelper.GetRandomNumber(0, 33);
return this.RedNums[index];
}
public class RandomHelper
{
/// <summary>
/// 获取随机数
/// </summary>
/// <param name="min">能出现的最小值</param>
/// <param name="max">不会出现</param>
/// <returns></returns>
public static int GetRandomNumber(int min, int max)
{
Guid guid = Guid.NewGuid();
string sGuid = guid.ToString();
int seed = DateTime.Now.Millisecond;
for (int i = 0; i < sGuid.Length; i++)
{
switch (sGuid[i])
{
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
seed = seed + 1;
break;
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
seed = seed + 2;
break;
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
seed = seed + 3;
break;
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
seed = seed + 3;
break;
default:
seed = seed + 4;
break;
}
}
Random random = new Random(seed);
return random.Next(min, max);
}
}
(3)获取已经存在的数据代码
private List<string> GetUsedRed(Label lbl)
{
List<string> usedList = new List<string>();
foreach (var labItem in LayoutRoot.Children)
{
if (labItem is Label && labItem != lbl)//当前的数字可以用
{
usedList.Add(((Label)labItem).Content.ToString());
}
}
return usedList;
}
(4)更新Lable内容代码
private void UpdateLable(Label lbl, string text)
{
this.Dispatcher.Invoke(new Action(() =>
{
lbl.Content = text;
}));
}
(5)展示结果代码:
private void ShowResult()
{
this.Dispatcher.Invoke(() =>
{
MessageBox.Show(string.Format("本期双色球结果为:{0} {1} {2} {3} {4} {5} 蓝球{6}"
, this.labred1.Content
, this.labred2.Content
, this.labred3.Content
, this.labred4.Content
, this.labred5.Content
, this.labred6.Content
, this.lab7.Content));
foreach (UIElement element in LayoutRoot.Children)
{
if (element is Label)
{
((Label)element).Content = "00";
}
}
}
);
}
(6)数据:
#region Data
/// <summary>
/// 红球集合
/// </summary>
private string[] RedNums =
{
"01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33"
};
/// <summary>
/// 蓝球集合
/// </summary>
private string[] BlueNums =
{
"01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16"
};
#endregion