嗨伙计们,我不知道为什么我的洗牌没有洗牌。我没有得到错误,但它没有改组,但当我打印我的列表时,它出来排序而不是洗牌。谁能告诉我问题是什么?我应该把我的shuffle放在我的链表中
我的捆绑/套牌:
namespace BlackJack
{
class Bundle
{
//private List<Card> deck1;
GameLinkedList<Card> deck = new GameLinkedList<Card>();
public void Shuffle()
{
for (int index = 0; index > 52; index++)
{
Random r = new Random();
int count = deck.Count();
for (int i = 0; i < count; i++)
{
int rnd = (int)(r.Next(count));
deck.AddLast(deck.Delete(rnd));
}
}
}
public void view()
{
deck.Print();
}
}
}
我的链表:
namespace BlackJack
{
class GameLinkedList<T> where T : IComparable
{
private Node<T> head;
private Node<T> temp;
private int count = 0;
public void Clear()
{
head = null;
count = 0;
}
public void Delete(T t)
{
Node<T> prev = temp;
temp = head;
while (temp != null)
{
if (t.CompareTo(temp.data) == 0)
{
prev.next = temp.next;
temp = null;
}
else
{
prev = temp;
temp = temp.next;
}
}
count--;
}
public T Delete(int i)
{
Node<T> prev = temp;
temp = head;
while (temp != null)
{
if (i.CompareTo(temp.id) == 0)
{
prev.next = temp.next;
count--;
return temp.data;
}
else
{
prev = temp;
temp = temp.next;
}
}
return temp.data;
}
public void AddLast(T t)
{
if (head == null)
{
head = new Node<T>();
head.data = t;
head.next = null;
}
else
{
Node<T> n = new Node<T>();
n.data = t;
Node<T> current = head;
while (current.next != null)
{
current = current.next;
}
current.next = n;
}
count++;
}
public int Count()
{...}
public void Print()
{
temp = head;
while (temp != null)
{
Console.WriteLine(temp.data.ToString());
temp = temp.next;
}
}
}
}
答案 0 :(得分:0)
如果没有可靠地重现问题的a good, minimal, complete code example,则无法识别代码中的每个问题。但是,Shuffle()
方法中存在许多明显的问题:
index > 52
。由于index
从0开始,表达式index > 52
立即为false,因此循环根本不会执行。Random
对象。这几乎总是一个错误,因为代码执行得如此之快,以至于每个新的Random
对象都将以相同的种子开始,从而返回相同的数字序列。Random
错误),但这样做只会增加实现的低效率。在我看来,您可以通过一些小的改动来显着改善您当前的实施:
public void Shuffle()
{
Random r = new Random();
int count = deck.Count();
for (int i = 0; i < count; i++)
{
int rnd = (int)(r.Next(count--));
deck.AddLast(deck.Delete(rnd));
}
}
换句话说,不要打扰外圈,并且在选择新卡时,请不要包含您之前选择的任何卡。通过每次迭代递减count
,这将限制将卡放在最后放置到尚未被选中的卡上。