二维数组无法获得预期的输出

时间:2018-06-04 14:41:40

标签: c# android unity3d unityscript

我先告诉你我的结果

enter image description here

上图是Two Dimensional Array.

在控制台中显示的结果

所以这里是代码

if ((nArray1 > 0 && nArray2 > 0) || nArray1 > 1)        //Beginning at line 2 of the second column ...
    {
        if (arrTmp[1, 1] != undefined)
        {
            xStart = 1;
            yStart = 1;
        }
        else
        {
            xStart = 2;
            yStart = 0;
        }
        //Primary arrays ....
        for (x = xStart; x <= nArray1; x++)     //record...
        {
            //try 6 here
            for (y = yStart; y < 6; y++)           //field 
            {
                if (RCount == 0)                    //Compare the first cell unconditionally with the next one ...
                {
                    if (arrTmp[y, x - 1] != undefined)
                    {
                        //Hong
                        arrResult11[RCount] = 2;
                        arrResult12[RCount] = 2;
                        arrResult13[RCount] = 2;
                    }
                    else
                    {

                        //Hong
                        arrResult11[RCount] = 1;
                        arrResult12[RCount] = 2;
                        arrResult13[RCount] = 1;
                    }
                }
                else
                {
                    if (arrTmp[y, x] == undefined) //Check before moving to the next box.
                    {
                        yLastp = y;
                        yStart = 0;
                        break;  //Go to the next line ...
                    }
                    if (y == 0)     //The first column is ...
                    {
                        if (arrTmp[yLastp, x - 2] != undefined) //Moved to a new cell, but if there is a value after the left column ....
                        {
                            arrResult11[RCount] = 2;    //Hong
                            arrResult12[RCount] = 1;    //Hong
                        }
                        else
                        {
                            arrResult11[RCount] = 2;    //Hong
                            arrResult12[RCount] = 2;    //Hong                      
                        }
                    }
                    else
                    {
                        if (arrTmp[y, x - 1] != undefined)
                        {
                            arrResult11[RCount] = 2;    //Hong
                            arrResult12[RCount] = 2;    //Hong                          
                        }
                        else
                        {
                            arrResult11[RCount] = 1;    //Hong
                            arrResult12[RCount] = 2;    //Hong                          
                        }
                    }
                    if (arrResult11[RCount - 1] == 2 && arrResult12[RCount - 1] == 1)  //If the previous contents are Hong-Cheong ...
                    {
                        if (arrResult11[RCount] == 2 && arrResult12[RCount] == 2)  //Currently, Hong-Hong ...
                            arrResult13[RCount] = 2;
                        else
                            arrResult13[RCount] = 1;
                    }
                    else if (arrResult11[RCount] == arrResult11[RCount - 1] && arrResult12[RCount] == arrResult12[RCount - 1])
                    {
                        arrResult13[RCount] = 2;
                    }
                    else
                    {
                        arrResult13[RCount] = 1;
                    }
                }
                RCount++;
            }
        }

以下是我尝试更改此行代码时的结果

//Primary arrays ....
        for (x = xStart; x <= nArray1; x++)     //record...
        {
            //try 6 here
            for (y = yStart; y < 8; y++)           //field 
            {
                 //more codes
            }
        }

enter image description here

请查看突出显示的区域。

但我需要的是这种结果

enter image description here

所以这里是

的整个代码
string[] arrPartStr = history.Split(',');

    int[] arrPart = new int[arrPartStr.Length];

    for (int c = 0; c < arrPart.Length; c++)
    {
        int temp = 0;
        if (arrPartStr[c][0] == 'P') temp = 100;
        else if (arrPartStr[c][0] == 'B') temp = 200;
        else temp = 300;
        if (arrPartStr[c][1] == 'P') temp += 10;
        if (arrPartStr[c][2] == 'P') temp += 1;

        arrPart[c] = temp;
    }

    //20 is the highest possible size
    int[,] arrTmp = new int[104, 104];

    for (int arrTmpx = 0; arrTmpx < arrTmp.GetLength(0); arrTmpx++)
    {
        for (int arrTmpy = 0; arrTmpy < arrTmp.GetLength(1); arrTmpy++)
        {
            arrTmp[arrTmpx, arrTmpy] = -1;  // think -1 as undefined.
        }
    }

    int i = 0;
    int intTmp_Ori = 0;    //For storing the result value (100,200,300)         
    int intTmp = 0;    //For storing the result value (1,2,3)
    bool bNextMove = false;
    int nArray1 = 0; //record
    int nArray2 = 0; //Column    
    intTmp_Ori = arrPart[0];
    intTmp = intTmp_Ori / 100;

    for (i = 0; i < arrPart.Length; i++)            //Turns history
    {
        if (i == 0) //If it's your first history
        {
            intTmp_Ori = (int)(arrPart[i]);       //Save current value
            intTmp = (int)(intTmp_Ori / 100);     //P,B,T Value
            bNextMove = false;
        }
        else    //From the second
        {
            if ((int)((int)(arrPart[i]) / 100) == 3)                //Tie
            {
                bNextMove = false;
            }
            else if ((int)((int)(arrPart[i]) / 100) != intTmp && intTmp != 3)//Unlike previous result
            {
                nArray1++;  //Go to next record
                //reset
                nArray2 = 0;
                intTmp_Ori = (int)(arrPart[i]);
                intTmp = (int)(intTmp_Ori / 100);
                bNextMove = false;
            }
            else if ((int)((int)(arrPart[i]) / 100) == intTmp || intTmp == 3)//If it is the same as the previous result.
            {
                if (intTmp == 3) nArray2--;
                bNextMove = true;
                intTmp_Ori = (int)(arrPart[i]);
                intTmp = (int)(intTmp_Ori / 100);
            }
        }
        if (bNextMove) nArray2++;

        arrTmp[nArray2,nArray1] = intTmp;
    }

    int[] arrResult11 = new int[104];  //1 for left value storage
    int[] arrResult12 = new int[104];  //For right value comparison
    int[] arrResult13 = new int[104];  //Array value (1,2) for the result in blue, red.

    int BfResult = 0 ;
    int xx = 0;
    int yy = 0; //Landscape, portrait ...
    int zz = 0; //Let's go over the number of Kan ...

    int x = 0;
    int y = 0;
    int xStart = 0;
    int yStart = 0;
    int yLastp = 0;
    int RCount = 0;
    int tmpData = 0;

    int intCount1 = 0;
    int intCount2 = 0;

    int undefined = -1;

    if ((nArray1 > 0 && nArray2 > 0) || nArray1 > 1)        //Beginning at line 2 of the second column ...
    {
        if (arrTmp[1, 1] != undefined)
        {
            xStart = 1;
            yStart = 1;
        }
        else
        {
            xStart = 2;
            yStart = 0;
        }
        //Primary arrays ....
        for (x = xStart; x <= nArray1; x++)     //record...
        {
            //try 6 here
            for (y = yStart; y < 8; y++)           //field 
            {
                if (RCount == 0)                    //Compare the first cell unconditionally with the next one ...
                {
                    if (arrTmp[y, x - 1] != undefined)
                    {
                        //Hong
                        arrResult11[RCount] = 2;
                        arrResult12[RCount] = 2;
                        arrResult13[RCount] = 2;
                    }
                    else
                    {

                        //Hong
                        arrResult11[RCount] = 1;
                        arrResult12[RCount] = 2;
                        arrResult13[RCount] = 1;
                    }
                }
                else
                {
                    if (arrTmp[y, x] == undefined) //Check before moving to the next box.
                    {
                        yLastp = y;
                        yStart = 0;
                        break;  //Go to the next line ...
                    }
                    if (y == 0)     //The first column is ...
                    {
                        if (arrTmp[yLastp, x - 2] != undefined) //Moved to a new cell, but if there is a value after the left column ....
                        {
                            arrResult11[RCount] = 2;    //Hong
                            arrResult12[RCount] = 1;    //Hong
                        }
                        else
                        {
                            arrResult11[RCount] = 2;    //Hong
                            arrResult12[RCount] = 2;    //Hong                      
                        }
                    }
                    else
                    {
                        if (arrTmp[y, x - 1] != undefined)
                        {
                            arrResult11[RCount] = 2;    //Hong
                            arrResult12[RCount] = 2;    //Hong                          
                        }
                        else
                        {
                            arrResult11[RCount] = 1;    //Hong
                            arrResult12[RCount] = 2;    //Hong                          
                        }
                    }
                    if (arrResult11[RCount - 1] == 2 && arrResult12[RCount - 1] == 1)  //If the previous contents are Hong-Cheong ...
                    {
                        if (arrResult11[RCount] == 2 && arrResult12[RCount] == 2)  //Currently, Hong-Hong ...
                            arrResult13[RCount] = 2;
                        else
                            arrResult13[RCount] = 1;
                    }
                    else if (arrResult11[RCount] == arrResult11[RCount - 1] && arrResult12[RCount] == arrResult12[RCount - 1])
                    {
                        arrResult13[RCount] = 2;
                    }
                    else
                    {
                        arrResult13[RCount] = 1;
                    }
                }
                RCount++;
            }
        }

        //Store group 1 results in a secondary array ....
        for (i = 0; i < RCount; i++)//Turns history ...
        {
            if (i == 0)
            {
                xx = 0;
                yy = 0;
                zz = 0;
            }
            else if ((int)(arrResult13[i]) != BfResult)   //Unlike previous results ...
            {
                xx++;               //Go to next record
                yy = 0;
                zz = 0;
            }
            else    //If it is the same as the previous result ...
            {
                yy++;
            }
            BfResult = (int)(arrResult13[i]);

            tmpData = (int)(arrayBigEyeRoad[yy, xx]);
            if (yy > 5)     //If the field is more than 6 ...
            {
                yy--;
                if (zz == 0)
                {
                    zz = xx + 1;
                }
                else
                {
                    zz++;
                }

            }
            else if (tmpData != 0)
            {
                yy--;
                if (yy == 0)
                {
                    zz = xx++;
                }
                else
                {
                    if (zz == 0)
                    {
                        zz = xx + 1;
                    }
                    else
                    {
                        zz++;
                    }
                }
            }
            else
            {
                zz = xx;
            }
            arrayBigEyeRoad[yy, zz] = BfResult;
            intCount1 = yy;
            intCount2 = zz;
        }
    }

以下是Two Dimensional Array

中的显示方式
bsb.makeRoad(history); // Road
    int[,] arrayRoad = bsb.GetRoad();

    string s = "";
    for (int y = 0; y < arrayRoad.GetLength(0); y++)
    {
        //just 27 for now
        for (int x = 0; x < 25; x++)
        {
            s += string.Format("{0:D2}", arrayRoad[y, x]);
            s += ".";
        }
        s += "\n";
    }
    Debug.Log(s);

我希望有人可以帮助我。

更新

当我尝试像这样添加我的表的索引时

int[,] arrayRoad = new int[15, 104];

The Original是:

int[,] arrayRoad = new int[6, 104];

我从这个

编辑了我的代码
//Primary arrays ....
        for (x = xStart; x <= nArray1; x++)     //record...
        {
            //try 6 here
            for (y = yStart; y < 6; y++)           //field 
            {
                //more code
            }
         }

到此

//Primary arrays ....
        for (x = xStart; x <= nArray1; x++)     //record...
        {
            //try 6 here
            for (y = yStart; y < 13; y++)           //field 
            {
                //more code
            }
         }

这是输出

enter image description here

但问题是我必须要保持索引[6,104]而不是[15,104]

2 个答案:

答案 0 :(得分:0)

目前还不清楚你的目标是什么(并且很难阅读你的代码)。这是如何将L写入2d数组的示例函数:

public static void WriteL(int x, int y, int width, int height, int value, ref int[,] array)
{
    int i = 0;
    int o = 0;
    while (o < height-1)
    {
        array[x+i, y+o] = value;
        o++;
    }
    while (i < width)
    {
        array[x+i, y+o] = value;
        i++;
    }
}

例如,您可以像这样调用它来绘制位置(2,2)的L,其高4块,宽6块:

WriteL(2, 2, 6, 4, 1, ref testarray);

enter image description here

答案 1 :(得分:0)

知道了。

//Primary arrays ....
    for (x = xStart; x <= nArray1; x++)     //record...
    {
        //try 6 here
        for (y = yStart; y < 104; y++)           //field 
        {
            //more code
        }
     }

并将我的结果2d数组表设置为

table[6,104];