Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将游戏对象分配到列表并给它们一个索引_C#_Oop_Unity3d - Fatal编程技术网

C# 将游戏对象分配到列表并给它们一个索引

C# 将游戏对象分配到列表并给它们一个索引,c#,oop,unity3d,C#,Oop,Unity3d,所以基本上,我有一个每个数字都有2个的列表,我想随机将其中的一个数字分配给每个被实例化的卡。这是一个拼图记忆游戏,在那里你可以找到匹配的 当我有2个网格行和4个网格列时,游戏运行得很好,但是如果我更改了行或列的数量,则会得到:ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合的大小。参数名:index,它阻止我扩展游戏。我试着在当前列表中添加额外的数字,但这似乎不起作用,而且如果我想有很多行和列,这种方法似乎也不是很有效 我想做的是获取一个列表,然后在每

所以基本上,我有一个每个数字都有2个的列表,我想随机将其中的一个数字分配给每个被实例化的卡。这是一个拼图记忆游戏,在那里你可以找到匹配的

当我有2个网格行和4个网格列时,游戏运行得很好,但是如果我更改了行或列的数量,则会得到:ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合的大小。参数名:index,它阻止我扩展游戏。我试着在当前列表中添加额外的数字,但这似乎不起作用,而且如果我想有很多行和列,这种方法似乎也不是很有效

我想做的是获取一个列表,然后在每次开始匹配时洗牌它的内容,然后在实例化时按顺序(内容已经洗牌)循环遍历索引,这是我的逻辑,但由于某种原因它无法工作

有人能帮我扩展一下,找到一个更好的存储索引的方法吗

代码如下:

 public const int gridRows = 2;
public const int gridCols = 4;
public const float offsetX = 4f;
public const float offsetY = 5f;

public MainCard originalCard;
public Sprite[] images;


private void Start()
{
    Vector3 startPos = originalCard.transform.position;
    List<int> numbers = new List<int> { 0, 0, 1, 1, 2, 2, 3, 3 };
    numbers = Shuffle(numbers);

    for (int i = 0; i < gridCols; i++)
    {
        for (int x = 0; x < gridRows; x++)
        {
            MainCard card;
            if (i == 0 && x == 0)
            {
                card = originalCard;
            }
            else
            {
                card = Instantiate(originalCard) as MainCard;
            }

            int index = x * gridCols + i;
            int id = numbers[index];
            card.ChangeSprite(id, images[id]);

            float posX = (offsetX * i) + startPos.x;
            float posY = (offsetX * x) + startPos.y;
            card.transform.position = new Vector3(posX, posY, startPos.z);
        }
    }

    List<T> Shuffle<T>(List<T> cards)
    {
        for (int i = 0; i < cards.Count; i++)
        {
            T temp = cards[i];
            int randomIndex = Random.Range(i, cards.Count);
            cards[i] = cards[randomIndex];
            cards[randomIndex] = temp;
        }

        return cards;
    }

}
public const int gridRows=2;
公共常数int gridCols=4;
公共常数浮动偏移量x=4f;
公共常数浮动补偿=5f;
公共主卡原件卡;
公共精灵[]形象;
私有void Start()
{
Vector3 startPos=originalCard.transform.position;
列表编号=新列表{0,0,1,1,2,2,3,3};
数字=洗牌(数字);
for(int i=0;i

}一种解决方案是将项目分成多个部分:

using System.Linq;//to Add

public int gridRows = 2;
public  int gridCols = 4;
public List<int> numbers;
public void Start()
{
    int[,] cards = new int[gridRows, gridCols];
    //just put different numbers not needed to duplicate 
    //except if you want to increase the probability
    // to have lot of same number
    numbers = new List<int> { 0, 1, 2, 3 };
    InitializeGame(cards);
}

private void InitializeGame(int[,] cards)
{
    //1 you could shuffle your number if you want
    //numbers = Shuffle(numbers);

    //2 you initialize cards to value not used (-1 here)
    for (int r = 0; r < gridRows; r++)
    {
        for (int c = 0; c < gridCols; c++)
        {
            cards[r, c] = -1;
        }
    }

    // Test if you have filled up all cards
    //Is there again one card with the value -1?
    while (cards.Cast<int>().Any(x => x == -1))
    {
        //3 you pick randomnly a number
        var numCard = numbers[Random.Range(0, numbers.Count)];

        //4 you fill same number to 2 different cards here
        var nbcardTofill = 2;
        while (nbcardTofill != 0)
        {
            //you choose randomny card (row and col) which is free (value = -1) not need but add fun
            var colRandom = Random.Range(0, gridCols);
            var rowRandom = Random.Range(0, gridRows);
            if(cards[rowRandom, colRandom] == -1)
            {
                nbcardTofill--;
                cards[rowRandom, colRandom] = numCard;
            }
        }
    }
}
使用System.Linq//加
公共int网格行=2;
公共int gridCols=4;
公开名单编号;
公开作废开始()
{
int[,]cards=新int[gridRows,gridCols];
//只需输入不需要重复的不同数字即可
//除非你想增加概率
//有很多相同的号码
数字=新列表{0,1,2,3};
初始化名称(卡片);
}
私有无效初始值设定名称(int[,]卡)
{
//1如果你想的话,你可以把你的号码洗牌
//数字=洗牌(数字);
//2您将卡初始化为未使用的值(-1)
对于(int r=0;rx=-1))
{
//3.你只能随机选择一个数字
var numCard=numbers[Random.Range(0,numbers.Count)];
//4你在这里把同样的号码填到两张不同的卡片上
var nbcardTofill=2;
while(nbcardTofill!=0)
{
//您可以选择randomny卡(行和列),这是免费的(值=-1),不需要,但可以增加乐趣
var colRandom=Random.Range(0,gridCols);
var rowdrandom=Random.Range(0,网格行);
if(卡片[rowRandom,colRandom]=-1)
{
nbcardTofill--;
卡片[rowRandom,colRandom]=numCard;
}
}
}
}
现在你有了充满数字的阵列卡,你现在可以画游戏了

