Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

c# - How to setup the exit condition matching the correct route

i cant seem to get the exit condition right for this

The code for moving and spacing the Array

List<string> route = new List<string>() { "Bmiddle", "Middle", "Tmiddle", "TRight", "Right", "Middle", "Left", "Bleft", "Middle", "Left" }; 
// this is the correct way the player should follow to be able to exit

        List<string> Currentroute = new List<string>();
// each time the player is at a spot it should add it to this list in order to meet the exit condition

        string[,] arrayLab = new string[3, 3] { {"Tleft",     "TMiddle",    "TRight"},   /*0 */
                                                   {"Left",       "Middle",    "Right"},    /*1 */
                                                   {"Bleft",      "Bmiddle",   "Bright"} }; /*2 */

do
{
// here is supposed to be all the controls and spacing to make it look like the array. and the logic

}
 while (Currentroute != route)
//The Current route must match the Route in order to meet this exit 

    }
}

My attempt at solving i cant seem to add the postion the player is in to the Currentroute list in order to be able to get out,

int a = playerY;
int b = playerX;

foreach (char i in arrayLab[b, a])
{
    Currentroute.Add(i.ToString());
    Console.WriteLine(i);
}

i tried this but it just spells the position char for char and doesnt save it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I did not understand exactly what you mean, but I prepared a piece of code to navigate the list.

In the end all the elements of the two lists are equal

for (int i = 0; i < route.Count; i++)
    {
       bool exitloop = false;
       for (int j = 0; j < 3; j++)
        {
           for (int k = 0; k < 3; k++)
           {
              if (arrayLab[j, k] == route[i])
              {
                 Currentroute.Add(arrayLab[j, k]);
                 exitloop = true;
                 break;
              }
           }
           if (exitloop)
              break;
       }
    }


foreach (var item in Currentroute)
{
   Console.WriteLine(item);
}

In the first list, change Tmiddle to TMiddle and do not use the while loop. I hope it was useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...