代码之家  ›  专栏  ›  技术社区  ›  Ginxxx

二维数组未获得预期输出

  •  2
  • Ginxxx  · 技术社区  · 6 年前

    我先告诉你我的结果

    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];
    

    原件是:

    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 回复  |  直到 6 年前
        1
  •  0
  •   N.D.C.    6 年前

    现在还不清楚你的目标是什么(而且很难阅读你的代码)。这是一个如何将L写入二维数组的示例函数:

    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

        2
  •  0
  •   Ginxxx    6 年前

    知道了。

    //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];