我想在一个数组中洗牌 5 张牌
I want to shuffle 5 cards in an array
我在 GabeObject[] 卡片中有 5 张卡片。我想将这些卡片洗牌并保存到 GameObject[] 卡片洗牌中。这段代码有问题。它洗牌相同。我怎样才能让它不再洗同一张牌?谢谢。
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject[] cards;
public GameObject[] cardshuffle;
int i=0;
int j = 0;
int save=0;
int[] kayit = new int[10];
int kayit2;
public GameObject[] player1;
public GameObject[] player2;
public GameObject[] player3;
// Use this for initialization
void Start () {
kayit[0] = 20;
while(i<5)
{
save = Random.Range(0, 5);
for (int a = 0; a < 5; a++ )
{
if(kayit[a] == save)
{
kayit2 = save;
}
}
if (kayit2 != save)
{
cardshuffle[i] = cards[save];
}
else
i--;
kayit[i] = save;
i++;
}
while (cards[j] != null)
{
player1[j] = cards[j];
j++;
Debug.Log(j);
}
}
// Update is called once per frame
void Update () {
}
}
向此方法传递一个 int 数组。它将随机排列数组:
void Shuffle(int[] texts)
{
// Knuth shuffle algorithm :: courtesy of Wikipedia :)
for (int t = 0; t < texts.Length; t++)
{
int tmp = texts[t];
int r = Random.Range(t, texts.Length);
texts[t] = texts[r];
texts[r] = tmp;
}
}
好的,感谢 Plastic Sturgeon。
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject[] cards;
public GameObject[] tmp;
public GameObject[] player1;
public GameObject[] player2;
public GameObject[] player3;
// Use this for initialization
void Start () {
for (int t = 0; t < cards.Length; t++)
{
tmp[0] = cards[t];
int r = Random.Range(t, cards.Length);
cards[t] = cards[r];
cards[r] = tmp[0];
}
for(int i=0; i<cards.Length;i++)
{
player1[i] = cards[i];
}
}
// Update is called once per frame
void Update () {
}
}
我在 GabeObject[] 卡片中有 5 张卡片。我想将这些卡片洗牌并保存到 GameObject[] 卡片洗牌中。这段代码有问题。它洗牌相同。我怎样才能让它不再洗同一张牌?谢谢。
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject[] cards;
public GameObject[] cardshuffle;
int i=0;
int j = 0;
int save=0;
int[] kayit = new int[10];
int kayit2;
public GameObject[] player1;
public GameObject[] player2;
public GameObject[] player3;
// Use this for initialization
void Start () {
kayit[0] = 20;
while(i<5)
{
save = Random.Range(0, 5);
for (int a = 0; a < 5; a++ )
{
if(kayit[a] == save)
{
kayit2 = save;
}
}
if (kayit2 != save)
{
cardshuffle[i] = cards[save];
}
else
i--;
kayit[i] = save;
i++;
}
while (cards[j] != null)
{
player1[j] = cards[j];
j++;
Debug.Log(j);
}
}
// Update is called once per frame
void Update () {
}
}
向此方法传递一个 int 数组。它将随机排列数组:
void Shuffle(int[] texts)
{
// Knuth shuffle algorithm :: courtesy of Wikipedia :)
for (int t = 0; t < texts.Length; t++)
{
int tmp = texts[t];
int r = Random.Range(t, texts.Length);
texts[t] = texts[r];
texts[r] = tmp;
}
}
好的,感谢 Plastic Sturgeon。
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject[] cards;
public GameObject[] tmp;
public GameObject[] player1;
public GameObject[] player2;
public GameObject[] player3;
// Use this for initialization
void Start () {
for (int t = 0; t < cards.Length; t++)
{
tmp[0] = cards[t];
int r = Random.Range(t, cards.Length);
cards[t] = cards[r];
cards[r] = tmp[0];
}
for(int i=0; i<cards.Length;i++)
{
player1[i] = cards[i];
}
}
// Update is called once per frame
void Update () {
}
}