Ok so I have made the changes you have recommended. They are all fixed except for; the ‘else’ i get the errors:
- invalid expression ‘else’.
- error expected ‘;’.
Also, for the ‘elseif’ I get the errors:
- Bad array declerator. To declare a managed array the rank specifier precedes the varible’s indentifier.
- Array size cannot be specified in a variable decleration, 3 errors of expected ‘;’.
- Lastly, Error invalid ‘==’.
I’ve also added some comments to my code to make it more understandable. Btw for future reference is this the best way to show new code on this website, by simply editing it or is there a better way?
void Start ()
{
int ArraySize = 20; //Actual array size will be 21x21 as it includes 0.
Random rnd = new Random();
int[,] MapArray = new int[ArraySize, ArraySize]; //Creates a 21x21 2D array (for future reference it may help to think of the map as a graph consisting of x and y coordinates).
List<int> listxCoodinates = new List<int>(); //Creates an empty list to contain all the x-coordinates.
List<int> listyCoodinates = new List<int>(); //Creates an empty list to contain all the y-coordinates.
for (int x = 0; x < ArraySize; x++) //This loop simply adds the number of elements in the array length-wise or width-wise to 2 separate lists, I know this would not work if the MapArray had different amount ofx-coordinates to y coordinates. I may fix this later.
{
listxCoordinates.Add(x); //This is a list containing all the x-coordinates of the map.
listyCoordinates.Add(x); //This is a list containing all the y-coordinates of the map.
MapArray[x, 0] = 1; //This is for creating the outer walls of the map (1 represents a wall, 0 represents the floor).
MapArray[x, MapArray.GetLength(0) - 1] = 1;
MapArray[0, x] = 1;
MapArray[MapArray.GetLength(1) - 1, x] = 1;
}
int RandomxOry = rnd.Next(0, 2); //This is for creating a variable containing either 1 or 0 chosen randomly. This will be used in deciding whether to start a wall going vertically or horizontally.
listxCoordinates.Remove(0, 1, MapArray.GetLength(0), (MapArray.GetLength(0) - 1)); //Removes a few x coodinates from the list of the possible x coordinates to be chosen from as they already are taken up as a wall or floor.
listyCoordinates.Remove(0, 1, MapArray.GetLength(1), (MapArray.GetLength(1) - 1)); //Removes a few y coodinates from the list of the possible y coordinates to be chosen from as they already are taken up as a wall or floor.
int RandomxCoordinate = rnd.Next(listxCoordinates.Count); //This gives a random index, not a random number in the list for the bot side of the map.
int RandomyCoordinate = rnd.Next(listyCoordinates.count); //This gives a random index, not a random number in the list for the left side of map.
if (RandomxOry == 0) //0 represents x side (Wall will be vertical).
{
MapArray[listxCoordinates[RandomxCoordinate], 1] = 1; //Makes MapArray at the random coordinate on the x-side equal 1 (Wall).
if (listxCoordinates[RandomxCoordinate] == 0) //if the index of the random x coodinate in the list listxCoordinates = 0 then
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1); //Removes item at index in list 0 and 1.
for (int x = 1; x < MapArray.GetLength(0); x++) //This loop is for making every 0 above the random coordinate (start of the wall) equal to 1 (wall) until it reaches another 1 (wall).
{
if (MapArray[listxCoordinates[RandomxCoordinate], x + 1] == 0)
{
MapArray[listxCoordinates[RandomxCoordinate], x + 1] = 1;
}
else
{
x = MapArray.GetLength(0); //This is for ending the loop
}
}
}
elseif listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1 //if the index of the random x coodinate in the list listxCoordinates = maximum index in list then
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate - 1); //Removes item at index in list maximum index and (maximum index - 1).
}
else
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1, RandomxCoordinate - 1); //Removes item at index in list RandomxCoordinates and the items at indexes next to that RandomxCoordinates index.
}
}
if (RandomxOry == 1)
{
MapArray[1, listyCoordinates[RandomyCoordinate]] = 1;
if (listyCoordinates[RandomyCoordinate] == 0)
{
listyCoordinates.RemoveAt(RandomyCoordinate, RandomyCoordinate + 1);
}
elseif listyCoordinates[RandomyCoordinate] == listyCoordinates.Count - 1
{
listyCoordinates.RemoveAt(RandomyCoordinate);
listyCoordinates.RemoveAt(RandomyCoordinate - 1);
}
else
{
listyCoordinates.RemoveAt(RandomyCoordinate);
listyCoordinates.RemoveAt(RandomyCoordinate + 1);
listyCoordinates.RemoveAt(RandomyCoordinate - 1);
}
}
}