因此,您可以根据需要更改列数/行数和数字列表

这只是编程的一种方式,但我认为它更具可读性。我添加了很多随机化来增加乐趣


另一种方法是按顺序填充你的牌(2乘2,数字相同),并在最后对所有牌进行洗牌…

一种解决方案是将你的项目分成多个部分:

using System.Linq;//to Add

public int gridRows = 2;
public  int gridCols = 4;
public List<int> numbers;
public void Start()
{
    int[,] cards = new int[gridRows, gridCols];
    //just put different numbers not needed to duplicate 
    //except if you want to increase the probability
    // to have lot of same number
    numbers = new List<int> { 0, 1, 2, 3 };
    InitializeGame(cards);
}

private void InitializeGame(int[,] cards)
{
    //1 you could shuffle your number if you want
    //numbers = Shuffle(numbers);

    //2 you initialize cards to value not used (-1 here)
    for (int r = 0; r < gridRows; r++)
    {
        for (int c = 0; c < gridCols; c++)
        {
            cards[r, c] = -1;
        }
    }

    // Test if you have filled up all cards
    //Is there again one card with the value -1?
    while (cards.Cast<int>().Any(x => x == -1))
    {
        //3 you pick randomnly a number
        var numCard = numbers[Random.Range(0, numbers.Count)];

        //4 you fill same number to 2 different cards here
        var nbcardTofill = 2;
        while (nbcardTofill != 0)
        {
            //you choose randomny card (row and col) which is free (value = -1) not need but add fun
            var colRandom = Random.Range(0, gridCols);
            var rowRandom = Random.Range(0, gridRows);
            if(cards[rowRandom, colRandom] == -1)
            {
                nbcardTofill--;
                cards[rowRandom, colRandom] = numCard;
            }
        }
    }
}
使用System.Linq//加
公共int网格行=2;
公共int gridCols=4;
公开名单编号;
公开作废开始()
{
int[,]cards=新int[gridRows,gridCols];
//只需输入不需要重复的不同数字即可
//除非你想增加概率
//有很多相同的号码
数字=新列表{0,1,2,3};
初始化名称(卡片);
}
私有无效初始值设定名称(int[,]卡)
{
//1如果你想的话,你可以把你的号码洗牌
//数字=洗牌(数字);
//2您将卡初始化为未使用的值(-1)
对于(int r=0;rx=-1))
{
//3.你只能随机选择一个数字
var numCard=numbers[Random.Range(0,numbers.Count)];
//4你在这里把同样的号码填到两张不同的卡片上
var nbcardTofill=2;
while(nbcardTofill!=0)
{
//您可以选择randomny卡(行和列),这是免费的(值=-1),不需要,但可以增加乐趣
var colRandom=Random.Range(0,gridCols);
var rowdrandom=Random.Range(0,网格行);
if(卡片[rowRandom,colRandom]=-1)
{
nbcardTofill--;
卡片[rowRandom,colRandom]=numCard;
}
}
}
}
现在你有了用num填充的阵列卡
int randomIndex = Random.Range(i, cards.Count);
int randomIndex = Random.Range(i, cards.Count-1);