C#
C#

help

Root Question Message

hmm..
hmm..12/21/2022
❔ 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++;
        }
Samarichitane
Samarichitane12/21/2022
oh gosh what kind of formatting is this
Sossenbinder
Sossenbinder12/21/2022
Do you know how to operate a debugger?
Sossenbinder
Sossenbinder12/21/2022
I think it's fairly hard to think into this code
Sossenbinder
Sossenbinder12/21/2022
But you can debug along and figure out where things go wrong for you
hmm..
hmm..12/21/2022
Im so sorry for the formatting, thanks for the suggestion, I will look into using a debugger
hmm..
hmm..12/21/2022
I am new to dotnet programming so excuse my lack of experience
hmm..
hmm..12/21/2022
I used a debugger but it didn't reveal any underlying issue I didn't see before
hmm..
hmm..12/21/2022
AntonC
AntonC12/22/2022
why casting to double and flooring? that's what integer division is for
AntonC
AntonC12/22/2022
integers can also do ceiling division
Renn
Renn12/22/2022
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
Renn
Renn12/22/2022
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)
hmm..
hmm..12/23/2022
/close
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy