i dont understand why sometimes this code works and sometimes i get an infinite loop
public List<List<int>> roomMatrix = new List<List<int>>
{
new List<int> {-1, -1, -1},
new List<int> {-1, -1, -1},
new List<int> {-1, -1, -1}
};
public List<List<bool>> roomsVisited = new List<List<bool>>
{
new List<bool> {false, false, false},
new List<bool> {false, false, false},
new List<bool> {false, false, false}
};
public List<List<bool>> roomsValid = new List<List<bool>>
{
new List<bool> {false, false, false},
new List<bool> {false, false, false},
new List<bool> {false, false, false}
};
// Start is called before the first frame update
public List<List<int>> roomMatrix = new List<List<int>>
{
new List<int> {-1, -1, -1},
new List<int> {-1, -1, -1},
new List<int> {-1, -1, -1}
};
public List<List<bool>> roomsVisited = new List<List<bool>>
{
new List<bool> {false, false, false},
new List<bool> {false, false, false},
new List<bool> {false, false, false}
};
public List<List<bool>> roomsValid = new List<List<bool>>
{
new List<bool> {false, false, false},
new List<bool> {false, false, false},
new List<bool> {false, false, false}
};
// Start is called before the first frame update
8 Replies
void Start()
{
roomCount = Random.Range(3, 7);
for(int i = 0; i < roomCount; i++)
{
horizontalChoice = Random.Range(0, 3);
verticalChoice = Random.Range(0, 3);
if (i > 0)
{
while (true)
{
if (roomsValid[verticalChoice][horizontalChoice] == true && roomsVisited[verticalChoice][horizontalChoice] == false)
{
break;
}
else
{
horizontalChoice = Random.Range(0, 3);
verticalChoice = Random.Range(0, 3);
}
}
}
upCheck = verticalChoice - 1;
downCheck = verticalChoice + 1;
leftCheck = horizontalChoice - 1;
rightCheck = horizontalChoice + 1;
for(int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
roomsValid[a][b] = false;
}
}
if (upCheck > -1)
{
roomsValid[upCheck][horizontalChoice] = true;
}
else if (downCheck < 3)
{
roomsValid[downCheck][horizontalChoice] = true;
}
else if (leftCheck > -1)
{
roomsValid[verticalChoice][leftCheck] = true;
}
else if (rightCheck < 3)
{
roomsValid[verticalChoice][rightCheck] = true;
}
roomsVisited[verticalChoice][horizontalChoice] = true;
roomMatrix[verticalChoice][horizontalChoice] = i;
}
void Start()
{
roomCount = Random.Range(3, 7);
for(int i = 0; i < roomCount; i++)
{
horizontalChoice = Random.Range(0, 3);
verticalChoice = Random.Range(0, 3);
if (i > 0)
{
while (true)
{
if (roomsValid[verticalChoice][horizontalChoice] == true && roomsVisited[verticalChoice][horizontalChoice] == false)
{
break;
}
else
{
horizontalChoice = Random.Range(0, 3);
verticalChoice = Random.Range(0, 3);
}
}
}
upCheck = verticalChoice - 1;
downCheck = verticalChoice + 1;
leftCheck = horizontalChoice - 1;
rightCheck = horizontalChoice + 1;
for(int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
roomsValid[a][b] = false;
}
}
if (upCheck > -1)
{
roomsValid[upCheck][horizontalChoice] = true;
}
else if (downCheck < 3)
{
roomsValid[downCheck][horizontalChoice] = true;
}
else if (leftCheck > -1)
{
roomsValid[verticalChoice][leftCheck] = true;
}
else if (rightCheck < 3)
{
roomsValid[verticalChoice][rightCheck] = true;
}
roomsVisited[verticalChoice][horizontalChoice] = true;
roomMatrix[verticalChoice][horizontalChoice] = i;
}
Please describe what the
Start()
is supposed to do?
First, I reorganized your code, I will continue later.
class Whatever
{
public const int N = 3;
public int[,] RoomMatrix { get; } = WhateverExtensions.GetMultiDimensionalSquare(N, -1);
public bool[,] RoomVisited { get; } = WhateverExtensions.GetMultiDimensionalSquare(N);
public bool[,] RoomValid { get; } = WhateverExtensions.GetMultiDimensionalSquare(N);
public void Start()
{
// in progress ...
// i am trying to decipher your code but it needs time ...
// let me continue later ...
}
}
static class WhateverExtensions
{
public static T[,] ToMultiDimensionalSquare<T>(this T[][] input)
{
int length = input.Length;
// make sure the input is a jagged square array
foreach (var row in input)
if (row.Length != length)
throw new Exception("Input is not a jagged square array");
// convert input to multidimensional square array
T[,] output = new T[length, length];
for (int i = 0; i < length; ++i)
for (int j = 0; j < length; ++j)
output[i, j] = input[i][j];
return output;
}
public static T[,] GetMultiDimensionalSquare<T>(int length = 3, T value = default!)
{
return Enumerable
.Range(0, length)
.Select(_ => Enumerable.Repeat(value, length).ToArray())
.ToArray()
.ToMultiDimensionalSquare();
}
}
class Whatever
{
public const int N = 3;
public int[,] RoomMatrix { get; } = WhateverExtensions.GetMultiDimensionalSquare(N, -1);
public bool[,] RoomVisited { get; } = WhateverExtensions.GetMultiDimensionalSquare(N);
public bool[,] RoomValid { get; } = WhateverExtensions.GetMultiDimensionalSquare(N);
public void Start()
{
// in progress ...
// i am trying to decipher your code but it needs time ...
// let me continue later ...
}
}
static class WhateverExtensions
{
public static T[,] ToMultiDimensionalSquare<T>(this T[][] input)
{
int length = input.Length;
// make sure the input is a jagged square array
foreach (var row in input)
if (row.Length != length)
throw new Exception("Input is not a jagged square array");
// convert input to multidimensional square array
T[,] output = new T[length, length];
for (int i = 0; i < length; ++i)
for (int j = 0; j < length; ++j)
output[i, j] = input[i][j];
return output;
}
public static T[,] GetMultiDimensionalSquare<T>(int length = 3, T value = default!)
{
return Enumerable
.Range(0, length)
.Select(_ => Enumerable.Repeat(value, length).ToArray())
.ToArray()
.ToMultiDimensionalSquare();
}
}
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
But it is in progress. My code above can easily make
n x n
matrices with one snap.Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Yes. Of course.
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Oh i fixed that yesterday dont worry about it bro
Start() is a unity method so dont worry