接上一篇:
C#使用随机数模拟器来模拟世界杯排名(一)
C#使用随机数模拟器来模拟世界杯排名(一)_斯内科的博客-CSDN博客
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCupRankingDemo
{
public class RandomUtil
{
private static int[] nums;
private static Random rand = new Random();
///
/// 初始化数组索引
///
///
public static void Init(int[] indexArray)
{
nums = indexArray;
}
///
/// 洗牌算法,洗牌算法的时间复杂度是 O(N)
/// 第一次有N种可能,第二次有N-1种可能,第三次有N-2种可能
/// ...最后一次只有一种可能
///
///
public static int[] Shuffle()
{
int n = nums.Length;
int[] copy = new int[n];
Array.Copy(nums, copy, n);
for (int i = 0; i < n; i++)
{
// 生成一个 [i, n-1] 区间内的随机数
int r = i + rand.Next(n - i);
// 交换 nums[i] 和 nums[r],即获取第r个数【放到第i个位置】
Swap(copy, i, r);
}
return copy;
}
///
/// 交换数组两个数的值
///
///
///
///
private static void Swap(int[] nums, int i, int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
控件UcCountry由一个panel(width:300,height:180)组成
panel由两个Label【国家,胜率】和一个PictureBox【设置:BackgroundImageLayout为Zoom】
自定义控件的设计器代码如下:
namespace WorldCupRankingDemo
{
partial class UcCountry
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.panel2 = new System.Windows.Forms.Panel();
this.lblWinningRatio = new System.Windows.Forms.Label();
this.lblCountryName = new System.Windows.Forms.Label();
this.picNationalFlag = new System.Windows.Forms.PictureBox();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.picNationalFlag)).BeginInit();
this.SuspendLayout();
//
// panel2
//
this.panel2.Controls.Add(this.lblWinningRatio);
this.panel2.Controls.Add(this.lblCountryName);
this.panel2.Controls.Add(this.picNationalFlag);
this.panel2.Location = new System.Drawing.Point(3, 3);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(300, 180);
this.panel2.TabIndex = 2;
//
// lblWinningRatio
//
this.lblWinningRatio.AutoSize = true;
this.lblWinningRatio.ForeColor = System.Drawing.Color.Green;
this.lblWinningRatio.Location = new System.Drawing.Point(181, 7);
this.lblWinningRatio.Name = "lblWinningRatio";
this.lblWinningRatio.Size = new System.Drawing.Size(32, 17);
this.lblWinningRatio.TabIndex = 5;
this.lblWinningRatio.Text = "胜率";
//
// lblCountryName
//
this.lblCountryName.AutoSize = true;
this.lblCountryName.ForeColor = System.Drawing.Color.Blue;
this.lblCountryName.Location = new System.Drawing.Point(25, 7);
this.lblCountryName.Name = "lblCountryName";
this.lblCountryName.Size = new System.Drawing.Size(32, 17);
this.lblCountryName.TabIndex = 4;
this.lblCountryName.Text = "国家";
//
// picNationalFlag
//
this.picNationalFlag.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.picNationalFlag.Location = new System.Drawing.Point(25, 30);
this.picNationalFlag.Name = "picNationalFlag";
this.picNationalFlag.Size = new System.Drawing.Size(250, 150);
this.picNationalFlag.TabIndex = 3;
this.picNationalFlag.TabStop = false;
//
// UcCountry
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panel2);
this.Name = "UcCountry";
this.Size = new System.Drawing.Size(306, 186);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.picNationalFlag)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Panel panel2;
public PictureBox picNationalFlag;
public Label lblWinningRatio;
public Label lblCountryName;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WorldCupRankingDemo
{
public partial class FormWorldCupRanking : Form
{
public FormWorldCupRanking()
{
InitializeComponent();
}
private void FormWorldCupRanking_Load(object sender, EventArgs e)
{
CountryUtil.InitCountry();
int[] indexArray = new int[CountryUtil.ListWorldCup.Count];
for (int i = 0; i < indexArray.Length; i++)
{
indexArray[i] = i;
}
RandomUtil.Init(indexArray);
int[] destArray = RandomUtil.Shuffle();
int rowCount = (destArray.Length + 1) / 2;
for (int i = 0; i < rowCount; i++)
{
UcCountry ucCountry1 = new UcCountry();
ucCountry1.Name = $"ucCountry{i * 2 + 1}";
Country country1 = CountryUtil.ListWorldCup[destArray[i * 2]];
ucCountry1.lblCountryName.Text = country1.CountryName;
ucCountry1.lblWinningRatio.Text = $"胜率:{country1.WinningRatio}";
ucCountry1.picNationalFlag.BackgroundImage = country1.NationalFlag;
ucCountry1.Tag = country1;
ucCountry1.Location = new Point(5, i * 200 + 5);
this.Controls.Add(ucCountry1);
Button button = new Button();
button.Name = $"button{i + 1}";
button.Text = "VS";
button.Font = new Font("宋体", 30, FontStyle.Bold);
button.Size = new Size(100, 100);
button.Location = new Point(330, i * 200 + 60);
this.Controls.Add(button);
if (i * 2 + 1 < destArray.Length)
{
//对奇数个世界杯比赛国家特殊处理,最后一个国家直接胜利
UcCountry ucCountry2 = new UcCountry();
ucCountry2.Name = $"ucCountry{i * 2 + 2}";
Country country2 = CountryUtil.ListWorldCup[destArray[i * 2 + 1]];
ucCountry2.lblCountryName.Text = country2.CountryName;
ucCountry2.lblWinningRatio.Text = $"胜率:{country2.WinningRatio}";
ucCountry2.picNationalFlag.BackgroundImage = country2.NationalFlag;
ucCountry2.Tag = country2;
ucCountry2.Location = new Point(455, i * 200 + 5);
this.Controls.Add(ucCountry2);
}
}
}
}
}