C#C
C#4y ago
15 replies
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++;
        }
Was this page helpful?