我有兩個陣列,一個是 PictureBox 陣列,另一個是 Integer 陣列,它們都具有相同數量的元素。我希望每次都隨機打亂兩個陣列,但都以相同的方式打亂。
如果我使用兩個 PictureBox 陣列或兩個 Integer 陣列,這是一個有效的代碼,但是我希望它對一個 PictureBox 陣列和一個 Integer 陣列進行混洗。
這是我想要作業的代碼:(兩個不同的陣列)
PictureBox[] cards = new PictureBox[13];
// PictureBox[] numValue = new PictureBox[13];
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
Random rnd = new Random();
for (int i = 0; i < cards.Length - 1; i )
{
int j = rnd.Next(i, cards.Length);
PictureBox temp = cards[j];
cards[j] = cards[i];
cards[i] = temp;
temp = numbers[j]; //An error occurs here because numbers is not a PictureBox variable
numbers[j] = numbers[i];
numbers[i] = temp;
}
這是有效的代碼:(對于相同的陣列)
PictureBox[] numValue = new PictureBox[13];
PictureBox[] cards = new PictureBox[13];
// int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
Random rnd = new Random();
for (int i = 0; i < cards.Length - 1; i )
{
int j = rnd.Next(i, cards.Length);
PictureBox temp = cards[j];
cards[j] = cards[i];
cards[i] = temp;
temp = numValue [j];
numValue [j] = numValue [i];
numValue [i] = temp;
}
如果您知道可以幫助我的其他不同代碼,請隨時分享!
uj5u.com熱心網友回復:
只需創建兩個臨時變數
for (int i = 0; i < cards.Length - 1; i )
{
int j = rnd.Next(i, cards.Length);
PictureBox tempPictureBox = cards[j]; // One for PictureBox
cards[j] = cards[i];
cards[i] = tempPictureBox;
int tempInt = numValue[j]; // Another one for int
numValue[j] = numValue[i];
numValue[i] = tempInt;
}
uj5u.com熱心網友回復:
如果你想要一個可以在任何集合上使用的洗牌器,你可以創建這樣一個類。
public class Shuffler
{
private List<Guid> order;
private List<Guid> createOrder(int count)
{
return Enumerable.Range(0, count)
.Select(i => Guid.NewGuid())
.ToList();
}
public Shuffler(int count)
{
order = createOrder(count);
}
public void Reshuffle()
{
order = createOrder(order.Count);
}
public IEnumerable<T> Shuffle<T>(IEnumerable<T> collection)
{
return collection.Zip(order)
.OrderBy(e => e.Second)
.Select(e => e.First);
}
}
當您創建 的實體時Shuffler,它會在內部創建一個串列或偽隨機元素(這里它們是Guids,但它可以是隨機選擇的整數,或任何您想要的)。要混洗一個集合,只需將它傳遞給該Shuffle方法。每個通過的集合都將以完全相同的方式重新排列。
它可以改進,例如它期望每個集合將具有相同數量的元素,并且這個數字被傳遞給Shuffler建構式,這可以放寬。也沒有錯誤檢查,但它應該給出原理的概念。
這是一個使用示例。您可以在 Linqpad 中嘗試。
void Main()
{
// The test collections of different types.
var cards = Enumerable.Range(0, 13)
.Select(i => new PictureBox { Foo = $"{i}"})
.ToArray();
var numbers = Enumerable.Range(0, 13)
.ToArray();
// Creation of the Shuffler instance
var shuffler = new Shuffler(cards.Length);
// Using the Shuffler instance to shuffle collections.
// Any number of collections can be shuffled that way, in the same exact way.
cards = shuffler.Shuffle(cards).ToArray();
numbers = shuffler.Shuffle(numbers).ToArray();
// Display results.
cards.Dump();
numbers.Dump();
// Perform a new shuffle using the same Shuffler instance.
shuffler.Reshuffle();
// Again use the Shuffler to shuffle the collections.
cards = shuffler.Shuffle(cards).ToArray();
numbers = shuffler.Shuffle(numbers).ToArray();
// Display results.
cards.Dump();
numbers.Dump();
}
// You can define other methods, fields, classes and namespaces here
public class PictureBox
{
public string Foo { get; set; }
}
public class Shuffler
{
private List<Guid> order;
private List<Guid> createOrder(int count)
{
return Enumerable.Range(0, count)
.Select(i => Guid.NewGuid())
.ToList();
}
public Shuffler(int count)
{
order = createOrder(count);
}
public void Reshuffle()
{
order = createOrder(order.Count);
}
public IEnumerable<T> Shuffle<T>(IEnumerable<T> collection)
{
return collection.Zip(order)
.OrderBy(e => e.Second)
.Select(e => e.First);
}
}
結果集:
cards number
"12" 12
"1" 1
"0" 0
"11" 11
"3" 3
"2" 2
"9" 9
"5" 5
"10" 10
"8" 8
"4" 4
"7" 7
"6" 6
具有靜態欄位和Shuffle擴展方法的純靜態類也可以按照相同的原理制作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366747.html
