如何将第二个值存储到最后一个值以与新值

时间:2018-05-17 03:58:26

标签: c# android unity3d

我有这段代码

string[,] table = new string[104, 15];
int xIndex = -1;
int yIndex = 0;
string newPrevious = "placeholder";
//P = BLUE, B = RED, T = GREEN
string[] strData = {"P  ,B  ,P  ,P B,B  ,P  ,B  ,T  ,P  ,P  "};

//conditions
string[] scoreBoard = new string[]
{"P  ", "B  ", "T  ",
 "P B","B P" };
string OriginalData = "";


void Start(){
    StartCoroutine ("Win_Log");
}

IEnumerator Win_Log(){

    yield return new WaitForEndOfFrame ();

    for (int i = 0; i < strData.Length; i++) {
        OriginalData += strData [i];
        OriginalData += ",";
    }
    string[] newNewData = OriginalData.Split (',');
    string result = "";

    foreach (string newStrData in newNewData) {
        Debug.Log ("This is the data : " + newStrData);

        GameObject o = Instantiate (prefab_gameobject) as GameObject;
        o.transform.SetParent (pos_big_road);
        o.transform.localScale = Vector3.one;

        img = (RawImage)o.GetComponent<RawImage> ();

        //check the length so that it won't throw an exception
        if (newStrData.Length > 1) {
            //get only the first letter of the value P,B,T
            result = newStrData.Substring (0, 1);
        } 
        else {
            result = "";
        }

        #region BIG ROAD
        if(table.GetLength(0) < xIndex){
            break;
        }

        if (result.Equals (newPrevious) || result.Equals("T") && yIndex < table.GetLength (1)) {
            yIndex += 1;
            table [xIndex, yIndex] = result;
        }
        else if(newPrevious.Equals("T") && yIndex < table.GetLength (1)){
            yIndex += 1;
            table [xIndex, yIndex] = result;
        }
        else
        {
            xIndex += 1;
            yIndex = 0;
            table [xIndex, yIndex] = result;
        }
        newPrevious = result ;

        o.transform.localPosition = new Vector3 (xIndex * 93, yIndex * -93, 0f);

我的问题是这个

enter image description here

正如您在上一栏

中看到的那样
  

红,绿,青,蓝

它的值是

//P = BLUE, B = RED, T = GREEN
string[] strData = {"P  ,B  ,P  ,P B,B  ,P  ,B  ,T  ,P  ,P  "};

BLUE, BLUE必须位于下一行,因为它不等于newPrevious变量,我所说的RED值高于GREEN值。所以你必须对此感到困惑,但是GREEN值更像是被忽视我的意思被忽视是这样的

enter image description here

现在最后一列的值是

  

蓝色,蓝色,绿色,红色,红色

其值为

//P = BLUE, B = RED, T = GREEN
string[] strData = {"P  ,B  ,P  ,P B,B  ,P  ,P  ,T  ,B  ,B  "};

正如您在图片中看到的那样,RED, RED必须位于下一行,因为它不等于newPrevious变量,而我所说的BLUE上面的GREEN newPrevious值。

现在我想要做什么&#39;实现:

我在这里可以实现的是将GREEN的值放到一个变量中,在该变量中它将值存储在newPrevious之前,这样我就可以将它与新的RED进行比较{1}}值。

例如,上面的图片具有值

  

红色,绿色,蓝色,蓝色

我想保存BLUE值,以便我可以将其与xIndex值进行比较,以便我可以使用yIndex代替(?i)(?:0x(?:[0-9a-f]+(?:'?[0-9a-f]+)*)|0b(?:[10]+(?:'?[10]+)*)|\d+(?:'?\d+)*)(?:ull|ll|ul|l|u)?

修改

第一个图像预期输出必须是此

enter image description here

第二个图像预期输出必须像这样

enter image description here

1 个答案:

答案 0 :(得分:2)

取另一个变量“previous”。将它设置为newPrevious的值,就像现在一样,并在下一次迭代中检查它是否已更改。

替换此

newPrevious = result ;

有了这个

if(!result.Equals("T")) 
   newPrevious = result ;