C
C#2y ago
hmm..

❔ Synchronous function result problem

I have a synchronous function that runs a couple of for and foreach loops that generates a matrix that is returned at the end of the program. The problem is that my program returns a matrix with a random length between 0 and 1 (including) when it should be returning a matrix with length of 16. Code (sorry about the formatting, weird pasting issue | please ask if I am missing any information, I am knew to this stuff):
public static List < List < bool >> GenerateLevel(int left, int right, int top, int bottom) {
TaskCompletionSource < List < List < bool >>> tcs = new TaskCompletionSource < List < List < bool >>> ();

List < List < bool >> WALLS = new List < List < bool >> ();

int wallsize = 100;

int rleft = (int) Math.Floor((double)(left + wallsize) / wallsize);
int rright = (int) Math.Floor((double)(right) / wallsize);
int rtop = (int) Math.Floor((double)(top + wallsize) / wallsize);
int rbottom = (int) Math.Floor((double)(bottom - wallsize) / wallsize);

List < List < bool >> cells = new List < List < bool >> ();

double total = (rright - rleft) * (rbottom - rtop);

Random random = new Random();

for (int y = rtop; y < rbottom; y++) {
List < bool > row = new List < bool > ();
for (int x = rleft; x < rright; x++) {
double rand = random.NextDouble() * 100;

row.Add(rand <= 30);
}

cells.Add(row);
Console.WriteLine(cells);
}

int _y = rtop;
int _x = rleft;

foreach(List < bool > row in cells.ToList()) {
List < bool > _row = new List < bool > ();
_x = rleft;
foreach(bool cell in row.ToList()) {
if (cell) {
row.Add(true);
}
_x++;

if (_x - rtop + 1 == row.Count) {
WALLS.Add(_row);
}
}

_y++;
}
public static List < List < bool >> GenerateLevel(int left, int right, int top, int bottom) {
TaskCompletionSource < List < List < bool >>> tcs = new TaskCompletionSource < List < List < bool >>> ();

List < List < bool >> WALLS = new List < List < bool >> ();

int wallsize = 100;

int rleft = (int) Math.Floor((double)(left + wallsize) / wallsize);
int rright = (int) Math.Floor((double)(right) / wallsize);
int rtop = (int) Math.Floor((double)(top + wallsize) / wallsize);
int rbottom = (int) Math.Floor((double)(bottom - wallsize) / wallsize);

List < List < bool >> cells = new List < List < bool >> ();

double total = (rright - rleft) * (rbottom - rtop);

Random random = new Random();

for (int y = rtop; y < rbottom; y++) {
List < bool > row = new List < bool > ();
for (int x = rleft; x < rright; x++) {
double rand = random.NextDouble() * 100;

row.Add(rand <= 30);
}

cells.Add(row);
Console.WriteLine(cells);
}

int _y = rtop;
int _x = rleft;

foreach(List < bool > row in cells.ToList()) {
List < bool > _row = new List < bool > ();
_x = rleft;
foreach(bool cell in row.ToList()) {
if (cell) {
row.Add(true);
}
_x++;

if (_x - rtop + 1 == row.Count) {
WALLS.Add(_row);
}
}

_y++;
}
9 Replies
Servator
Servator2y ago
oh gosh what kind of formatting is this
Sossenbinder
Sossenbinder2y ago
Do you know how to operate a debugger? I think it's fairly hard to think into this code But you can debug along and figure out where things go wrong for you
hmm..
hmm..OP2y ago
Im so sorry for the formatting, thanks for the suggestion, I will look into using a debugger I am new to dotnet programming so excuse my lack of experience I used a debugger but it didn't reveal any underlying issue I didn't see before
hmm..
hmm..OP2y ago
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
Anton
Anton2y ago
why casting to double and flooring? that's what integer division is for integers can also do ceiling division
Renn
Renn2y ago
yeah this is definitely a candidate for a debugger. Set a breakpoint at your first for loop and see what rbottom is. It's based on a parameter and then you do some math on it so you should confirm that it's actually going to loop the requisite amount You also really shouldn't be using nested lists for this. This is a perfect candidate for a matrix (something like var walls = new bool[maxX, maxY] and the same for cells)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
hmm..
hmm..OP2y ago
/close
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server